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. Add a comment.



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

Bookmarks: 

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 R. Landgraf


Bookmarks: 

This blog post has not received any comments yet. Add a comment.



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

Bookmarks: 

Tuesday, May 23, 2017

This is part 3 of the tutorial on how to create TMS XData and TMS Sparkle servers on Linux. On Part 2, we have installed PAServer and launched our first "Hello, World" console application on Linux.

Here is video for part 3: Create and deploy an Apache module to Linux.



We will use WebBroker to create an empty Apache module. But first we need to install Apache on Linux.

1. Install Apache on Linux

On Linux terminal, use the following command to install Apache:

sudo apt-get install apache2

This should be enough to get Apache up and running. You can open your browser on Windows and just try to open the root page (let's consider our host name is "ubuntu" from now on):

http://ubuntu/

And you should get a page from server telling you that Apache is running.

2. Create WebBroker Apache module in Delphi

Going back to Delphi, use menu option "File, New, Open...", then choose "Delphi Projects, WebBroker, Web Server Application".

Check "Linux" option, Next, and then select "Apache dynamic link module". When choosing the module name, type "xdata_module". Of course you can use any name you want, but we will use this name for further configuration in this tutorial. Finish the wizard and you will have the module created.

3. Build and deploy Apache module to Linux

Now build your Apache module (make sure to first select Linux64 platform in Project Manager, otherwise you would be building the module for Win32), and then click "Project, Deploy" menu option to deploy it to Linux. If you still have your PAServer running on Linux, it should send the module file to Linux.

The deployed file location should be something like this:

/home/user/PAServer/scratch-dir/User-Ubuntu/mod_xdata/libmod_xdata.so

I might be slightly different in your Linux computer, because you might have used a different user name for Linux user, and a different user name/profile in your Windows machine (the "User-Ubuntu" part). You can simply use WinSCP from Windows to browse your Linux files and discover where is the .so file deployed by PAServer. You should find it by navigating from the PAServer folder created in your home directory.

4. Create Apache configuration files

We must now configure Apache to use our new module. On Linux terminal, go to Apache module configuration folder:

cd /etc/apache2/mods-available

Create two new configuration files:

sudo touch xdata.conf
sudo touch xdata.load

Edit the first file to load module (we will use Nano editor here, if you don't have it installed in your Linux, you can use command "sudo apt-get install nano"):

sudo nano xdata.load

Use the following content for xdata.load file. Here we must use the location where our .so module file was deployed, as we saw in step 3.

LoadModule xdata_module /home/user/PAServer/scratch-dir/User-Ubuntu/mod_xdata/libmod_xdata.so

Press Ctrl+X to save and exit from nano editor, and then let's edit xdata.conf file:

sudo nano xdata.conf

This should be the content of this file:

<Location /tms>
    SetHandler libmod_xdata-handler
</Location>

Save that file as well, and Apache configuration is finished.

5. Enable Apache module

The configuration files we created are just a module that is "available". We should now enable the module using the following command:

sudo a2enmod xdata


6. Restart Apache

Now we should stop and restart Apache service. Use the following commands for that (we will use such commands very often further in next parts of this tutorial):

sudo apache2ctl stop
sudo apache2ctl start


7. Test Apache module

Now we can go back to Windows, use our browser and go to the following url (remember to change "ubuntu" to the hostname or IP address of your Linux machine, if it's different):

http://ubuntu/tms

And the browser should display "Web Server Application" as result.

In the next part we will learn how to add TMS Sparkle to the Apache module.

Wagner R. Landgraf


Bookmarks: 

This blog post has not received any comments yet. Add a comment.



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

Bookmarks: 

Monday, May 22, 2017

This is part 2 of the tutorial on how to create TMS XData and TMS Sparkle servers on Linux. On Part 1, we have installed Ubuntu and connected to it from Windows.

Here is video for second part: Installing PAServer on Linux and running our first "Hello, World" application on it.



To install PAServer, we need to send the tar.gz file containing PAServer files to Linux.

1. Download WinSCP from https://winscp.net/eng/download.php and install it.

To transfer files between Windows and Linux, we will use WinSCP tool. It's very convenient, just install it, and connect to Linux using same configuration/credentials we used when connecting with Putty (actually it even detects Putty is installed and asks if you want to import connection settings).

2. Transfer file LinuxPAServer19.0.tar.gz file to Linux

That file is located in the PAServer folder of your Delphi installation directory. If you used default settings, this should be in "C:Program Files (x86)EmbarcaderoStudio9.0PAServer" folder.

To transfer, just drag that file and drop it in WinSCP in the Linux folder you want. I recommend transferring it to your Linux home folder (/home/yourusername) which should be the default folder displayed when WinSCP is open.

3. Decompress PAServer

On Linux terminal, type the following command to decompress PAServer:

tar -xvf LinuxPAServer19.0.tar.gz


4. Launch PAServer

The files should be decompressed in a newly created folder named "PAServer-19.0". If you are still in the same directory, you can launch paserver with the following commands:

cd PAServer-19.0
./paserver


5. Create a new console application in Delphi with Linux64 support

Go back to Delphi and create a simple console application. Add Linux64 platform to it using the project manager. You can add a line to output a "Hello, Linux!" message:

WriteLn('Hello, Linux');


6. Build application to download SDK

Use menu "Project | Build" to build the console application. Delphi should warn you that you need Linux SDK, and would ask you for settings to connect to a Linux computer to download the SDK.

Type a name for the connection, enter the IP (or hostname) of the Ubuntu Linux computer we're running the PAServer, and you can proceed. Delphi should connect to the PAServer on Linux and download all files needed to build Linux applications. The download should take a while.

7. Run the application

Run the application (without debugging). Delphi should connect to the PAServer, send the application to Linux and launch it from there. If you go to Linux terminal, you should see the "Hello, Linux" message on it.

In the next part we will create and deploy our first Apache module on Linux.

Wagner R. Landgraf


Bookmarks: 

This blog post has not received any comments yet. Add a comment.



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

Bookmarks: 

Friday, May 19, 2017

Latest versions of TMS XData and TMS Sparkle have been just released with a major feature: Linux support. You can now deploy your XData/Sparkle server to Linux using Delphi 10.2 Tokyo, and in this video/blog post series, I will show you how to create and deploy such a server step-by-step. Since some Delphi users are mostly Windows users, I believe some of them are not very familiar with Linux and/or Apache, and because of that this tutorial is very basic and very detailed, covering it from the very beginning.

Here is the video with the first part: installing Linux on VirtualBox and connecting to it from Windows computer.



We will use Virtual Box to create our virtual machine, but you can choose any tool you want, if you are used to: VMWare, Parallels, Hyper-V, etc.

1. Download VirtualBox from https://www.virtualbox.org/wiki/Downloads and install it.

Just follow installer using default settings.

2. Download Ubuntu Linux Server from https://www.ubuntu.com/download/server

We will use the Server edition here (no GUI available by default). Save the ISO file in some folder, we will use it in step 3 below.

3. Create and configure VM

a) Create a new VM, name it, choose Linux as the guest OS
b) Just use default settings in the VM wizard, you can increase RAM or disk size if you want to
c) Once VM is created, change Network Adapter to Bridge (optional, I personally find it easier as it appears as another computer in the network)
b) Add the Ubuntu ISO file as a virtual disk in the VM storage

4. Launch VM and install Ubuntu

Launch VM and the Ubuntu installer will start from the ISO. Just use default settings for most of the install. You can follow the video for more details, but the installer will just ask for user name and password to be created, and several basic settings that you can just confirm what is presented.

5. Login and update OS

After install is complete, login and upgrade the system with the following commands on Linux terminal:

sudo apt-get update
sudo apt-get dist-upgrade

6. Preparing Linux for remote connection

We shouldn't have to switch to Linux VM all the time we want to operate on it. We can just connect to it from Windows. For that, we would need to install openssh-server package, and it will help if we install Samba so we can find our Linux computer by host name:

sudo apt-get install openssh-server samba
You can check the IP address of your Linux computer:

hostname -I
or just check the host name using hostname command:

hostname
7. Connect from Windows

We don't have to switch to Linux VM all the time we want to operate on it. We can just connect to it from Windows. For that, download Putty from http://www.putty.org and install it.

It will ask for the IP or host name for the connection, type the one you found in step 6 above, login using your credentials, and you're done.

In the next part we will create our first "Hello, World" application on Linux using Delphi.

Wagner R. Landgraf


Bookmarks: 

This blog post has received 1 comment. Add a comment.




Previous  |  Next  |  Index