Have an amazing solution built in RAD Studio? Let us know. Looking for discounts? Visit our Special Offers page!
News

Introduction to Building Your First RAD Server Service

[See the full index of articles for more information]

Table of Contents:

In this blog post, you’ll learn how to build your first RAD Server service-based applications using Delphi and C++Builder. Before you begin development, you’ll want to make sure that your InterBase database server is running. RAD Server uses an InterBase database for the storage of user information, user groups, analytics, registered devices, version information, registered Edge Modules, push notification messages and more.  


Figure 1: InterBase 2017 Server Manager

1. Using the RAD Server Project Wizard

The fastest way to get started is to use the New Projects menu (File | New | Other…) and choose the RAD Server | RAD Server Package wizard for Delphi or C++Builder.


Figure 2: RAD Server Project Wizard choices for Delphi and C++

Select the RAD Server Package project. A wizard will appear to help create the starting project. On the first page choose how the wizard will create the resources and endpoints that will appear in the RAD Server application. The RAD Server Package Wizard provides two choices to proceed.

Choice 1: Create an empty package that does not register a resource. Choose this option if you are going to add your resources later. Using this choice, a package project will be created with a starting main library.


Figure 3: Create an empty RAD Server package

Clicking the Finish button will create the starting project for additional development work to create the finished RAD Server application.

Choice 2: Create a package with a resource that extends the REST API of for the RAD Server. Click the Next Button and two additional wizard steps will appear to help create the package project, resource and endpoints. To build the first RAD Server project make this choice.


Figure 4: Create a resource based RAD Server package

On the wizard’s second page set the Resource name to “Test”. The File type radio buttons presents two options: 1) create a unit for implementing the resource in code, and 2) create a data module for implementing the resource using the IDE’s designer, components and code editor. For this first RAD Server application chose to use a Data Module.


Figure 5: RAD Server Package Wizard page 2 – set the resource name and file type

Click the Next button to create a starting set of endpoints.


Figure 6: RAD Server Package Wizard page 3 – choose starting EndPoints

On the wizard’s third page choose from a set of predefined EndPoints: Get (REST GET), GetItem (REST GET with a segment at the end of the URL that identifies the item to get) , Post (REST Post), PutItem (REST Put with a segment at the end of the URL that identifies the item to put), and DeleteItem (REST Delete with a segment at the end of the URL that identifies the item to delete). There is also a Select/deselect all check box at the bottom of the EndPoints list.

To create a package project with all of the EndPoints defined, click on all of the starting end points or choose Select All.

You can also step back through the wizard or click on the step links to make changes to the Package, Resource and Endpoints. To create your starting project is to click the Finish button.

After the wizard completes, you are placed back in the IDE. The first thing to do is to save the project. For the C++ and Delphi data module, use the name “MyDMUnit”. For the C++ project and package use the name “MyFirstCppRADServerPackage”. For the Delphi project and package use the name “MyFirstDelphiRADServerPackage”.

2. The Wizard Generated RAD Server Project and Source Code

