XData RemoteDB Multi Tenan

I'm using the XData Demos folder application, specifically the MultiTenantByHeader project, the idea is to use it as a basis for a project where the server has several folders with Firebird Databases, the client application will inform via Header the location of the database in the server. Once the server receives the request, from reading the header it would create or return a connection within a pool to access the data.
I would also like the same dynamic above to obtain the connection to be used also with RemoteDB.

Given the scenario above, here are my doubts:

1 - The correct way to configure a client application so that when a query in xdataset is executed it sends in the header the location of the database would be something like the example below:

RemoteDBDatabase1.OnRequestSending: = RequestSending;

Procedure TfmThreeTierAureliusRemoteDB.RequestSending (Sender: TObject; Req: THttpRequest);
begin
Req.Headers.SetValue ('database-name', 'c: \ database \ data.fdb');
end;

2 - Where would be the appropriate place to execute the AddDatabase method (DBName: string); whereas DBName would come from the Header of the client application, both through XData and RemoteDB.

3 - To use RemoteDB in this scenario I will need an AureliusConnection and optionally an FDConnection, can I simply add these components to the datamodule or will I need to create it dynamically?

4 - If I need to create the FDConnection dynamically does the mode below seem adequate?

function TServerByHeaderModule.CreateConnectionPool (
const DBName: string): IDBConnectionPool;
var
Connection: TFDConnection;
begin
// Just create your connection pool the way you do normally
// Each connection pool should have its specific connection settings to point
// to the correct database
Connection: = TFDConnection.Create (nil);
Connection.Params.Values ​​['Database']: = DBName;
Connection.Params.Values ​​['User_Name']: = 'SYSDBA';
Connection.Params.Values ​​['Password']: = 'masterkey';
Connection.Params.Values ​​['Server']: = 'localhost';

Result: = TDBConnectionPool.Create (10,
function: IDBConnection
begin
Result: = TFireDacConnectionAdapter.Create (Connection, False);
// Result: = TSQLiteNativeConnectionAdapter.Create (DBName + '.db');
end
);
end;

5 - How would it be to obtain information from the database location that comes in the request header, I can make use of the connection related to it that is in the pool for both xdata and remotedb?

Hello Moacir,

RemoteDB is different, it doesn't use a pool. It's actually simpler, you just need to create the connection according to the header. You can use something like this (pseudo code):

threadvar ThreadDBName: string;

procedure StartServer;  
var  
  Server: THttpSysServer;  
  Module: TRemoteDBModule;  
begin  
  Server := THttpSysServer.Create;  
  try  
    Module := TRemoteDBModule.Create(''http://localhost:2001/tms/remotedb'',  
      TDBConnectionFactory.Create(  
        function: IDBConnection  
        var  
          Module: TConnectionModule;  
        begin  
          Module := TConnectionModule.Create(nil);  
          Module.FDConnection.Params.Values[‘Database’] := ThreadDBName;  
          Result := TFireDacConnectionAdapter.Create(Module.FDConnection1, Module);  
        end  
      )  
    );  
    Module.AddMiddleware(TAnonymousMiddleware.Create(  
      procedure(C: THttpServerContext; Next: THttpServerProc)  
      begin  
        ThreadDBName := C.Request.Headers.Get('tenant-db-name');  
        Next(C);  
        ThreadDBName := '';  
      end  
    ));  
    Server.AddModule(Module);  
    Server.Start;  
    WriteLn(''RemoteDB server running...'');  
    ReadLn;  
  finally  
    Server.Free;  
  end;  
end;