You are not logged in.
Pages: 1
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
Define routes at HTTP server level.
Offline
Define routes at HTTP server level.
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
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
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
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
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
In that case, best move your code to mormot2 and use new router, as @ab point.
Offline
Pages: 1