URLRouter_frMain.pas 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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 URLRouter_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. BrookURLRouter,
  48. Utility;
  49. type
  50. TfrMain = class(TForm)
  51. alMain: TActionList;
  52. acStart: TAction;
  53. acStop: TAction;
  54. BrookHTTPServer1: TBrookHTTPServer;
  55. lbLink: TLabel;
  56. pnTop: TPanel;
  57. lbPort: TLabel;
  58. edPort: TNumberBox;
  59. btStart: TButton;
  60. btStop: TButton;
  61. BrookURLRouter1: TBrookURLRouter;
  62. procedure acStartExecute(Sender: TObject);
  63. procedure acStopExecute(Sender: TObject);
  64. procedure lbLinkMouseEnter(Sender: TObject);
  65. procedure lbLinkMouseLeave(Sender: TObject);
  66. procedure lbLinkClick(Sender: TObject);
  67. procedure BrookHTTPServer1Request(ASender: TObject;
  68. ARequest: TBrookHTTPRequest; AResponse: TBrookHTTPResponse);
  69. procedure BrookHTTPServer1RequestError(ASender: TObject;
  70. ARequest: TBrookHTTPRequest; AResponse: TBrookHTTPResponse;
  71. AException: Exception);
  72. procedure BrookURLRouter1NotFound(ASender: TObject; const ARoute: string;
  73. ARequest: TBrookHTTPRequest; AResponse: TBrookHTTPResponse);
  74. procedure BrookURLRouter1Routes0Request(ASender: TObject;
  75. ARoute: TBrookURLRoute; ARequest: TBrookHTTPRequest;
  76. AResponse: TBrookHTTPResponse);
  77. procedure BrookURLRouter1Routes1Request(ASender: TObject;
  78. ARoute: TBrookURLRoute; ARequest: TBrookHTTPRequest;
  79. AResponse: TBrookHTTPResponse);
  80. procedure BrookURLRouter1Routes2Request(ASender: TObject;
  81. ARoute: TBrookURLRoute; ARequest: TBrookHTTPRequest;
  82. AResponse: TBrookHTTPResponse);
  83. procedure BrookHTTPServer1Start(Sender: TObject);
  84. procedure BrookHTTPServer1Stop(Sender: TObject);
  85. procedure edPortChange(Sender: TObject);
  86. procedure edPortChangeTracking(Sender: TObject);
  87. procedure BrookHTTPServer1Error(ASender: TObject; AException: Exception);
  88. public
  89. procedure UpdateControls; inline;
  90. end;
  91. var
  92. frMain: TfrMain;
  93. implementation
  94. {$R *.fmx}
  95. procedure TfrMain.UpdateControls;
  96. begin
  97. if Application.Terminated then
  98. Exit;
  99. if BrookHTTPServer1.Active then
  100. edPort.Value := BrookHTTPServer1.Port
  101. else
  102. BrookHTTPServer1.Port := edPort.Text.ToInteger;
  103. lbLink.Text := Concat('http://localhost:', edPort.Value.ToString);
  104. acStart.Enabled := not BrookHTTPServer1.Active;
  105. acStop.Enabled := not acStart.Enabled;
  106. edPort.Enabled := acStart.Enabled;
  107. lbLink.Enabled := not acStart.Enabled;
  108. end;
  109. procedure TfrMain.acStartExecute(Sender: TObject);
  110. begin
  111. BrookURLRouter1.Open;
  112. BrookHTTPServer1.Open;
  113. end;
  114. procedure TfrMain.acStopExecute(Sender: TObject);
  115. begin
  116. BrookHTTPServer1.Close;
  117. end;
  118. procedure TfrMain.lbLinkMouseEnter(Sender: TObject);
  119. begin
  120. lbLink.Font.Style := lbLink.Font.Style + [TFontStyle.fsUnderline];
  121. end;
  122. procedure TfrMain.lbLinkMouseLeave(Sender: TObject);
  123. begin
  124. lbLink.Font.Style := lbLink.Font.Style - [TFontStyle.fsUnderline];
  125. end;
  126. procedure TfrMain.lbLinkClick(Sender: TObject);
  127. begin
  128. Utility.OpenURL(lbLink.Text);
  129. end;
  130. procedure TfrMain.BrookHTTPServer1Request(ASender: TObject;
  131. ARequest: TBrookHTTPRequest; AResponse: TBrookHTTPResponse);
  132. begin
  133. BrookURLRouter1.Route(ASender, ARequest, AResponse);
  134. end;
  135. procedure TfrMain.BrookHTTPServer1RequestError(ASender: TObject;
  136. ARequest: TBrookHTTPRequest; AResponse: TBrookHTTPResponse;
  137. AException: Exception);
  138. begin
  139. AResponse.SendFmt(
  140. '<html><head><title>Error</title></head><body><font color="red">%s</font></body></html>',
  141. [AException.Message], 'text/html; charset=utf-8', 500);
  142. end;
  143. procedure TfrMain.BrookURLRouter1NotFound(ASender: TObject;
  144. const ARoute: string; ARequest: TBrookHTTPRequest;
  145. AResponse: TBrookHTTPResponse);
  146. begin
  147. AResponse.SendFmt(
  148. '<html><head><title>Not found</title></head><body>Page not found: %s</body></html>',
  149. [ARequest.Path], 'text/html; charset=utf-8', 404);
  150. end;
  151. procedure TfrMain.BrookURLRouter1Routes0Request(ASender: TObject;
  152. ARoute: TBrookURLRoute; ARequest: TBrookHTTPRequest;
  153. AResponse: TBrookHTTPResponse);
  154. begin
  155. AResponse.Send(
  156. '<html><head><title>Home page</title></head><body>Home page</body></html>',
  157. 'text/html; charset=utf-8', 200);
  158. end;
  159. procedure TfrMain.BrookURLRouter1Routes1Request(ASender: TObject;
  160. ARoute: TBrookURLRoute; ARequest: TBrookHTTPRequest;
  161. AResponse: TBrookHTTPResponse);
  162. begin
  163. AResponse.SendFmt(
  164. '<html><head><title>Downloads</title></head><body>Downloaded file: %s</body></html>',
  165. [ARoute.Variables['file']], 'text/html; charset=utf-8', 200);
  166. end;
  167. procedure TfrMain.BrookURLRouter1Routes2Request(ASender: TObject;
  168. ARoute: TBrookURLRoute; ARequest: TBrookHTTPRequest;
  169. AResponse: TBrookHTTPResponse);
  170. begin
  171. AResponse.SendFmt(
  172. '<html><head><title>Page</title></head><body>Page number: %d</body></html>',
  173. [ARoute.Segments[0].ToInteger], 'text/html; charset=utf-8', 200);
  174. end;
  175. procedure TfrMain.BrookHTTPServer1Start(Sender: TObject);
  176. begin
  177. UpdateControls;
  178. end;
  179. procedure TfrMain.BrookHTTPServer1Stop(Sender: TObject);
  180. begin
  181. UpdateControls;
  182. end;
  183. procedure TfrMain.edPortChange(Sender: TObject);
  184. begin
  185. UpdateControls;
  186. end;
  187. procedure TfrMain.edPortChangeTracking(Sender: TObject);
  188. begin
  189. UpdateControls;
  190. end;
  191. procedure TfrMain.BrookHTTPServer1Error(ASender: TObject;
  192. AException: Exception);
  193. begin
  194. TThread.Synchronize(nil,
  195. procedure
  196. begin
  197. TDialogService.MessageDialog(AException.Message, TMsgDlgType.mtError,
  198. [TMsgDlgBtn.mbOK], TMsgDlgBtn.mbOK, 0, nil);
  199. end);
  200. end;
  201. end.