#1 2025-09-22 14:57:52

g_garzotto
Member
Registered: 2023-09-07
Posts: 8

Custom routing on method based service

Hello,

I have a method based service that expose a series of method.
The constructor of service create a model that define an api route:

constructor TSSPOSRestServer.Create(APort: Integer);
begin
  FPort := IntToStr(APort);

  FModel := TSQLModel.Create([], 'api');
  FService := TSSPOSRESTProcessor.Create(FModel);
  FServer := TSQLHttpServer.Create(FPort, [FService], '+', useHttpApiRegisteringURI);
  FServer.AccessControlAllowOrigin := '*';
end;

so the methods are available at URL:

http://ip:port/api/mymethod

There is a simple way to add another model to the current server, in order to have some methods that use another routing ? For example:

http://ip:port/orders/myothermethod

Or, there is a way to define custom routing for method based services ? I read about TSQLRestServerURIContext class, but I can't figure out how to use it.

Thanks in advice

Offline

#2 2025-09-22 16:39:47

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 15,224
Website

Re: Custom routing on method based service

Define routes at HTTP server level.

https://blog.synopse.info/?post/2022/12 … -Christmas

Offline

#3 2025-09-24 11:07:04

g_garzotto
Member
Registered: 2023-09-07
Posts: 8

Re: Custom routing on method based service

ab wrote:

Sorry for the delay on response.

I read the article, but I can't find a Route method on TSQLHttpServer class.

BTW, I'm still using mORMot 1, maybe I should use another approach ?

Thanks again

Offline

#4 2025-09-24 12:10:41

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 15,224
Website

Re: Custom routing on method based service

Oh I did not get it was for mORMot 1.
In this version, there is no router engine available to rewrite the routes.

My guess is that you could parse and rewrite the URI by using the THttpServerGeneric.OnBeforeRequest callback.

Offline

#5 2025-09-24 13:05:35

g_garzotto
Member
Registered: 2023-09-07
Posts: 8

Re: Custom routing on method based service

Can you provide a quick example ?

I'm unfamiliar with URL rewriting, and I didn't find an example that show how to do id.

Many thanks

Offline

#6 2025-09-24 13:13:55

ttomas
Member
Registered: 2013-03-08
Posts: 145

Re: Custom routing on method based service

g_garzotto wrote:

There is a simple way to add another model to the current server, in order to have some methods that use another routing ? For example:
http://ip:port/orders/myothermethod

Do you need another model in context of ORM or just another root uri for method based service?
If you need just another root, you can create TSQLRestServer inherited from TSQLRestServerFullMemory
TSQLRestServerOrders = class(TSQLRestServerFullMemory)
with constructor like

constructor TSQLRestServerOrders.Create(const aRoot: RawUTF8);
begin
  inherited CreateWithOwnModel([], false, aRoot);
end;
//... and change your code to
  FModel := TSQLModel.Create([], 'api');
  FService := TSSPOSRESTProcessor.Create(FModel);
  FOrders := TSQLRestServerOrders.Create('orders');
  FServer := TSQLHttpServer.Create(FPort, [FService, FOrders], '+', useHttpApiRegisteringURI);

Offline

#7 2025-09-24 13:23:45

g_garzotto
Member
Registered: 2023-09-07
Posts: 8

Re: Custom routing on method based service

For my scenario I think another root uri for method based service is the best choice.

What I would like to obtain, if possible, is to have some routes like (for example):

http://ip:port/api/checkout
http://ip:port/api/print
http://ip:port/api/coupons/claim
http://ip:port/api/coupons/release

and all methods (Checkout, Print, ClaimCoupon, ReleaseCoupon) should belongs to the same TSSPOSRESTProcessor class:

  TSSPOSRESTProcessor = class(TSQLRestServerFullMemory)
  private
    ...
  published
    function Checkout(AContext: TSQLRestServerURIContext): RawJSON;
    function Print(AContext: TSQLRestServerURIContext): RawJSON;
    function ClaimCoupon(AContext: TSQLRestServerURIContext): RawJSON;
    function ReleaseCoupon(AContext: TSQLRestServerURIContext): RawJSON;
  end;

I know that it's not the best choice, but I found most of the code already written, and I can't actually rewrite it in a better way.

Offline

#8 2025-09-24 14:22:39

ttomas
Member
Registered: 2013-03-08
Posts: 145

Re: Custom routing on method based service

In that case, best move your code to mormot2 and use new router, as @ab point.

Offline

#9 2025-09-25 07:45:13

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 15,224
Website

Re: Custom routing on method based service

Or just use 'api' as model Root, and 3 methods: Checkout/Print/Coupons

Then in the Coupons() method, split the URI into 'claim' and 'release'.

Offline

Board footer

Powered by FluxBB