frmMain.pas 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. (* _ _
  2. * | |__ _ __ ___ ___ | | __
  3. * | '_ \| '__/ _ \ / _ \| |/ /
  4. * | |_) | | | (_) | (_) | <
  5. * |_.__/|_| \___/ \___/|_|\_\
  6. *
  7. * Microframework which helps to develop web Pascal applications.
  8. *
  9. * Copyright (c) 2012-2021 Silvio Clecio <[email protected]>
  10. *
  11. * Brook framework is free software; you can redistribute it and/or
  12. * modify it under the terms of the GNU Lesser General Public
  13. * License as published by the Free Software Foundation; either
  14. * version 2.1 of the License, or (at your option) any later version.
  15. *
  16. * Brook framework is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  19. * Lesser General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Lesser General Public
  22. * License along with Brook framework; if not, write to the Free Software
  23. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  24. *)
  25. unit frmMain;
  26. {$MODE DELPHI}
  27. interface
  28. uses
  29. SysUtils,
  30. Classes,
  31. StdCtrls,
  32. ActnList,
  33. Graphics,
  34. Spin,
  35. Dialogs,
  36. Forms,
  37. LCLIntf,
  38. BrookHTTPRequest,
  39. BrookHTTPResponse,
  40. BrookHTTPServer,
  41. DMPersistence;
  42. type
  43. TfrMain = class(TForm)
  44. acStart: TAction;
  45. acStop: TAction;
  46. alMain: TActionList;
  47. BrookHTTPServer1: TBrookHTTPServer;
  48. btStart: TButton;
  49. btStop: TButton;
  50. edPort: TSpinEdit;
  51. lbLink: TLabel;
  52. lbPort: TLabel;
  53. procedure acStartExecute(Sender: TObject);
  54. procedure acStopExecute(Sender: TObject);
  55. procedure BrookHTTPServer1Request(ASender: TObject;
  56. ARequest: TBrookHTTPRequest; AResponse: TBrookHTTPResponse);
  57. procedure BrookHTTPServer1RequestError(ASender: TObject;
  58. ARequest: TBrookHTTPRequest; AResponse: TBrookHTTPResponse;
  59. AException: Exception);
  60. procedure BrookHTTPServer1Start(Sender: TObject);
  61. procedure BrookHTTPServer1Stop(Sender: TObject);
  62. procedure edPortChange(Sender: TObject);
  63. procedure FormShow(Sender: TObject);
  64. procedure lbLinkClick(Sender: TObject);
  65. procedure lbLinkMouseEnter(Sender: TObject);
  66. procedure lbLinkMouseLeave(Sender: TObject);
  67. public
  68. procedure UpdateControls; {$IFNDEF DEBUG}inline;{$ENDIF}
  69. end;
  70. var
  71. frMain: TfrMain;
  72. implementation
  73. {$R *.lfm}
  74. procedure TfrMain.UpdateControls;
  75. begin
  76. if BrookHTTPServer1.Active then
  77. edPort.Value := BrookHTTPServer1.Port
  78. else
  79. BrookHTTPServer1.Port := edPort.Value;
  80. lbLink.Caption := Concat('http://localhost:', edPort.Value.ToString);
  81. acStart.Enabled := not BrookHTTPServer1.Active;
  82. acStop.Enabled := not acStart.Enabled;
  83. edPort.Enabled := acStart.Enabled;
  84. lbLink.Enabled := not acStart.Enabled;
  85. end;
  86. procedure TfrMain.acStartExecute(Sender: TObject);
  87. begin
  88. BrookHTTPServer1.Open;
  89. end;
  90. procedure TfrMain.acStopExecute(Sender: TObject);
  91. begin
  92. BrookHTTPServer1.Close;
  93. end;
  94. procedure TfrMain.lbLinkMouseEnter(Sender: TObject);
  95. begin
  96. lbLink.Font.Style := lbLink.Font.Style + [fsUnderline];
  97. end;
  98. procedure TfrMain.lbLinkMouseLeave(Sender: TObject);
  99. begin
  100. lbLink.Font.Style := lbLink.Font.Style - [fsUnderline];
  101. end;
  102. procedure TfrMain.lbLinkClick(Sender: TObject);
  103. begin
  104. OpenURL(lbLink.Caption);
  105. end;
  106. procedure TfrMain.BrookHTTPServer1Request(ASender: TObject;
  107. ARequest: TBrookHTTPRequest; AResponse: TBrookHTTPResponse);
  108. begin
  109. if ARequest.Payload.Length > 0 then
  110. Persistence.SavePersons(ARequest.Payload.Content)
  111. else
  112. AResponse.SendStream(Persistence.ListPersons, 200);
  113. end;
  114. procedure TfrMain.BrookHTTPServer1RequestError(ASender: TObject;
  115. ARequest: TBrookHTTPRequest; AResponse: TBrookHTTPResponse;
  116. AException: Exception);
  117. begin
  118. AResponse.SendFmt(
  119. '<html><head><title>Error</title></head><body><font color="red">%s</font></body></html>',
  120. [AException.Message], 'text/html; charset=utf-8', 500);
  121. end;
  122. procedure TfrMain.BrookHTTPServer1Start(Sender: TObject);
  123. begin
  124. UpdateControls;
  125. end;
  126. procedure TfrMain.BrookHTTPServer1Stop(Sender: TObject);
  127. begin
  128. UpdateControls;
  129. end;
  130. procedure TfrMain.edPortChange(Sender: TObject);
  131. begin
  132. UpdateControls;
  133. end;
  134. procedure TfrMain.FormShow(Sender: TObject);
  135. begin
  136. edPort.Value := 8080;
  137. acStart.Execute;
  138. end;
  139. end.