Recent

Author Topic: Embedded webserver and httprourer  (Read 461 times)

ldvmikro

  • Newbie
  • Posts: 2
Embedded webserver and httprourer
« on: August 25, 2020, 09:53:13 am »
Hello,
I use embedded webserver based on fpweb unit an in the current stage I need routing of different requests. I tried to use Route Syntax described her https://wiki.lazarus.freepascal.org/fpWeb_Tutorial, but no luck, its not working with embedded server, only with simple http server realization such as this:

Code: Pascal  [Select][+][-]
  1.   HTTPRouter.RegisterRoute('*', @DoHello);
  2.   Application.Port := 9000;
  3.   Application.Initialize;
  4.   Application.Run;


This is my code that describe and starting my webserver
Code: Pascal  [Select][+][-]
  1. type
  2.   THTTPServerThread = class(TThread)
  3.   strict private
  4.     FServer: TFPHTTPServer;
  5.     FRequest: TFPHTTPConnectionRequest;
  6.     FResponse: TFPHTTPConnectionResponse;
  7.     procedure DoHandleRequest(Sender: TObject;
  8.       var ARequest: TFPHTTPConnectionRequest;
  9.       var AResponse: TFPHTTPConnectionResponse);
  10.     procedure ShowRequest;
  11.   protected
  12.     procedure Execute; override;
  13.   public
  14.     constructor Create(APort: Word);
  15.     destructor Destroy; override;
  16.     procedure StopServer;
  17.   end;    
  18.  
  19. { THTTPServerThread }
  20. constructor THTTPServerThread.Create(APort: Word);
  21. begin
  22.   inherited Create(True);
  23.   FServer := TFPHttpServer.Create(nil);
  24.   FServer.Port := APort;
  25.   FServer.OnRequest := @DoHandleRequest;
  26.   FreeOnTerminate := True;
  27. end;
  28.  
  29. destructor THTTPServerThread.Destroy;
  30. begin
  31.   FServer.Free;
  32.   inherited;
  33. end;  
  34.  
  35. procedure THTTPServerThread.Execute;
  36. begin
  37.      FServer.Active := True;
  38. end;  
  39.  
  40. procedure THTTPServerThread.StopServer;
  41. begin
  42.   FServer.Active := False;
  43. end;  
  44.  
  45.   FThread := THTTPServerThread.Create(9080);
  46.   FThread.OnTerminate := @ThreadTerminated;  
  47.  
  48.  

Please help, when should put  HTTPRouter.RegisterRoute line for requests routing?
Thanks

PierceNg

  • New Member
  • *
  • Posts: 25
Re: Embedded webserver and httprourer
« Reply #1 on: August 25, 2020, 05:50:56 pm »
Embedded server = simple HTTP server, no? The other options mentioned on that wiki page are CGI, FastCGI, Apache module, libmicrohttpd and Windows kernel HTTP server.

Here's a multi threaded example with 2 routes using new-style routing.

Code: Pascal  [Select][+][-]
  1. program tworoutes;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. uses
  6.   {$ifdef UNIX}cthreads, cmem,{$endif} fphttpapp, httpdefs, httproute;
  7.  
  8. procedure route1(aReq: TRequest; aResp: TResponse);
  9. begin
  10.   aResp.content:='<html><body><h1>Route 1 The Default</h1></body></html>'
  11. end;
  12.  
  13. procedure route2(aReq: TRequest; aResp: TResponse);
  14. begin
  15.   aResp.content:='<html><body><h1>Route 2</h1></body></html>'
  16. end;
  17.  
  18. begin
  19.   HTTPRouter.registerRoute('/', @route1, true);
  20.   HTTPRouter.registerRoute('/route2', @route2)
  21.   Application.port := 8080;
  22.   Application.threaded := true;
  23.   Application.initialize;
  24.   Application.run;
  25. end.
  26.  

Leledumbo

  • Hero Member
  • *****
  • Posts: 8266
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: Embedded webserver and httprourer
« Reply #2 on: August 26, 2020, 09:14:30 pm »
My tutorial never mentions direct use of TFPHTTPServer, only through fphttpapp (that utilizes TFPHTTPServer behind the scene). Please read this answer.

ldvmikro

  • Newbie
  • Posts: 2
Re: Embedded webserver and httprourer
« Reply #3 on: August 28, 2020, 11:21:35 pm »
Many thanks, Leledumbo. Now I understand the problem much better.

 

TinyPortal © 2005-2018