#1 2015-08-04 20:29:40

restneophyte
Member
Registered: 2014-11-18
Posts: 26

Adding a Service via method to my mORMot server...

Using Delphi XE8, the latest nightly build of mORMot and trying to follow the 'SUM' example of Chapter 14 of the documentation in addition to accessing a mySQL database, I have created the following ReST Server:

uses
  ...
  ReST_ModelTest in 'ReST_ModelTest.pas';

const
  ...

type
  TMyHttpServer = class(TSQLHttpServer)
    published
      procedure Sum(Ctxt: TSQLRestServerURIContext);
  end;

procedure TMyHttpServer.Sum(Ctxt: TSQLRestServerURIContext);
begin
  with Ctxt do
    Results([InputDouble['a'] + InputDouble['b']]);
end;

var
  iModel: TSQLModel;
  iHttpServer: TMyHttpServer;
  iRestServer: TSQLRestServerDB;
  iConnection: TSQLDBConnectionProperties;
begin
  SQLite3Log.Family.Level        := LOG_VERBOSE;
  SQLite3Log.Family.PerThreadLog := ptIdentifiedInOnFile;

  iConnection := TSQLDBZEOSConnectionProperties.Create(TSQLDBZEOSConnectionProperties.URI(dMySQL, c_mySQLDBServer+ ':3306'), c_mySQLDB, c_mySQLDBUsername, c_mySQlDBPassword);
  try
    iModel := MyDataModel;
    VirtualTableExternalRegister(iModel, TMyCustomers, iConnection, 'tblMyCustomers');
    try
      iRestServer := TSQLRestServerDB.Create(iModel, ':memory:', false);  // authentication = false
      try
        iRestServer.AcquireExecutionMode[execORMGet]   := amBackgroundORMSharedThread;
        iRestServer.AcquireExecutionMode[execORMWrite] := amBackgroundORMSharedThread;
        iRestServer.CreateMissingTables;

        iHttpServer := TMyHttpServer.Create(SERVER_PORT, [iRestServer], '+', useHttpApiRegisteringURI);
        try
          iHttpServer.AccessControlAllowOrigin := '*'; // allow cross-site AJAX queries
          writeln('Background server is running.'#10);
          write('Press [Enter] to close the server.');
          readln;
        finally
          iHttpServer.Free;
        end;
      finally
        iRestServer.Free;
      end;
    finally
      iModel.Free;
    end;
  finally
    iConnection.Free;
  end;
end.

My 'ReST_ModelTest' is defined as follow:

unit ReST_ModelTest;

interface

uses
  SynCommons,
  mORMot;

type
  TMyCustomers = class(TSQLRecord)  // TSQLRecord has a built-in ID: integer primary key
    private
      fCustomerID: RawUTF8;
      fCustomerName: RawUTF8;
    published
      /// ORM will create the table columns
      property CustomerID: RawUTF8 index 15 read fCustomerID write fCustomerID;
      property CustomerName: RawUTF8 index 16 read fCustomerName write fCustomerName;
  end;

function MyDataModel: TSQLModel;

const
  SERVER_ROOT = 'root';
  SERVER_PORT = '888';

implementation

function MyDataModel: TSQLModel;
begin
  Result := TSQLModel.Create([TMyCustomers], SERVER_ROOT);

  TMyCustomers.AddFilterOrValidate('CustomerID', TSynValidateText.Create);    // ensure field exists
  TMyCustomers.AddFilterOrValidate('CustomerName', TSynValidateText.Create);  // ensure field exists
end;

end.

After generating my 'mORMotClient' unit, I would like to create a REST client that would be using the both the 'TMyCustomers' type defined above as well as the 'SUM' function. However, I am not sure on how to do that. I have tried the following but it crashes my Client app.

function do_Sum(aVal1: Double; aVal2: Double): Double;
var
  iErr: Integer;
  iResults: TSQLRecordClass;
  iClient: TSQLRestClientHTTP;
begin
  iClient := GetClient('localhost');
  try
    Val(iClient.CallBackGetResult('sum', ['a', aVal1, 'b', aVal2], iResults), Result, iErr);
  finally
    iClient.Free;
  end;
end;

I must be doing something wrong but I cannot figure out what it is. Any pointers to a solution to my problem would be greatly appreciated.

Offline

Board footer

Powered by FluxBB