HTTPServer_frMain.pas 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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 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 BrookHTTPServer1Start(Sender: TObject);
  71. procedure BrookHTTPServer1Stop(Sender: TObject);
  72. procedure edPortChange(Sender: TObject);
  73. procedure edPortChangeTracking(Sender: TObject);
  74. public
  75. procedure UpdateControls; {$IFNDEF DEBUG}inline;{$ENDIF}
  76. end;
  77. var
  78. frMain: TfrMain;
  79. implementation
  80. {$R *.fmx}
  81. procedure TfrMain.UpdateControls;
  82. begin
  83. if Application.Terminated then
  84. Exit;
  85. if BrookHTTPServer1.Active then
  86. edPort.Value := BrookHTTPServer1.Port
  87. else
  88. BrookHTTPServer1.Port := edPort.Text.ToInteger;
  89. lbLink.Text := Concat('http://localhost:', edPort.Value.ToString);
  90. acStart.Enabled := not BrookHTTPServer1.Active;
  91. acStop.Enabled := not acStart.Enabled;
  92. edPort.Enabled := acStart.Enabled;
  93. lbLink.Enabled := not acStart.Enabled;
  94. end;
  95. procedure TfrMain.acStartExecute(Sender: TObject);
  96. begin
  97. BrookHTTPServer1.Open;
  98. end;
  99. procedure TfrMain.acStopExecute(Sender: TObject);
  100. begin
  101. BrookHTTPServer1.Close;
  102. end;
  103. procedure TfrMain.lbLinkMouseEnter(Sender: TObject);
  104. begin
  105. lbLink.Font.Style := lbLink.Font.Style + [TFontStyle.fsUnderline];
  106. end;
  107. procedure TfrMain.lbLinkMouseLeave(Sender: TObject);
  108. begin
  109. lbLink.Font.Style := lbLink.Font.Style - [TFontStyle.fsUnderline];
  110. end;
  111. procedure TfrMain.lbLinkClick(Sender: TObject);
  112. begin
  113. Utility.OpenURL(lbLink.Text);
  114. end;
  115. procedure TfrMain.BrookHTTPServer1Request(ASender: TObject;
  116. ARequest: TBrookHTTPRequest; AResponse: TBrookHTTPResponse);
  117. begin
  118. AResponse.Send(
  119. '<html><head><title>Hello world</title></head><body>Hello world</body></html>',
  120. 'text/html; charset=utf-8', 200);
  121. end;
  122. procedure TfrMain.BrookHTTPServer1RequestError(ASender: TObject;
  123. ARequest: TBrookHTTPRequest; AResponse: TBrookHTTPResponse;
  124. AException: Exception);
  125. begin
  126. AResponse.SendFmt(
  127. '<html><head><title>Error</title></head><body><font color="red">%s</font></body></html>',
  128. [AException.Message], 'text/html; charset=utf-8', 500);
  129. end;
  130. procedure TfrMain.BrookHTTPServer1Start(Sender: TObject);
  131. begin
  132. UpdateControls;
  133. end;
  134. procedure TfrMain.BrookHTTPServer1Stop(Sender: TObject);
  135. begin
  136. UpdateControls;
  137. end;
  138. procedure TfrMain.edPortChange(Sender: TObject);
  139. begin
  140. UpdateControls;
  141. end;
  142. procedure TfrMain.edPortChangeTracking(Sender: TObject);
  143. begin
  144. UpdateControls;
  145. end;
  146. end.