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

Bookmarks: 

Thursday, May 25, 2017

This is part 5 of the tutorial on how to create TMS XData and TMS Sparkle servers on Linux. On Part 4, we have added Sparkle to our Apache module, preparing it to receive any modules we want to add. And in this final part we will add the TMS XData module to it.

Here is video for part 5: Adding the TMS XData module to Apache.



Now that we can add any Sparkle module we want, develop our XData server should be nothing different from when we use Windows. You can find more detailed information about it in other resources (blog posts, our TMS XData documentation, etc.). This tutorial will just cover the basics and deal with some final Linux specific things, like SQLite database permissions.

1. Create a new Aurelius connection for SQLite database

Go to Delphi menu item "File, New, Other...", then "Delphi Projects, TMS Business, TMS Aurelius DB Connection" to open the wizard. Choose "SQLite" for both driver and SQL dialect, and then confirm to create the new connection. Unit name created will be called "ConnectionModule".

2. Add XData Module to Sparkle server

Going back to WebModule1 unit, add the following units to the uses clause:

uses
   {...}, XData.Server.Module, ConnectionModule, Aurelius.Engine.DatabaseManager;

Now remove the Sparkle anonymous module we have added just for testing in previous part 4 of this tutorial, and add the new XData module. Also, let's do a call to TDatabaseManager.Update so our tables and columns are created in the database if needed. The final code of initialization/finalization part of our unit should be like this:

initialization
  Server := TWebBrokerServer.Create;
  TDatabaseManager.Update(TSQLiteSQLiteConnection.CreateConnection);
  Server.Dispatcher.AddModule(TXDataServerModule.Create(
    'http://ubuntu/tms', TSQLiteSQLiteConnection.CreateConnection
  ));
finalization
  Server.Free;
end.

3. Define SQLite database location

The two steps above should be enough to get our XData server up and running (yep, only those). But our server connects to a database, and we have chosen to use SQLite. We must then define the location of our SQLite database, and specifically on Linux, we should create and set permissions on it so Apache module can read and write to the database.

Let's go back to the ConnectionModule unit created by the wizard, and change the location of SQLite database by changing the parameter in the TSQLiteSQLiteConnection.CreateConnection function:

class function TSQLiteSQLiteConnection.CreateConnection: IDBConnection;
begin
  Result := TSQLiteNativeConnectionAdapter.Create('/home/user/xdata/xdata.db');
end;

In our example, the SQLite database will be the xdata/xdata.db file in our home folder. You might need to change that location a little bit if you have a different user name on Linux.

4. Create SQLite database on Linux and give proper permissions

Now let's go back to Linux terminal and create the database. Make sure you are in your home directory (you can use "cd" command for that):

cd
mkdir xdata
touch xdata/xdata.db

Now, we need to give Apache access to our database. On Ubuntu, Apache module runs under "www-data" group. This will work here if you are following this tutorial from the very beginning, but it can vary depending on the Linux flavor you are using or other Apache settings. In our case, we will change the group of our xdata folder and all its content to "www-data":

sudo chgrp www-data xdata -R


5. Install SQLite library

Our XData module in Apache will use SQLite to access the database. We must then install SQLite libraries for it to do that. If we were using any other database, like MySQL, PostgreSQL, etc., we would might need to install proper client libraries for them as well. Use the following command to install SQLite library:

sudo apt-get install libsqlite3-dev


6. Rebuild, deploy and restart Apache

All is done. We just have to rebuild our Apache module, deploy it again on Linux, and restart Apache on Linux like we did before in previous parts of this tutorial:

sudo apache2ctl stop
sudo apache2ctl start

And our XData server should be running at root address "http://ubuntu/tms"!

This is the end of this tutorial, I hope it was useful for you. From now on, developing further the XData server is no different than developing for Windows. The video above shows more steps further, like creating an entity in the server and doing some CRUD operations on it.

And of course, for further information on TMS XData, please refer to the XData web page or XData documentation.

Wagner R. Landgraf


Bookmarks: 

This blog post has received 2 comments.


1. Monday, June 26, 2017 at 3:17:58 PM

Very helpful videos.

Thanks!!!

Kouraklis John


2. Monday, June 26, 2017 at 3:23:55 PM

Thank you John!

Wagner R. Landgraf




Add a new comment:
Author:
Email:
  You will receive a confirmation mail with a link to validate your comment, so please use a valid email address.
Comment:
 
Change Image
Fill in the characters from the image above:
 

All fields are required.
 




Previous  |  Next  |  Index