URLRouter_frMain.pas 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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 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. public
  88. procedure UpdateControls; {$IFNDEF DEBUG}inline;{$ENDIF}
  89. end;
  90. var
  91. frMain: TfrMain;
  92. implementation
  93. {$R *.fmx}
  94. procedure TfrMain.UpdateControls;
  95. begin
  96. if Application.Terminated then
  97. Exit;
  98. if BrookHTTPServer1.Active then
  99. edPort.Value := BrookHTTPServer1.Port
  100. else
  101. BrookHTTPServer1.Port := edPort.Text.ToInteger;
  102. lbLink.Text := Concat('http://localhost:', edPort.Value.ToString);
  103. acStart.Enabled := not BrookHTTPServer1.Active;
  104. acStop.Enabled := not acStart.Enabled;
  105. edPort.Enabled := acStart.Enabled;
  106. lbLink.Enabled := not acStart.Enabled;
  107. end;
  108. procedure TfrMain.acStartExecute(Sender: TObject);
  109. begin
  110. BrookURLRouter1.Open;
  111. BrookHTTPServer1.Open;
  112. end;
  113. procedure TfrMain.acStopExecute(Sender: TObject);
  114. begin
  115. BrookHTTPServer1.Close;
  116. end;
  117. procedure TfrMain.lbLinkMouseEnter(Sender: TObject);
  118. begin
  119. lbLink.Font.Style := lbLink.Font.Style + [TFontStyle.fsUnderline];
  120. end;
  121. procedure TfrMain.lbLinkMouseLeave(Sender: TObject);
  122. begin
  123. lbLink.Font.Style := lbLink.Font.Style - [TFontStyle.fsUnderline];
  124. end;
  125. procedure TfrMain.lbLinkClick(Sender: TObject);
  126. begin
  127. Utility.OpenURL(lbLink.Text);
  128. end;
  129. procedure TfrMain.BrookHTTPServer1Request(ASender: TObject;
  130. ARequest: TBrookHTTPRequest; AResponse: TBrookHTTPResponse);
  131. begin
  132. BrookURLRouter1.Route(ASender, ARequest, AResponse);
  133. end;
  134. procedure TfrMain.BrookHTTPServer1RequestError(ASender: TObject;
  135. ARequest: TBrookHTTPRequest; AResponse: TBrookHTTPResponse;
  136. AException: Exception);
  137. begin
  138. AResponse.SendFmt(
  139. '<html><head><title>Error</title></head><body><font color="red">%s</font></body></html>',
  140. [AException.Message], 'text/html; charset=utf-8', 500);
  141. end;
  142. procedure TfrMain.BrookURLRouter1NotFound(ASender: TObject;
  143. const ARoute: string; ARequest: TBrookHTTPRequest;
  144. AResponse: TBrookHTTPResponse);
  145. begin
  146. AResponse.SendFmt(
  147. '<html><head><title>Not found</title></head><body>Page not found: %s</body></html>',
  148. [ARequest.Path], 'text/html; charset=utf-8', 404);
  149. end;
  150. procedure TfrMain.BrookURLRouter1Routes0Request(ASender: TObject;
  151. ARoute: TBrookURLRoute; ARequest: TBrookHTTPRequest;
  152. AResponse: TBrookHTTPResponse);
  153. begin
  154. AResponse.Send(
  155. '<html><head><title>Home page</title></head><body>Home page</body></html>',
  156. 'text/html; charset=utf-8', 200);
  157. end;
  158. procedure TfrMain.BrookURLRouter1Routes1Request(ASender: TObject;
  159. ARoute: TBrookURLRoute; ARequest: TBrookHTTPRequest;
  160. AResponse: TBrookHTTPResponse);
  161. begin
  162. AResponse.SendFmt(
  163. '<html><head><title>Downloads</title></head><body>Downloaded file: %s</body></html>',
  164. [ARoute.Variables['file']], 'text/html; charset=utf-8', 200);
  165. end;
  166. procedure TfrMain.BrookURLRouter1Routes2Request(ASender: TObject;
  167. ARoute: TBrookURLRoute; ARequest: TBrookHTTPRequest;
  168. AResponse: TBrookHTTPResponse);
  169. begin
  170. AResponse.SendFmt(
  171. '<html><head><title>Page</title></head><body>Page number: %d</body></html>',
  172. [ARoute.Segments[0].ToInteger], 'text/html; charset=utf-8', 200);
  173. end;
  174. procedure TfrMain.BrookHTTPServer1Start(Sender: TObject);
  175. begin
  176. UpdateControls;
  177. end;
  178. procedure TfrMain.BrookHTTPServer1Stop(Sender: TObject);
  179. begin
  180. UpdateControls;
  181. end;
  182. procedure TfrMain.edPortChange(Sender: TObject);
  183. begin
  184. UpdateControls;
  185. end;
  186. procedure TfrMain.edPortChangeTracking(Sender: TObject);
  187. begin
  188. UpdateControls;
  189. end;
  190. end.