Blog

All Blog Posts  |  Next Post  |  Previous Post

TMS XData REST Server on Linux - Step by Step - Part 4

Wednesday, May 24, 2017

This is part 4 of the tutorial on how to create TMS XData and TMS Sparkle servers on Linux. On Part 3, we have created and deployed our first Apache Module using WebBroker. Now we're going to add TMS Sparkle to it.

Here is video for part 4: Adding TMS Sparkle and using Sparkle modules in the Apache module.



Basically, what we need to do is "wrap" the WebBroker request/response objects into a Sparkle request/response objects. So any Sparkle module can process the requests and provide responses. This part is also described in the documentation, in this page: http://www.tmssoftware.biz/business/sparkle/doc/web/apache-based-server.html

1. In WebModuleUnit1 unit, add the units Sparkle.WebBroker.Server and Sparkle.WebBroker.Adapter to the uses clause:

uses {...},
  Sparkle.WebBroker.Server,
  Sparkle.WebBroker.Adapter,
   // just for the anonymous module example
  Sparkle.HttpServer.Module, Sparkle.HttpServer.Context;


2. Declare and create a global TWebBrokerServer instance:

Add a global variable declaration in the unit:

var
  Server: TWebBrokerServer;

And then create it in initialization:

initialization
  Server := TWebBrokerServer.Create;
finalization
  Server.Free;
end.


3. Replace the WebModule1DefaultHandlerAction event handler with the code below to dispatch the requests:

procedure TWebModule1.WebModule1DefaultHandlerAction(Sender: TObject;
  Request: TWebRequest; Response: TWebResponse; var Handled: Boolean);
var
  Adapter: IWebBrokerAdapter;
begin
  Adapter := TWebBrokerAdapter.Create(Request, Response);
  Server.DispatchRequest(Adapter);
end;

And this should be enough to have TMS Sparkle "plugged" into the Apache module. From now on, you can code just like you do on Windows: create Sparkle modules (like XData), and add them to the server Dispatcher. As a quick example, you could add the following anonymous module to test:
initialization
  Server := TWebBrokerServer.Create;
  // add modules you want to use. This example assumes a simple Sparkle module,
  // but you can add your XData, RemoteDB or any other Sparkle module
  Server.Dispatcher.AddModule(TAnonymousServerModule.Create(
    'http://localhost/tms/hello',
    procedure(const C: THttpServerContext)
    begin
      C.Response.StatusCode := 200;
      C.Response.ContentType := 'text/plain'';
      C.Response.Close(TEncoding.UTF8.GetBytes('Hello from Sparkle!'));
    end
  ));
finalization
  Server.Free;
end.

Now you can rebuild the module and deploy to Linux. If you now go to Linux and restart Apache:

sudo apache2ctl stop
sudo apache2ctl start

And refresh the URL in your browser:

http://ubuntu/tms

You should see a response "Hello from Sparkle!"

In the next part (the final one) we will add the XData module to our Apache module.

Wagner Landgraf




This blog post has received 2 comments.


1. Friday, September 27, 2024 at 10:06:35 AM

Under Apache, the protocol, server name and URI would be configured in its .conf files and the module may be loaded under different server names and/or paths, how would you create TXdataServerModule then? - if it''s done in the initialization, it would happen before you even have a chance to read any config files. Can it be created later, in the WebModuleCreate? Can the URL parameter of TXdataServerModule.Create be omitted entirely, or contain just the URI path? It would not be practical to hard-code any of that, nor even remember what the server name or protocol or URI are, because they can all change between Apache restarts.

Alexander Pastuhov


2. Tuesday, October 8, 2024 at 8:57:53 PM

You can create it at any time as long of course it''s already created when a request is processed. XData uses the base URL parameter to know how to process the "sub path" URL, i.e., it needs to know at least the base URL of the server so it knows the relative paths. For more details, please create a request in our support center.

Wagner Landgraf




Add a new comment

You will receive a confirmation mail with a link to validate your comment, please use a valid email address.
All fields are required.



All Blog Posts  |  Next Post  |  Previous Post