unit Sparkle.WebBroker.Server;

interface

uses
  Sparkle.HttpServer.Context,
  Sparkle.HttpServer.Module,
  Sparkle.HttpServer.Dispatcher,
  Sparkle.WebBroker.Context,
  Sparkle.Logger;

type
  TWebBrokerServer = class
  strict private
    FDispatcher: THttpDispatcher;
    FLogger: TLogger;
  strict private
    function GetLogEngine: ILogEngine;
    procedure SetLogEngine(const Value: ILogEngine);
  protected
    procedure DoProcessRequest(Context: THttpServerContext); virtual;
    property Logger: TLogger read FLogger;
  public
    constructor Create;
    destructor Destroy; override;
    procedure DispatchRequest(Adapter: IWebBrokerAdapter);
    property LogEngine: ILogEngine read GetLogEngine write SetLogEngine;
    property Dispatcher: THttpDispatcher read FDispatcher;
  end;

implementation

{ TWebBrokerServer }

constructor TWebBrokerServer.Create;
begin
  FLogger := TLogger.Create;
  FDispatcher := THttpDispatcher.Create;
end;

destructor TWebBrokerServer.Destroy;
begin
  FLogger.Free;
  FDispatcher.Free;
  inherited;
end;

procedure TWebBrokerServer.DispatchRequest(Adapter: IWebBrokerAdapter);
var
  C: THttpServerContext;
begin
  C := TWebBrokerContext.Create(Adapter);
  try
    FDispatcher.DispatchRequest(C);
  finally
    C.Free;
  end;
end;

procedure TWebBrokerServer.DoProcessRequest(Context: THttpServerContext);
begin
  FDispatcher.DispatchRequest(Context);
end;

function TWebBrokerServer.GetLogEngine: ILogEngine;
begin
  Result := Logger.LogEngine;
end;

procedure TWebBrokerServer.SetLogEngine(const Value: ILogEngine);
begin
  Logger.LogEngine := Value;
  FDispatcher.LogEngine := Value;
end;

end.
