delphimvcframework/samples/swaggerdoc_extended/SwaggerDocApiExtended.dpr
2022-01-04 15:44:47 +01:00

130 lines
3.5 KiB
ObjectPascal

program SwaggerDocApiExtended;
{$APPTYPE CONSOLE}
uses
{$IF Defined(MSWINDOWS)}
Winapi.ShellAPI,
Winapi.Windows,
{$ENDIF }
System.SysUtils,
MVCFramework.Logger,
MVCFramework.Commons,
MVCFramework.REPLCommandsHandlerU,
MVCFramework.Middleware.Redirect,
Web.ReqMulti,
Web.WebReq,
Web.WebBroker,
IdContext,
IdHTTPWebBrokerBridge,
WebModuleMainU in 'WebModuleMainU.pas' {WebModule1: TWebModule},
ControllersU in 'ControllersU.pas',
EntitiesU in 'EntitiesU.pas',
BaseControllerU in 'BaseControllerU.pas',
AuthHandler in 'AuthHandler.pas',
MVCFramework.Swagger.Commons in '..\..\sources\MVCFramework.Swagger.Commons.pas',
MVCFramework.Middleware.Swagger in '..\..\sources\MVCFramework.Middleware.Swagger.pas';
{$R *.res}
procedure RunServer(APort: Integer);
var
LServer: TIdHTTPWebBrokerBridge;
LCustomHandler: TMVCCustomREPLCommandsHandler;
LCmd: string;
lURL: string;
begin
Writeln('** DMVCFramework Server ** build ' + DMVCFRAMEWORK_VERSION);
LCmd := 'start';
if ParamCount >= 1 then
LCmd := ParamStr(1);
LCustomHandler := function(const Value: String; const Server: TIdHTTPWebBrokerBridge; out Handled: Boolean): THandleCommandResult
begin
Handled := False;
Result := THandleCommandResult.Unknown;
// Write here your custom command for the REPL using the following form...
// ***
// Handled := False;
// if (Value = 'apiversion') then
// begin
// REPLEmit('Print my API version number');
// Result := THandleCommandResult.Continue;
// Handled := True;
// end
// else if (Value = 'datetime') then
// begin
// REPLEmit(DateTimeToStr(Now));
// Result := THandleCommandResult.Continue;
// Handled := True;
// end;
end;
LServer := TIdHTTPWebBrokerBridge.Create(nil);
try
LServer.OnParseAuthentication := TMVCParseAuthentication.OnParseAuthentication;
LServer.DefaultPort := APort;
LServer.KeepAlive := True;
{ more info about MaxConnections
http://ww2.indyproject.org/docsite/html/frames.html?frmname=topic&frmfile=index.html }
LServer.MaxConnections := 0;
{ more info about ListenQueue
http://ww2.indyproject.org/docsite/html/frames.html?frmname=topic&frmfile=index.html }
LServer.ListenQueue := 200;
lURL := Format('http://localhost:%d', [APort]);
WriteLn('SWAGGER UI available at ', lURL);
{$IF Defined(MSWINDOWS)}
ShellExecute(0,
nil,
PChar(LURL), nil, nil, SW_SHOWNOACTIVATE);
{$ENDIF}
WriteLn('Write "quit" or "exit" to shutdown the server');
repeat
if LCmd.IsEmpty then
begin
Write('-> ');
ReadLn(LCmd)
end;
try
case HandleCommand(LCmd.ToLower, LServer, LCustomHandler) of
THandleCommandResult.Continue:
begin
Continue;
end;
THandleCommandResult.Break:
begin
Break;
end;
THandleCommandResult.Unknown:
begin
REPLEmit('Unknown command: ' + LCmd);
end;
end;
finally
LCmd := '';
end;
until False;
finally
LServer.Free;
end;
end;
begin
ReportMemoryLeaksOnShutdown := True;
IsMultiThread := True;
try
if WebRequestHandler <> nil then
WebRequestHandler.WebModuleClass := WebModuleClass;
WebRequestHandlerProc.MaxConnections := 1024;
RunServer(8080);
except
on E: Exception do
Writeln(E.ClassName, ': ', E.Message);
end;
end.