URLRouter_frMain.pas 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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. {$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. BrookURLRouter;
  42. type
  43. TfrMain = class(TForm)
  44. acStart: TAction;
  45. acStop: TAction;
  46. alMain: TActionList;
  47. BrookURLRouter1: TBrookURLRouter;
  48. BrookHTTPServer1: TBrookHTTPServer;
  49. btStart: TButton;
  50. btStop: TButton;
  51. edPort: TSpinEdit;
  52. lbLink: TLabel;
  53. lbPort: TLabel;
  54. procedure acStartExecute(Sender: TObject);
  55. procedure acStopExecute(Sender: TObject);
  56. procedure BrookURLRouter1NotFound(ASender: TObject; const ARoute: string;
  57. ARequest: TBrookHTTPRequest; AResponse: TBrookHTTPResponse);
  58. procedure BrookURLRouter1Routes0Request(ASender: TObject;
  59. ARoute: TBrookURLRoute; ARequest: TBrookHTTPRequest;
  60. AResponse: TBrookHTTPResponse);
  61. procedure BrookURLRouter1Routes1Request(ASender: TObject;
  62. ARoute: TBrookURLRoute; ARequest: TBrookHTTPRequest;
  63. AResponse: TBrookHTTPResponse);
  64. procedure BrookURLRouter1Routes2Request(ASender: TObject;
  65. ARoute: TBrookURLRoute; ARequest: TBrookHTTPRequest;
  66. AResponse: TBrookHTTPResponse);
  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 BrookHTTPServer1Start(Sender: TObject);
  73. procedure BrookHTTPServer1Stop(Sender: TObject);
  74. procedure edPortChange(Sender: TObject);
  75. procedure lbLinkClick(Sender: TObject);
  76. procedure lbLinkMouseEnter(Sender: TObject);
  77. procedure lbLinkMouseLeave(Sender: TObject);
  78. public
  79. procedure UpdateControls; {$IFNDEF DEBUG}inline;{$ENDIF}
  80. end;
  81. var
  82. frMain: TfrMain;
  83. implementation
  84. {$R *.lfm}
  85. procedure TfrMain.UpdateControls;
  86. begin
  87. if BrookHTTPServer1.Active then
  88. edPort.Value := BrookHTTPServer1.Port
  89. else
  90. BrookHTTPServer1.Port := edPort.Value;
  91. lbLink.Caption := Concat('http://localhost:', edPort.Value.ToString);
  92. acStart.Enabled := not BrookHTTPServer1.Active;
  93. acStop.Enabled := not acStart.Enabled;
  94. edPort.Enabled := acStart.Enabled;
  95. lbLink.Enabled := not acStart.Enabled;
  96. end;
  97. procedure TfrMain.acStartExecute(Sender: TObject);
  98. begin
  99. BrookURLRouter1.Open;
  100. BrookHTTPServer1.Open;
  101. end;
  102. procedure TfrMain.acStopExecute(Sender: TObject);
  103. begin
  104. BrookHTTPServer1.Close;
  105. end;
  106. procedure TfrMain.lbLinkMouseEnter(Sender: TObject);
  107. begin
  108. lbLink.Font.Style := lbLink.Font.Style + [fsUnderline];
  109. end;
  110. procedure TfrMain.lbLinkMouseLeave(Sender: TObject);
  111. begin
  112. lbLink.Font.Style := lbLink.Font.Style - [fsUnderline];
  113. end;
  114. procedure TfrMain.lbLinkClick(Sender: TObject);
  115. begin
  116. OpenURL(lbLink.Caption);
  117. end;
  118. procedure TfrMain.BrookHTTPServer1Request(ASender: TObject;
  119. ARequest: TBrookHTTPRequest; AResponse: TBrookHTTPResponse);
  120. begin
  121. BrookURLRouter1.Route(ASender, ARequest, AResponse);
  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.BrookURLRouter1NotFound(ASender: TObject;
  132. const ARoute: string; ARequest: TBrookHTTPRequest;
  133. AResponse: TBrookHTTPResponse);
  134. begin
  135. AResponse.SendFmt(
  136. '<html><head><title>Not found</title></head><body>Page not found: %s</body></html>',
  137. [ARequest.Path], 'text/html; charset=utf-8', 404);
  138. end;
  139. procedure TfrMain.BrookURLRouter1Routes0Request(ASender: TObject;
  140. ARoute: TBrookURLRoute; ARequest: TBrookHTTPRequest;
  141. AResponse: TBrookHTTPResponse);
  142. begin
  143. AResponse.Send(
  144. '<html><head><title>Home page</title></head><body>Home page</body></html>',
  145. 'text/html; charset=utf-8', 200);
  146. end;
  147. procedure TfrMain.BrookURLRouter1Routes1Request(ASender: TObject;
  148. ARoute: TBrookURLRoute; ARequest: TBrookHTTPRequest;
  149. AResponse: TBrookHTTPResponse);
  150. begin
  151. AResponse.SendFmt(
  152. '<html><head><title>Downloads</title></head><body>Downloaded file: %s</body></html>',
  153. [ARoute.Variables['file']], 'text/html; charset=utf-8', 200);
  154. end;
  155. procedure TfrMain.BrookURLRouter1Routes2Request(ASender: TObject;
  156. ARoute: TBrookURLRoute; ARequest: TBrookHTTPRequest;
  157. AResponse: TBrookHTTPResponse);
  158. begin
  159. AResponse.SendFmt(
  160. '<html><head><title>Page</title></head><body>Page number: %d</body></html>',
  161. [ARoute.Segments[0].ToInteger], 'text/html; charset=utf-8', 200);
  162. end;
  163. procedure TfrMain.BrookHTTPServer1Start(Sender: TObject);
  164. begin
  165. UpdateControls;
  166. end;
  167. procedure TfrMain.BrookHTTPServer1Stop(Sender: TObject);
  168. begin
  169. UpdateControls;
  170. end;
  171. procedure TfrMain.edPortChange(Sender: TObject);
  172. begin
  173. UpdateControls;
  174. end;
  175. end.