Using TCustomHTTPApplication in GUI Application lazarus / 38-Networking and Web Programming ---------------------------------------------------------------------- d.ioannidis · 2023-09-11 08:30 Hi,   I wanted to see if TCustomHTTPApplication can be used in GUI Application's and the following code is working .  As I'm not too familiar with the LCL, can someone review the code and see if there is a catch or bad practice ? EDIT: Free Pascal 3.2.2 / Lazarus 2.2.6 TCustomHTTPApplication  in a Thread Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i unit uHTTPSrv; {$mode ObjFPC}{$H+} interface uses  Classes, SysUtils, custhttpapp; type   { TWebServerThread }   TWebServerThread = class(TThread)  private    FHTTPApp: TCustomHTTPApplication;  protected    procedure Execute; override;  public    constructor Create(APort: word);    destructor Destroy; override;    procedure TerminateHTTPApp;  end; implementation { TWebServerThread } procedure TWebServerThread.Execute;begin  FHTTPApp.Run;  while not (Terminated and FHTTPApp.Terminated) do Sleep(1);end; constructor TWebServerThread.Create(APort: word);begin  FHTTPApp := TCustomHTTPApplication.Create(nil);  FHTTPApp.Port := (APort);  FHTTPApp.Threaded := True;  FHTTPApp.Initialize;  inherited Create(False);end; destructor TWebServerThread.Destroy;begin  FreeAndNil(FHTTPApp);  inherited Destroy;end; procedure TWebServerThread.TerminateHTTPApp;begin  if Assigned(FHTTPApp) then  FHTTPApp.Terminate;end; end. Managing TCustomHTTPApplication  from main form thread. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i uses   ........, uHTTPSrv,  HTTPDefs, httproute;     { TfrmAppMain }   TfrmAppMain = class(TForm)    .........    procedure FormCreate(Sender: TObject);    .........  private    FWebServer: TWebServerThread;    procedure StartHTTPApiServer;    procedure StopHTTPApiServer;  public procedure TfrmAppMain.FormCreate(Sender: TObject);begin   HTTPRouter.RegisterRoute('/', TDefaultRoute, True);  HTTPRouter.RegisterRoute('/echo/:echo', TEchoRoute); end; procedure TfrmAppMain.StartHTTPApiServer;begin  if not Assigned(FWebServer) then    FWebServer := TWebServerThread.Create(22100);end; procedure TfrmAppMain.StopHTTPApiServer;begin  if Assigned(FWebServer) then  begin    FWebServer.TerminateHTTPApp;    FWebServer.Terminate;    FWebServer.WaitFor();    FreeAndNil(FWebServer);  end;end;   The routes Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i   { TDefaultRoute }   TDefaultRoute = class(TRouteObject)    procedure HandleRequest(ARequest: TRequest; AResponse: TResponse); override;  end;   { TEchoRoute }   TEchoRoute = class(TRouteObject)    procedure HandleRequest(ARequest: TRequest; AResponse: TResponse); override;  end; { TDefaultRoute } procedure TDefaultRoute.HandleRequest(ARequest: TRequest; AResponse: TResponse);begin  if ARequest.URI <> '/' then  begin    AResponse.Code := 404;    AResponse.CodeText := 'Not Found';  end  else  begin    AResponse.Content := ApplicationName;    AResponse.Code := 200;    AResponse.CodeText := 'OK';    AResponse.ContentLength := Length(AResponse.Content);  end;end; { TEchoRoute } procedure TEchoRoute.HandleRequest(ARequest: TRequest; AResponse: TResponse);begin  AResponse.Content := ARequest.RouteParams['echo'];  AResponse.Code := 200;  AResponse.CodeText := 'OK';  AResponse.ContentLength := Length(AResponse.Content);end;  regards, ---------------------------------------------------------------------- Leledumbo · 2023-09-12 09:04 Quote from: d.ioannidis on September 11, 2023, 10:30:53 amAs I'm not too familiar with the LCL, can someone review the code and see if there is a catch or bad practice ? Don't use the CustomXXX classes directly, they're meant to be extended. That's what THTTPApplication is for if you want something out of the box. Apart from that, you're creating the thread UNsuspended, meaning it will directly call Execute after creation. Just be aware and ensure route registration is executed before calling FHTTPApp.Run as I don't think you can register routes on the fly, they might be cached.