unit Sparkle.Comp.HttpSysDispatcher;

{$I Sparkle.Inc}

interface

uses
  System.Classes, System.SysUtils,
  Sparkle.HttpSys.Server,
  Sparkle.Comp.Server;

type
  [ComponentPlatforms(pidWin32 or pidWin64)]
  TSparkleHttpSysDispatcher = class(TSparkleDispatcher)
  private
    {$IFDEF MSWINDOWS}
    FHttpSys: THttpSysServer;
    function GetHttpSys: THttpSysServer;
    property HttpSys: THttpSysServer read GetHttpSys;
    {$ENDIF}
  strict protected
    procedure DoStart; override;
    procedure DoStop; override;
    function GetActive: Boolean; override;
  public
    destructor Destroy; override;
  published
    property Active;
  end;

implementation

{ TSparkleHttpSysDispatcher }

destructor TSparkleHttpSysDispatcher.Destroy;
begin
  {$IFDEF MSWINDOWS}
  FHttpSys.Free;
  {$ENDIF}
  inherited;
end;

procedure TSparkleHttpSysDispatcher.DoStart;
begin
  {$IFDEF MSWINDOWS}
  if Active then Exit;

  HttpSys.Dispatcher.ClearModules;
  AddDispatcherModules(HttpSys.Dispatcher);
  HttpSys.Start;
  {$ENDIF}
end;

procedure TSparkleHttpSysDispatcher.DoStop;
begin
  {$IFDEF MSWINDOWS}
  HttpSys.Stop;
  {$ENDIF}
end;

function TSparkleHttpSysDispatcher.GetActive: Boolean;
begin
  {$IFDEF MSWINDOWS}
  Result := HttpSys.IsRunning;
  {$ELSE}
  Result := False;
  {$ENDIF}
end;

{$IFDEF MSWINDOWS}
function TSparkleHttpSysDispatcher.GetHttpSys: THttpSysServer;
begin
  if FHttpSys = nil then
    FHttpSys := THttpSysServer.Create;
  Result := FHttpSys;
end;
{$ENDIF}

end.
