2013-10-29 16:51:16 +01:00
|
|
|
program BasicDemo;
|
|
|
|
|
|
|
|
{$APPTYPE CONSOLE}
|
|
|
|
|
2016-06-22 17:49:16 +02:00
|
|
|
|
2013-10-29 16:51:16 +01:00
|
|
|
uses
|
|
|
|
System.SysUtils,
|
|
|
|
Web.WebReq,
|
|
|
|
Web.WebBroker,
|
|
|
|
IdHTTPWebBrokerBridge,
|
2017-04-14 16:43:31 +02:00
|
|
|
MVCFramework.REPLCommandsHandlerU,
|
2017-04-14 17:28:18 +02:00
|
|
|
MVCFramework.Logger,
|
2013-10-29 16:51:16 +01:00
|
|
|
WebModuleUnit1 in 'WebModuleUnit1.pas' {WebModule1: TWebModule} ,
|
|
|
|
App1MainControllerU in 'App1MainControllerU.pas';
|
|
|
|
|
|
|
|
{$R *.res}
|
|
|
|
|
2016-06-22 17:49:16 +02:00
|
|
|
|
2013-10-29 16:51:16 +01:00
|
|
|
procedure RunServer(APort: Integer);
|
|
|
|
var
|
2017-04-14 16:43:31 +02:00
|
|
|
lServer: TIdHTTPWebBrokerBridge;
|
|
|
|
lCustomHandler: TMVCCustomREPLCommandsHandler;
|
|
|
|
lCmd, lStartupCommand: string;
|
2013-10-29 16:51:16 +01:00
|
|
|
begin
|
2017-04-14 16:43:31 +02:00
|
|
|
if ParamCount >= 1 then
|
|
|
|
lStartupCommand := ParamStr(1)
|
|
|
|
else
|
2017-05-25 16:57:49 +02:00
|
|
|
lStartupCommand := 'start';
|
2017-04-14 16:43:31 +02:00
|
|
|
|
|
|
|
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;
|
|
|
|
|
2017-04-14 17:28:18 +02:00
|
|
|
// Writeln(Format('Starting HTTP Server or port %d', [APort]));
|
2013-10-29 16:51:16 +01:00
|
|
|
LServer := TIdHTTPWebBrokerBridge.Create(nil);
|
|
|
|
try
|
|
|
|
LServer.DefaultPort := APort;
|
2017-04-14 17:28:18 +02:00
|
|
|
LogI(Format('Server started on port %d', [APort]));
|
|
|
|
|
|
|
|
{ more info about MaxConnections
|
|
|
|
http://www.indyproject.org/docsite/html/frames.html?frmname=topic&frmfile=TIdCustomTCPServer_MaxConnections.html }
|
|
|
|
LServer.MaxConnections := 0;
|
|
|
|
|
|
|
|
{ more info about ListenQueue
|
|
|
|
http://www.indyproject.org/docsite/html/frames.html?frmname=topic&frmfile=TIdCustomTCPServer_ListenQueue.html }
|
|
|
|
LServer.ListenQueue := 200;
|
|
|
|
|
2017-04-14 16:43:31 +02:00
|
|
|
WriteLn('Write "quit" or "exit" to shutdown the server');
|
|
|
|
repeat
|
|
|
|
// TextColor(RED);
|
|
|
|
// TextColor(LightRed);
|
|
|
|
Write('-> ');
|
|
|
|
// TextColor(White);
|
|
|
|
if lStartupCommand.IsEmpty then
|
|
|
|
ReadLn(lCmd)
|
|
|
|
else
|
|
|
|
begin
|
|
|
|
lCmd := lStartupCommand;
|
|
|
|
lStartupCommand := '';
|
|
|
|
WriteLn(lCmd);
|
|
|
|
end;
|
|
|
|
|
|
|
|
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;
|
|
|
|
until false;
|
|
|
|
|
2013-10-29 16:51:16 +01:00
|
|
|
finally
|
|
|
|
LServer.Free;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
|
|
|
begin
|
|
|
|
ReportMemoryLeaksOnShutdown := True;
|
|
|
|
try
|
|
|
|
if WebRequestHandler <> nil then
|
|
|
|
WebRequestHandler.WebModuleClass := WebModuleClass;
|
|
|
|
WebRequestHandlerProc.MaxConnections := 1024;
|
2017-05-25 16:57:49 +02:00
|
|
|
RunServer(8080);
|
2013-10-29 16:51:16 +01:00
|
|
|
except
|
|
|
|
on E: Exception do
|
|
|
|
Writeln(E.ClassName, ': ', E.Message);
|
2014-09-05 12:47:40 +02:00
|
|
|
end;
|
2013-10-29 16:51:16 +01:00
|
|
|
|
|
|
|
end.
|