HTTPServer_frMain.pas 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. (* _ _
  2. * | |__ _ __ ___ ___ | | __
  3. * | '_ \| '__/ _ \ / _ \| |/ /
  4. * | |_) | | | (_) | (_) | <
  5. * |_.__/|_| \___/ \___/|_|\_\
  6. *
  7. * Microframework which helps to develop web Pascal applications.
  8. *
  9. * Copyright (c) 2012-2020 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 HTTPServer_frMain;
  26. interface
  27. uses
  28. System.SysUtils,
  29. System.UITypes,
  30. System.Classes,
  31. System.Actions,
  32. FMX.Types,
  33. FMX.ActnList,
  34. FMX.Graphics,
  35. FMX.Controls,
  36. FMX.StdCtrls,
  37. FMX.Edit,
  38. FMX.EditBox,
  39. FMX.NumberBox,
  40. FMX.DialogService,
  41. FMX.Forms,
  42. FMX.Controls.Presentation,
  43. BrookHandledClasses,
  44. BrookHTTPRequest,
  45. BrookHTTPResponse,
  46. BrookHTTPServer,
  47. Utility;
  48. type
  49. TfrMain = class(TForm)
  50. lbPort: TLabel;
  51. edPort: TNumberBox;
  52. btStart: TButton;
  53. btStop: TButton;
  54. lbLink: TLabel;
  55. alMain: TActionList;
  56. acStart: TAction;
  57. acStop: TAction;
  58. BrookHTTPServer1: TBrookHTTPServer;
  59. pnTop: TPanel;
  60. procedure acStartExecute(Sender: TObject);
  61. procedure acStopExecute(Sender: TObject);
  62. procedure lbLinkMouseEnter(Sender: TObject);
  63. procedure lbLinkMouseLeave(Sender: TObject);
  64. procedure lbLinkClick(Sender: TObject);
  65. procedure BrookHTTPServer1Request(ASender: TObject;
  66. ARequest: TBrookHTTPRequest; AResponse: TBrookHTTPResponse);
  67. procedure BrookHTTPServer1RequestError(ASender: TObject;
  68. ARequest: TBrookHTTPRequest; AResponse: TBrookHTTPResponse;
  69. AException: Exception);
  70. procedure BrookHTTPServer1Error(ASender: TObject; AException: Exception);
  71. procedure BrookHTTPServer1Start(Sender: TObject);
  72. procedure BrookHTTPServer1Stop(Sender: TObject);
  73. procedure edPortChange(Sender: TObject);
  74. procedure edPortChangeTracking(Sender: TObject);
  75. public
  76. procedure UpdateControls; inline;
  77. end;
  78. var
  79. frMain: TfrMain;
  80. implementation
  81. {$R *.fmx}
  82. procedure TfrMain.UpdateControls;
  83. begin
  84. if Application.Terminated then
  85. Exit;
  86. if BrookHTTPServer1.Active then
  87. edPort.Value := BrookHTTPServer1.Port
  88. else
  89. BrookHTTPServer1.Port := edPort.Text.ToInteger;
  90. lbLink.Text := Concat('http://localhost:', edPort.Value.ToString);
  91. acStart.Enabled := not BrookHTTPServer1.Active;
  92. acStop.Enabled := not acStart.Enabled;
  93. edPort.Enabled := acStart.Enabled;
  94. lbLink.Enabled := not acStart.Enabled;
  95. end;
  96. procedure TfrMain.acStartExecute(Sender: TObject);
  97. begin
  98. BrookHTTPServer1.Open;
  99. end;
  100. procedure TfrMain.acStopExecute(Sender: TObject);
  101. begin
  102. BrookHTTPServer1.Close;
  103. end;
  104. procedure TfrMain.lbLinkMouseEnter(Sender: TObject);
  105. begin
  106. lbLink.Font.Style := lbLink.Font.Style + [TFontStyle.fsUnderline];
  107. end;
  108. procedure TfrMain.lbLinkMouseLeave(Sender: TObject);
  109. begin
  110. lbLink.Font.Style := lbLink.Font.Style - [TFontStyle.fsUnderline];
  111. end;
  112. procedure TfrMain.lbLinkClick(Sender: TObject);
  113. begin
  114. Utility.OpenURL(lbLink.Text);
  115. end;
  116. procedure TfrMain.BrookHTTPServer1Request(ASender: TObject;
  117. ARequest: TBrookHTTPRequest; AResponse: TBrookHTTPResponse);
  118. begin
  119. AResponse.Send(
  120. '<html><head><title>Hello world</title></head><body>Hello world</body></html>',
  121. 'text/html; charset=utf-8', 200);
  122. end;
  123. procedure TfrMain.BrookHTTPServer1RequestError(ASender: TObject;
  124. ARequest: TBrookHTTPRequest; AResponse: TBrookHTTPResponse;
  125. AException: Exception);
  126. begin
  127. AResponse.SendFmt(
  128. '<html><head><title>Error</title></head><body><font color="red">%s</font></body></html>',
  129. [AException.Message], 'text/html; charset=utf-8', 500);
  130. end;
  131. procedure TfrMain.BrookHTTPServer1Start(Sender: TObject);
  132. begin
  133. UpdateControls;
  134. end;
  135. procedure TfrMain.BrookHTTPServer1Stop(Sender: TObject);
  136. begin
  137. UpdateControls;
  138. end;
  139. procedure TfrMain.edPortChange(Sender: TObject);
  140. begin
  141. UpdateControls;
  142. end;
  143. procedure TfrMain.edPortChangeTracking(Sender: TObject);
  144. begin
  145. UpdateControls;
  146. end;
  147. procedure TfrMain.BrookHTTPServer1Error(ASender: TObject;
  148. AException: Exception);
  149. begin
  150. TThread.Synchronize(nil,
  151. procedure
  152. begin
  153. TDialogService.MessageDialog(AException.Message, TMsgDlgType.mtError,
  154. [TMsgDlgBtn.mbOK], TMsgDlgBtn.mbOK, 0, nil);
  155. end);
  156. end;
  157. end.