urlrouter.lpr 4.4 KB

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