unit Server.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
  TServerModule = class(TDataModule)
    AureliusConnection1: TAureliusConnection;
    SparkleHttpSysDispatcher1: TSparkleHttpSysDispatcher;
    XDataConnectionPool1: TXDataConnectionPool;
    XDataServer1: TXDataServer;
    XDataServer1CORS: TSparkleCorsMiddleware;
    procedure DataModuleCreate(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
    procedure StartServer(ABaseUrl: string);
  end;

implementation

uses
  XData.OpenApi.Service,
  DatabaseUtils;

{%CLASSGROUP 'System.Classes.TPersistent'}

{$R *.dfm}

{ TServerModule }

procedure TServerModule.DataModuleCreate(Sender: TObject);
var
  Conn: IDBConnection;
begin
  Conn := XDataConnectionPool1.GetPoolInterface.GetConnection;
  UpdateDatabase(Conn);
end;

procedure TServerModule.StartServer(ABaseUrl: string);
begin
  // Enable OpenAPI/Swagger support
  XDataServer1.BaseUrl := ABaseUrl;

//  RegisterOpenApiService;

  SparkleHttpSysDispatcher1.Start;
  WriteLn(Format('Module listening at "%s"', [ABaseUrl]));
end;

end.
