2017-04-14 16:43:31 +02:00
|
|
|
unit MVCFramework.REPLCommandsHandlerU;
|
|
|
|
|
|
|
|
interface
|
|
|
|
|
|
|
|
uses
|
2018-08-05 20:31:33 +02:00
|
|
|
IdHTTPWebBrokerBridge,
|
|
|
|
MVCFramework;
|
2017-04-14 16:43:31 +02:00
|
|
|
|
|
|
|
type
|
|
|
|
|
2018-08-05 20:31:33 +02:00
|
|
|
{$SCOPEDENUMS ON}
|
2017-04-14 16:43:31 +02:00
|
|
|
THandleCommandResult = (Continue, Break, Unknown);
|
2018-08-05 20:31:33 +02:00
|
|
|
TMVCCustomREPLCommandsHandler = reference to function(const Value: string; const Server: TIdHTTPWebBrokerBridge; out Handled: Boolean)
|
|
|
|
: THandleCommandResult;
|
2017-04-14 16:43:31 +02:00
|
|
|
|
2018-08-05 20:31:33 +02:00
|
|
|
function HandleCommand(const Value: string; const Server: TIdHTTPWebBrokerBridge;
|
|
|
|
const CustomCommandsHandler: TMVCCustomREPLCommandsHandler = nil): THandleCommandResult;
|
2017-10-16 22:57:27 +02:00
|
|
|
procedure REPLEmit(const Value: string);
|
2017-04-14 16:43:31 +02:00
|
|
|
|
|
|
|
implementation
|
|
|
|
|
|
|
|
uses
|
2018-08-05 20:31:33 +02:00
|
|
|
System.SysUtils,
|
|
|
|
MVCFramework.Commons,
|
|
|
|
MVCFramework.Logger;
|
2017-04-14 16:43:31 +02:00
|
|
|
|
2017-10-16 22:57:27 +02:00
|
|
|
procedure REPLEmit(const Value: string);
|
2017-04-14 16:43:31 +02:00
|
|
|
begin
|
|
|
|
// TextColor(LightGreen);
|
2017-10-16 22:57:27 +02:00
|
|
|
write('#> ');
|
2017-04-14 16:43:31 +02:00
|
|
|
// TextColor(White);
|
|
|
|
WriteLn(Value);
|
|
|
|
// TextColor(White);
|
|
|
|
end;
|
|
|
|
|
2018-08-05 20:31:33 +02:00
|
|
|
function HandleCommand(const Value: string; const Server: TIdHTTPWebBrokerBridge;
|
|
|
|
const CustomCommandsHandler: TMVCCustomREPLCommandsHandler): THandleCommandResult;
|
2017-04-14 16:43:31 +02:00
|
|
|
var
|
|
|
|
lTempCommandResult: THandleCommandResult;
|
|
|
|
lHandled: Boolean;
|
|
|
|
begin
|
|
|
|
Result := THandleCommandResult.Unknown;
|
|
|
|
if Assigned(CustomCommandsHandler) then
|
|
|
|
begin
|
|
|
|
lTempCommandResult := CustomCommandsHandler(Value, Server, lHandled);
|
|
|
|
if lHandled then
|
|
|
|
Exit(lTempCommandResult);
|
|
|
|
end;
|
|
|
|
|
|
|
|
// Handling standard REPL commands
|
|
|
|
if (Value = 'quit') or (Value = 'exit') then
|
|
|
|
begin
|
2018-08-05 20:31:33 +02:00
|
|
|
EnterInShutdownState;
|
2017-04-14 16:43:31 +02:00
|
|
|
REPLEmit('Shutting down...');
|
|
|
|
Result := THandleCommandResult.Break;
|
|
|
|
end
|
|
|
|
else if (Value = 'version') then
|
|
|
|
begin
|
|
|
|
REPLEmit(DMVCFRAMEWORK_VERSION);
|
|
|
|
Result := THandleCommandResult.Continue;
|
|
|
|
end
|
|
|
|
else if (Value = 'stop') then
|
|
|
|
begin
|
|
|
|
if not Server.Active then
|
|
|
|
begin
|
|
|
|
REPLEmit('Server not running');
|
|
|
|
end
|
|
|
|
else
|
|
|
|
begin
|
|
|
|
REPLEmit('Stopping server...');
|
|
|
|
Server.Active := False;
|
|
|
|
REPLEmit('done!');
|
|
|
|
end;
|
|
|
|
Result := THandleCommandResult.Continue;
|
|
|
|
end
|
|
|
|
else if (Value = 'start') then
|
|
|
|
begin
|
|
|
|
if Server.Active then
|
|
|
|
begin
|
|
|
|
REPLEmit(Format('Server already running on port %d...', [Server.Bindings.DefaultPort]));
|
|
|
|
end
|
|
|
|
else
|
|
|
|
begin
|
|
|
|
REPLEmit(Format('Starting server on port %d...', [Server.Bindings.DefaultPort]));
|
|
|
|
Server.Active := True;
|
2017-10-16 22:57:27 +02:00
|
|
|
LogI('Server started on port ' + Server.Bindings.DefaultPort.ToString);
|
2017-04-14 16:43:31 +02:00
|
|
|
REPLEmit('done!');
|
|
|
|
end;
|
|
|
|
Result := THandleCommandResult.Continue;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
|
|
|
end.
|