urlrouter.dpr 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. program urlrouter;
  26. {$IFDEF MSWINDOWS}
  27. {$APPTYPE CONSOLE}
  28. {$ENDIF}
  29. uses
  30. SysUtils,
  31. Classes,
  32. BrookUtility,
  33. BrookHTTPRequest,
  34. BrookHTTPResponse,
  35. BrookHTTPServer,
  36. BrookURLRouter;
  37. type
  38. { TRouteHome }
  39. TRouteHome = class(TBrookURLRoute)
  40. protected
  41. procedure DoRequest(ASender: TObject; ARoute: TBrookURLRoute;
  42. ARequest: TBrookHTTPRequest; AResponse: TBrookHTTPResponse); override;
  43. public
  44. procedure AfterConstruction; override;
  45. end;
  46. { TRouteDownload }
  47. TRouteDownload = class(TBrookURLRoute)
  48. protected
  49. procedure DoRequest(ASender: TObject; ARoute: TBrookURLRoute;
  50. ARequest: TBrookHTTPRequest; AResponse: TBrookHTTPResponse); override;
  51. public
  52. procedure AfterConstruction; override;
  53. end;
  54. { TRoutePage }
  55. TRoutePage = class(TBrookURLRoute)
  56. protected
  57. procedure DoRequest(ASender: TObject; ARoute: TBrookURLRoute;
  58. ARequest: TBrookHTTPRequest; AResponse: TBrookHTTPResponse); override;
  59. public
  60. procedure AfterConstruction; override;
  61. end;
  62. { TRouter }
  63. TRouter = class(TBrookURLRouter)
  64. protected
  65. procedure DoNotFound(ASender: TObject; const ARoute: string;
  66. ARequest: TBrookHTTPRequest; AResponse: TBrookHTTPResponse); override;
  67. end;
  68. { TServer }
  69. TServer = class(TBrookHTTPServer)
  70. private
  71. FRouter: TRouter;
  72. protected
  73. procedure DoRequest(ASender: TObject; ARequest: TBrookHTTPRequest;
  74. AResponse: TBrookHTTPResponse); override;
  75. public
  76. constructor Create(AOwner: TComponent); override;
  77. end;
  78. { TRouteHome }
  79. procedure TRouteHome.AfterConstruction;
  80. begin
  81. Methods := [rmGET];
  82. Pattern := '/home';
  83. Default := True;
  84. end;
  85. procedure TRouteHome.DoRequest(ASender: TObject; ARoute: TBrookURLRoute;
  86. ARequest: TBrookHTTPRequest; AResponse: TBrookHTTPResponse);
  87. begin
  88. AResponse.Send('Home page', 'text/plain', 200);
  89. end;
  90. { TRouteDownload }
  91. procedure TRouteDownload.AfterConstruction;
  92. begin
  93. Methods := [rmGET];
  94. Pattern := '/download/(?P<file>[a-z]+)';
  95. end;
  96. procedure TRouteDownload.DoRequest(ASender: TObject; ARoute: TBrookURLRoute;
  97. ARequest: TBrookHTTPRequest; AResponse: TBrookHTTPResponse);
  98. begin
  99. AResponse.SendFmt('Downloaded file: %s',
  100. [ARoute.Variables['file']], 'text/plain', 200);
  101. end;
  102. { TRoutePage }
  103. procedure TRoutePage.AfterConstruction;
  104. begin
  105. Methods := [rmGET];
  106. Pattern := '/page/([0-9]+)';
  107. end;
  108. procedure TRoutePage.DoRequest(ASender: TObject; ARoute: TBrookURLRoute;
  109. ARequest: TBrookHTTPRequest; AResponse: TBrookHTTPResponse);
  110. begin
  111. AResponse.SendFmt('Page number: %d', [ARoute.Segments[0].ToInteger],
  112. 'text/plain', 200);
  113. end;
  114. { TRouter }
  115. procedure TRouter.DoNotFound(ASender: TObject; const ARoute: string;
  116. ARequest: TBrookHTTPRequest; AResponse: TBrookHTTPResponse);
  117. begin
  118. AResponse.Send('Page not found', 'text/plain', 404);
  119. end;
  120. { TServer }
  121. constructor TServer.Create(AOwner: TComponent);
  122. begin
  123. inherited Create(AOwner);
  124. FRouter := TRouter.Create(Self);
  125. TRouteHome.Create(FRouter.Routes);
  126. TRouteDownload.Create(FRouter.Routes);
  127. TRoutePage.Create(FRouter.Routes);
  128. FRouter.Active := True;
  129. end;
  130. procedure TServer.DoRequest(ASender: TObject; ARequest: TBrookHTTPRequest;
  131. AResponse: TBrookHTTPResponse);
  132. begin
  133. FRouter.Route(ASender, ARequest, AResponse);
  134. end;
  135. begin
  136. with TServer.Create(nil) do
  137. try
  138. Open;
  139. if not Active then
  140. Exit;
  141. WriteLn('Server running at http://localhost:', Port);
  142. ReadLn;
  143. finally
  144. Free;
  145. end;
  146. end.