Here are the projects and source code created by the wizard for Delphi and C++. For the C++ code I have added a couple of lines of code in the Get and GetItem endpoint implementations to set the response (look for the // sample code comment line in MyDMUnit.cpp).

Delphi:


Figure 7: Generated Delphi project
and Figure 8: Generated Delphi resource module

MyDMUnit.pas:

C++:


Figure 9: Generated C++ project
and Figure 10: Generated C++ resource module

MyFirstCppRADServerPackage.cpp:

MyDMUnit.h:

3. Configuring your first RAD Server Application

Now that you’ve used the wizard to build your first RAD Server application, you can use the IDE to compile and test the application. The IDE uses the EMSDevServer as the host executable (EMDDevServer.exe) to start execution with the parameter being the package file to load. There are two versions of the EMSDevServer for Win32 and Win64 development ($(BDS)binEMSDevServer.exe and $(BDS)bin64EMSDevServer.exe).


Figure 11: Run | Parameters… dialog showing EMSDevServer.exe as the Host application

RAD Studio also includes EMSDevConsole.exe which will start the EMSDevConsole server and open the EMS Console Server Window. The EMS Console provides a web application which displays analytics, provides for user/group management, and more for your RAD Server application.

Choose the Run | Run menu item or hit F9 to compile, link and run the starting application. The RAD Server Development Server will start, by default, using TCP port 8080. The RAD Server Development Console uses, by default, port 8081.

If this is the first time that you’ve tried to run a RAD Server Application you will see a dialog box that says the RAD Server configuration file, emsserver.ini, was not found. This happens when there is no RAD Server registry key or if the configuration file does not exist.


Figure 12: Trying to start the RAD Server Development Server without a configuration file

Click the Yes button to run the RAD Server configuration wizard.


Figure 13: RAD Server setup wizard page 1 – specify the new EMS database connection parameters

In the first wizard step, enter the InterBase server instance (by default RAD Studio’s development version of InterBase Server uses gds_db). If you previously installed InterBase using a different server instance name, enter that name string. This wizard page also contains the name for the RAD Server database (emsserver.ib), and directory that will contain the database and configuration file.

Click the Next button to tell the wizard whether you to create sample RAD Server database data for users and user groups. For development and testing we’ll set both check boxes.


Figure 14: RAD Server setup wizard page 2 – choose to generate sample data for the database

Click the Next button to set up the user name and password for logging into the RAD Server console.


Figure 15: RAD Server setup wizard page 2 – choose a Console user name and password

Click the Next button to go to the final wizard step. The wizard is ready to create the RAD Server database file, configuration file, and set the Windows registry key for the currently logged in user.



Figure 16: Final RAD Server configuration wizard page

This page displays the database file path and name, configuration path and name and the Windows Registry key. You can always make changes to the RAD Server configuration file (emsserver.ini) at any time. Click the Finish button. A confirmation dialog will appear with a reminder that the configuration will use an instance of InterBase that does not have a RAD Server license. The development license limits your RAD Server application to a maximum of 5 users.


Figure 17: Confirmation dialog box – using InterBase instance without a RAD Server license

When you are ready to deploy your RAD Server application, you’ll be able to use your deployment licenses for RAD Server and InterBase.

Click the Yes button. The wizard will display location of the emsserver.ini configuration file. It also lists the sample data that has been added to the database.


Figure 18: List of RAD Server files created by the configuration wizard

Click the OK button. Two files now appear in the C:UsersPublicDocumentsEmbarcaderoEMS directory.


Figure 19: Files on disk created by the RAD Server configuration wizard

You can use InterBase’s IBConsole program to view the emsserver.ib database.


Figure 20: IBConsole view of the emsserver.ib database file

Use RegEdit to view the registry key created for the RAD Server configuration file location.



Figure 21: RAD Server configuration file registry key

4. Testing your first RAD Server Application

When the RAD Server configuration server is created, the RAD Server Development Server will start executing. On my development computer, the EMSDevServer reports that the default port (8080) is already in use because some other application (Tomcat Server, another web server or some other running application) is already using the port 8080.


Figure 22: The RAD Server application cannot start – the default port is already in use.

To fix this problem, Click the OK button.


Figure 23: RAD Server Development Server not started due to a port conflict

You can use the edit box to enter an unused port number (I chose port 8087). Click the Start button and, if the new port number is unused, the RAD Server Development server will start running for this first launch.

To make the port change permanent for your all of your development work, edit the RAD Server development emsserver.ini configuration file. Use a text editor to open the emsserver.ini file. Search for the [Server.Connection.Dev] section. Edit the unused port number and save the configuration file

[Server.Connection.Dev]
Port=8087

The RAD Server configuration file will be covered in more detail in the chapter titled “RAD Server Configuration INI File Explaned”.

When you hit run in the IDE your RAD Server Development Server will start executing and the Log will show the operations that take place for your package application.

C++ Version:



Figure 24: RAD Server Development Server starting the first C++ application package

Delphi Version:


Figure 25: RAD Server Development Server starting the first Delphi application package

The RAD Server Development Server log will display the configuration, database connection, licensing information, the application package that was loaded, the resources that were registered, and endpoints that were created.

Clicking the Open Browser button will start your default browser and display the JSON result of calling the GetVersion built-in endpoint. You’ve now used your first RAD Server REST endpoint!


Figure 26: Browser showing the output from calling the version endpoint

In the browser, change the URL to localhost:8087/Test and hit enter. The browser will receive the JSON response from the Get endpoint.


Figure 27: Browser showing the JSON output from the Test resource’s Get method

If you pass an additional item on the URL, the GetItem endpoint will be called, and the code behind will return a JSON string containing the resource name plus the item you typed.


Figure 28: Browser showing the JSON output from the Test resource’s GetItem method

For simple examples it’s okay to return a JSON string, but for larger, more complex data structures, you may not want to return a large JSON string. RAD Studio provides many other ways to generate JSON data including using JSON objects, JSON streams and the JSON writer.

Edit the URL to use the “Users” resource which will call the default GetUsers endpoint to display JSON for the user generated by the RAD Server configuration wizard in the RAD Server datastore (there is only one to start).



Figure 29: JSON response in the Browser for a call to the GetUsers end point

You’ve now used four of the endpoints that were generated by the RAD Server project wizard.

5. See Also

RAD Server’s Enterprise Mobility Services (EMS)

http://docwiki.embarcadero.com/RADStudio/en/Enterprise_Mobility_Services_(EMS)

Setting Up Your RAD Studio (EMS) Server

http://docwiki.embarcadero.com/RADStudio/en/Setting_Up_Your_EMS_Server

Configuring Your EMS Server or EMS Console Server on Windows

http://docwiki.embarcadero.com/RADStudio/en/Configuring_Your_EMS_Server_or_EMS_Console_Server_on_Windows


Reduce development time and get to market faster with RAD Studio, Delphi, or C++Builder.
Design. Code. Compile. Deploy.
Start Free Trial   Upgrade Today

   Free Delphi Community Edition   Free C++Builder Community Edition

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

IN THE ARTICLES