Recent

Author Topic: [SOLVED]fpweb documentation do not cover middleware setup  (Read 3827 times)

Sniper

  • New Member
  • *
  • Posts: 42
[SOLVED]fpweb documentation do not cover middleware setup
« on: December 13, 2024, 09:02:16 pm »
Hello y'all.
I haven't found how to create middleware using internal web server in this article https://wiki.freepascal.org/fpWeb_Tutorial.

Code: Pascal  [Select][+][-]
  1. program webserver;
  2.    
  3. {$mode objfpc}{$H+}
  4.  
  5. uses
  6. {$ifdef UNIX}
  7.    cthreads, cmem,
  8. {$endif}
  9.    fphttpapp, httpdefs, httproute;
  10.  
  11. procedure route1(aReq: TRequest; aResp: TResponse);
  12. begin
  13.    aResp.content:='Hello, World!'
  14. end;
  15.  
  16. begin
  17.    HTTPRouter.registerRoute('/', @route1, true);
  18.    Application.port := 8080;
  19.    Application.threaded := true;
  20.    Application.initialize;
  21.    Application.run;
  22.  end.
  23.  
Could anyone point me where I can see any bits of this type of information?
« Last Edit: December 28, 2024, 01:22:41 pm by Sniper »

Leledumbo

  • Hero Member
  • *****
  • Posts: 8783
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: fpweb documentation do not cover middleware setup
« Reply #1 on: December 18, 2024, 01:36:50 pm »
I haven't found how to create middleware using internal web server in this article https://wiki.freepascal.org/fpWeb_Tutorial.
...
Could anyone point me where I can see any bits of this type of information?
What kind of middleware do you expect? The tutorial doesn't cover any of the fpWeb components you get in Lazarus Component Palette on purpose, because... I simply don't know how to work with them. And while the previous router have some injectable events, the more recent httproute router is much simpler and is exactly how you code it: It only links URIs to procedures, everything else should be made on top of that.

Sniper

  • New Member
  • *
  • Posts: 42
Re: fpweb documentation do not cover middleware setup
« Reply #2 on: December 25, 2024, 05:43:13 pm »
I haven't found how to create middleware using internal web server in this article https://wiki.freepascal.org/fpWeb_Tutorial.
...
Could anyone point me where I can see any bits of this type of information?
What kind of middleware do you expect? The tutorial doesn't cover any of the fpWeb components you get in Lazarus Component Palette on purpose, because... I simply don't know how to work with them. And while the previous router have some injectable events, the more recent httproute router is much simpler and is exactly how you code it: It only links URIs to procedures, everything else should be made on top of that.

"Middleware" I'm talking about is a function where I can check something in a query and the decide what to do next with it accept or decline fo example.
The ordinary modern request track looks like: "/route1" -> middleware1_function(write query log)->middlware2_function(check user credentials) -> route1_function
in pascal fpweb framework it might look like this HTTPRouter.RegisterRoute('/route1', @middleware1_function(@middleware2_function(@route1)));
the design patter for this type is decorator

PierceNg

  • Sr. Member
  • ****
  • Posts: 398
    • SamadhiWeb
Re: fpweb documentation do not cover middleware setup
« Reply #3 on: December 26, 2024, 01:33:28 am »
"Middleware" I'm talking about is a function where I can check something in a query and the decide what to do next with it accept or decline fo example.
The ordinary modern request track looks like: "/route1" -> middleware1_function(write query log)->middlware2_function(check user credentials) -> route1_function
in pascal fpweb framework it might look like this HTTPRouter.RegisterRoute('/route1', @middleware1_function(@middleware2_function(@route1)));
the design patter for this type is decorator

These are called interceptors in fcl-web. Not in FPC 3.2.2, only in trunk. Here's trunk's demo. The relevant code:

Code: Pascal  [Select][+][-]
  1. if Fauth<>'' then
  2.     HTTPRouter.RegisterInterceptor('auth',@DoAuthorization);
  3.   if not FQuiet then
  4.     begin
  5.     HTTPRouter.RegisterInterceptor('logstart',@DoRequestStart);
  6.     HTTPRouter.RegisterInterceptor('logend',@DoRequestEnd,iaAfter);
  7.     end;

Leledumbo

  • Hero Member
  • *****
  • Posts: 8783
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: fpweb documentation do not cover middleware setup
« Reply #4 on: December 26, 2024, 11:07:07 am »
These are called interceptors in fcl-web. Not in FPC 3.2.2, only in trunk.
Ah, new feature that escapes my watch. I assume they will be executed in order of RegisterInterceptor call, yes?

Sniper

  • New Member
  • *
  • Posts: 42
Re: fpweb documentation do not cover middleware setup
« Reply #5 on: December 28, 2024, 01:22:28 pm »
These are called interceptors in fcl-web. Not in FPC 3.2.2, only in trunk. Here's trunk's demo. The relevant code:

Code: Pascal  [Select][+][-]
  1. if Fauth<>'' then
  2.     HTTPRouter.RegisterInterceptor('auth',@DoAuthorization);
  3.   if not FQuiet then
  4.     begin
  5.     HTTPRouter.RegisterInterceptor('logstart',@DoRequestStart);
  6.     HTTPRouter.RegisterInterceptor('logend',@DoRequestEnd,iaAfter);
  7.     end;
That is what I'm looking for. Thank you!

PierceNg

  • Sr. Member
  • ****
  • Posts: 398
    • SamadhiWeb
Re: [SOLVED]fpweb documentation do not cover middleware setup
« Reply #6 on: December 29, 2024, 02:31:16 am »
Ah, new feature that escapes my watch. I assume they will be executed in order of RegisterInterceptor call, yes?

Haven't looked thru the source. I am guessing for requests the interceptors are invoked first-added first-to-process, while for responses they are first-added last-to-process, typical of this middleware pattern.

 

TinyPortal © 2005-2018