My goal is to use Delphi mORMot framework to implement a FHIR server.
So what are FHIR and mORMot?
FHIR stands for Fast Health Interoperability Resource, a framework focussed on exchanging health data via RESTful API, thus a FHIR server is normally a RESTful server.
mORMot is a Delphi framework that has features such as ORM an SOA etc, it supports REST service out of the box.
So why mORMot for FHIR?
mORMot is all about REST server, i.e., provides service via RESTful API, taking request and returning response in JSON format, it matches what a FHIR server is doing perfectly.
So how easy can we build up a RESTful server in mORMot to take a JSON request and return a JSON response?
I strongly recommend you to download mORMot source code by using Git client so that you can update the source code easily as it is still being updated actively. So I save the source code in "C:\MyDev\mORMot\Git Version". We will start with a new console project:
and save it as ECHO project:
The goal of this project is to build a server that can echo a HTTP post. First, we need to define a RESTful server:
TECHOServer = class (TSQLRestServerFullMemory)
published
procedure Echo(Ctxt: TSQLRestServerURIContext);
end;
And the implementation of Echo is quite simple, assigning the input to output and returning HTTP 200 to client:
procedure TECHOServer.Echo(Ctxt: TSQLRestServerURIContext);
begin
Ctxt.Call.Outbody := Ctxt.Call.InBody;
Ctxt.Call.OutStatus :=200;
end;
Then we need to start the RESTful server, as mORMot ties data model, data server and HTTP server together, thus we need to initialize a model and a server before we start a HTTP server:
aModel := TSQLModel.Create([],'service');
try
aServer := TECHOServer.Create(aModel);
try
aHTTPServer := TSQLHttpServer.Create('7878',aServer,'+',useHttpApiRegisteringURI);
write('Press [Enter] to close the server.');
try
readln;
finally
FreeAndNil(aHTTPServer);
end;
finally
FreeAndNil(aServer);
end;
finally
FreeAndNil(aModel);
end;
And the whole souce code is as simple as below (less than 50 lines):
program Echo;
{$APPTYPE CONSOLE}
{$R *.res}
uses
System.SysUtils,
mORMot,
mORMotHttpServer;
type
TECHOServer = class (TSQLRestServerFullMemory)
published
procedure Echo(Ctxt: TSQLRestServerURIContext);
end;
procedure TECHOServer.Echo(Ctxt: TSQLRestServerURIContext);
begin
Ctxt.Call.Outbody := Ctxt.Call.InBody;
Ctxt.Call.OutStatus :=200;
end;
var
aModel: TSQLModel;
aServer : TECHOServer;
aHTTPServer: TSQLHttpServer;
begin
aModel := TSQLModel.Create([],'service');
try
aServer := TECHOServer.Create(aModel);
try
aHTTPServer := TSQLHttpServer.Create('7878',aServer,'+',useHttpApiRegisteringURI);
write('Press [Enter] to close the server.');
try
readln;
finally
FreeAndNil(aHTTPServer);
end;
finally
FreeAndNil(aServer);
end;
finally
FreeAndNil(aModel);
end;
end.
Compile and run it:
Now it is time to test it:
It works!
As you can see, use mORMot to build a RESTful server it pretty simple.
In the next part, I will try to deserialize the input JSON so that we can use the resource as an object internally.