Blog
All Blog Posts | Next Post | Previous PostTMS XData REST Server on Linux - Step by Step - Part 5
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 Landgraf
This blog post has received 5 comments.
2. Monday, June 26, 2017 at 3:23:55 PM
Thank you John!
Wagner R. Landgraf
3. Wednesday, August 15, 2018 at 2:20:44 AM
Wagner, thank you very-very much for the detailed and compact tutorial! This will make use of TMS WEB (and XDATA) very easy! Thanks!
Yuri Yolkin
4. Friday, September 27, 2024 at 11:57:16 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
5. Tuesday, October 8, 2024 at 8:58:25 PM
Answered in part 4 blog post.
Wagner Landgraf
All Blog Posts | Next Post | Previous Post
Thanks!!!
Kouraklis John