Please enable JavaScript to view this site.

Codolex

Structure

Setting up an API with Codolex is very simple.

By default the generated code includes a folder "\.fsrc\[yourcodolexprojectname]\.API". this folder contains the following files as basis.

[yourcodolexprojectname].API.Authentication.pas

[yourcodolexprojectname].API.Controller.base.pas

[yourcodolexprojectname].API.Server.pas

[yourcodolexprojectname].API.Server.dfm

 

If you have a Codolex project linked, the generated folder is included in the search path by default.

The ...Server.pas file contains everything you need to start the API.

 

APIServerClass

 

You can include this file anywhere in your project. and start the API with the following code

 

begin

 var APIServer := TCodolexAPIProjectApiServer.Create;

 

 APIServer.Start(8080);

end;

 

Configuration

The server is normally run on the localhost without a certificate. The Sever instance is available as property to configure the server.

This can be used set a certificate for example.

 

var ServerInstance := APIServer.ServerInstance;

var Handler := TIdServerIOHandlerSSLOpenSSL.Create;

 

Handler.SSLOptions.Mode := sslmBoth;

Handler.SSLOptions.KeyFile := KeyFileLocation;

Handler.SSLOptions.CertFile := CertFileLocation;

Handler.SSLOptions.RootCertFile := RootCertFileLocation;

Handler.SSLOptions.SSLVersions := [sslvTLSv1, sslvTLSv1_1, sslvTLSv1_2];

 

ServerInstance.OnQuerySSLPort := OnQuerySSL;

ServerInstance.IOHandler := Handler;

 

Next to the server instance, there are 2 public functions that can be used for configuration.

 

1. OnAuthentication

The procedure expects an action to validate the user. The 'TOnAuthenticationEvent' type is the following function

TOnAuthenticationEvent = reference to function(const UserName, Password: string; const UserRoles: TList<string>; const SessionData: TDictionary<string, string>): Boolean;  

 

This function gets called before every request to check if the user has the right access to the object/flow.

The result must be a boolean. you can check the UserName and Password that the user provides to authorize the request.

The roles will be further explained in the section Configure security

 

2. WithSwaggerInfo

The API generates Swaggerinfo by default. This swagger info can be given extra details with the 'TUseSwaggerInfo' function.

TUseSwaggerInfo = reference to function: TMVCSwaggerInfo;

 

The function has the return the TMVCSwaggerInfo object. This object has some attributes that can set the swagger information.

SwaggerInfoObject

 

So the swagger info can be provided like this:

var SwaggerInfo: TUseSwaggerInfo := function: TMVCSwaggerInfo

begin

 Result.Title := 'TestProject API Server';

 Result.Version := '2.4.0';

 Result.Description := 'TestProject API Server generated by Codolex';

 Result.ContactEmail := 'info@codolex.com';

end;

 

var APIServer := TCodolexAPIProjectApiServer.create;

APIServer.WithSwaggerInfo(SwaggerInfo);

 

Stop server

The server can be stopped by calling the Stop function, or by closing the application.

© by GDK Software