unit Ws.Server.Module;

interface

uses
  System.SysUtils, System.Classes, System.Generics.Collections,
  Aurelius.Drivers.SQLite,
  Aurelius.Comp.Connection,
  Aurelius.Drivers.Interfaces,
  XData.Aurelius.ConnectionPool, XData.Server.Module, Sparkle.Comp.Server,
  XData.Comp.Server, XData.Comp.ConnectionPool, Sparkle.Comp.HttpSysDispatcher,
  Aurelius.Criteria.Linq,
  Sparkle.HttpServer.Module, Sparkle.HttpServer.Context,
  Sparkle.Comp.CompressMiddleware, Sparkle.Comp.CorsMiddleware,
  Sparkle.Comp.GenericMiddleware,  Sparkle.Middleware.WebSocket, Sparkle.Comp.WebSocketMiddleware,
  Aurelius.Drivers.UniDac, UniProvider,
  Data.DB, DBAccess, Uni, Sparkle.Comp.ForwardMiddleware;

type
  TWsServerModule = class(TDataModule)
    SparkleHttpSysDispatcher3: TSparkleHttpSysDispatcher;
    XDataServer1: TXDataServer;
    XDataServer1Logging: TSparkleGenericMiddleware;
    XDataServer1CORS: TSparkleCorsMiddleware;
    XDataServer1WebSocket: TSparkleWebSocketMiddleware;

    procedure XDataServer1LoggingMiddlewareCreate(Sender: TObject;
      var Middleware: IHttpServerMiddleware);
  private
    { Private declarations }
  public
    { Public declarations }
    procedure StartWsServer(ABaseUrl: string; AModelName: string);
  end;

const
  SERVER_PATH_SEGMENT = 'ws';

var
  WsServerModule: TWsServerModule;

implementation

uses
  Sparkle.HttpServer.Request,
  Sparkle.Middleware.Cors,
  Sparkle.Middleware.Compress,
  XData.OpenApi.Service,
  XData.Sys.Exceptions,
  Common.Logging,
  Common.Middleware.Logging,
  Common.Config, Vcl.Forms, IniFiles,
  XData.Service.Common,
  Ws.ServiceImpl;

{%CLASSGROUP 'Vcl.Controls.TControl'}

{$R *.dfm}

{ TWsServerModule }



procedure TWsServerModule.StartWsServer(ABaseUrl: string; AModelName: string);
var
  Url: string;
begin
  RegisterOpenApiService;
  RegisterServiceType(TWebSocketService);
  Url := ABaseUrl;
  if not Url.EndsWith('/') then
    Url := Url + '/';
  Url := Url + SERVER_PATH_SEGMENT;
  XDataServer1.BaseUrl := Url;
  XDataServer1.ModelName := AModelName;
  SparkleHttpSysDispatcher3.Start;
  Logger.Log(1, Format('Ws server module listening at "%s"', [Url]));
end;

procedure TWsServerModule.XDataServer1LoggingMiddlewareCreate(Sender: TObject;
  var Middleware: IHttpServerMiddleware);
begin
  Middleware := TLoggingMiddleware.Create(Logger, 1);
end;

end.
