unit App.Server.Module;

interface

uses
  System.SysUtils, System.Classes, System.Generics.Collections,
  Sparkle.Comp.Server, Sparkle.Comp.StaticServer, Sparkle.Comp.HttpSysDispatcher,
  Sparkle.Module.Static, Sparkle.Comp.CompressMiddleware,
  Sparkle.HttpServer.Module, Sparkle.HttpServer.Context;

type
  TAppServerModule = class(TDataModule)
    SparkleHttpSysDispatcher1: TSparkleHttpSysDispatcher;
    SparkleStaticServer1: TSparkleStaticServer;
    SparkleStaticServer1Compress: TSparkleCompressMiddleware;
  private
    { Private declarations }
  public
    { Public declarations }
    procedure StartAppServer(ABaseUrl: string);
  end;

const
  SERVER_PATH_SEGMENT = 'app';

implementation

uses
  Sparkle.Middleware.Compress,
  Common.Logging,
  Common.Config;

{%CLASSGROUP 'Vcl.Controls.TControl'}

{$R *.dfm}

{ TAppServerModule }

procedure TAppServerModule.StartAppServer(ABaseUrl: string);
var
  Url: string;
begin
  Url := ABaseUrl;
  if not Url.EndsWith('/') then
    Url := Url + '/';
  Url := Url + SERVER_PATH_SEGMENT;

  SparkleStaticServer1.BaseUrl := Url;
  SparkleStaticServer1.RootDir := ServerConfig.WebAppFolder;

  SparkleHttpSysDispatcher1.Start;
  Logger.Log(Format('App server module listening at "%s"', [Url]));
end;

end.
