delphimvcframework/sources/MVCFramework.REPLCommandsHandlerU.pas

93 lines
2.3 KiB
ObjectPascal
Raw Normal View History

unit MVCFramework.REPLCommandsHandlerU;
interface
uses
IdHTTPWebBrokerBridge,
MVCFramework;
type
{$SCOPEDENUMS ON}
THandleCommandResult = (Continue, Break, Unknown);
TMVCCustomREPLCommandsHandler = reference to function(const Value: string; const Server: TIdHTTPWebBrokerBridge; out Handled: Boolean)
: THandleCommandResult;
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);
implementation
uses
System.SysUtils,
MVCFramework.Commons,
MVCFramework.Logger;
2017-10-16 22:57:27 +02:00
procedure REPLEmit(const Value: string);
begin
2020-11-10 17:15:24 +01:00
Write('#> ');
WriteLn(Value);
end;
function HandleCommand(const Value: string; const Server: TIdHTTPWebBrokerBridge;
const CustomCommandsHandler: TMVCCustomREPLCommandsHandler): THandleCommandResult;
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
EnterInShutdownState;
REPLEmit('Stop listening...');
Server.StopListening;
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);
REPLEmit('done!');
end;
Result := THandleCommandResult.Continue;
end;
end;
end.