unit Sparkle.Comp.CorsMiddleware;

interface

uses
  Sparkle.Comp.Server,
  Sparkle.HttpServer.Module,
  Sparkle.Middleware.Cors;

type
  TSparkleCorsMiddleware = class(TSparkleMiddleware)
  private
    FOrigin: string;
    FMethods: string;
    FIncludeMaxAge: Boolean;
    FMaxAge: Integer;
  protected
    function CreateMiddlewareInterface: IHttpServerMiddleware; override;
    class function GetDisplayName: string; override;
  published
    property Origin: string read FOrigin write FOrigin;
    property Methods: string read FMethods write FMethods;
    property MaxAge: Integer read FMaxAge write FMaxAge default 0;
    property IncludeMaxAge: Boolean read FIncludeMaxAge write FIncludeMaxAge default False;
  end;

implementation

{ TSparkleCorsMiddleware }

function TSparkleCorsMiddleware.CreateMiddlewareInterface: IHttpServerMiddleware;
begin
  if IncludeMaxAge then
    Result := TCorsMiddleware.Create(Origin, Methods, MaxAge)
  else
    Result := TCorsMiddleware.Create(Origin, Methods);
end;

class function TSparkleCorsMiddleware.GetDisplayName: string;
begin
  Result := 'CORS';
end;

end.
