unit ServerByUrl.Module;

interface

uses
  System.SysUtils, System.Classes, System.Generics.Collections,
  Aurelius.Drivers.SQLite,
  Aurelius.Drivers.Interfaces, XData.Server.Module, Sparkle.Comp.Server,
  Sparkle.Comp.JwtMiddleware, XData.Comp.Server, XData.Comp.ConnectionPool,
  Sparkle.Comp.HttpSysDispatcher, Aurelius.Comp.Connection,
  Sparkle.Comp.CorsMiddleware, Sparkle.HttpServer.Module,
  Sparkle.HttpServer.Context;

type
  TServerByUrlModule = class(TDataModule)
    AureliusConnection1: TAureliusConnection;
    SparkleHttpSysDispatcher1: TSparkleHttpSysDispatcher;
    XDataConnectionPool1: TXDataConnectionPool;
    XDataServer1: TXDataServer;
    XDataServer1CORS: TSparkleCorsMiddleware;
  private
    { Private declarations }
  public
    { Public declarations }
    procedure StartServer(DBName: string);
  end;

const
  DefaultServerUrl = 'http://localhost:2001/tms/multitenant/';

implementation

uses
  XData.OpenApi.Service,
  DatabaseUtils;

{%CLASSGROUP 'System.Classes.TPersistent'}

{$R *.dfm}

{ TServerByUrlModule }

procedure TServerByUrlModule.StartServer(DBName: string);
var
  Conn: IDBConnection;
begin
  XDataServer1.BaseUrl := DefaultServerUrl + DBName;
  AureliusConnection1.Params.Values['Database'] := DBName + '.db';

  Conn := AureliusConnection1.CreateConnection;
  UpdateDatabase(Conn);

  // Enable OpenAPI/Swagger support
  RegisterOpenApiService;

  SparkleHttpSysDispatcher1.Start;
end;

end.
