Daniele Teti 2022-12-09 09:58:55 +01:00
parent e8f611c1b6
commit dad88f85b6
6 changed files with 110 additions and 97 deletions

View File

@ -594,7 +594,9 @@ function TCustomer.ToString: String;
begin begin
Result := ''; Result := '';
if PKIsNull then if PKIsNull then
Result := '<null>'; Result := '<null>'
else
Result := fID.ValueOrDefault.ToString;
Result := Format('[ID: %6s][CODE: %6s][CompanyName: %18s][City: %16s][Rating: %3d][Note: %s]',[ Result := Format('[ID: %6s][CODE: %6s][CompanyName: %18s][City: %16s][Rating: %3d][Note: %s]',[
Result, fCode.ValueOrDefault, fCompanyName.ValueOrDefault, fCity, fRating.ValueOrDefault, fNote]); Result, fCode.ValueOrDefault, fCompanyName.ValueOrDefault, fCity, fRating.ValueOrDefault, fNote]);
end; end;

View File

@ -511,24 +511,24 @@ begin
end, fGeneralErrorHandler); end, fGeneralErrorHandler);
end; end;
procedure TMainForm.btnSubtractClick(Sender: TObject); procedure TMainForm.btnSubtractClick(Sender: TObject);
var var
lReq: IJSONRPCRequest; lReq: IJSONRPCRequest;
lExecutorAsync: IMVCJSONRPCExecutorAsync; lExecutorAsync: IMVCJSONRPCExecutorAsync;
begin begin
lExecutorAsync := TMVCJSONRPCExecutor.Create('http://localhost:8080'); lExecutorAsync := TMVCJSONRPCExecutor.Create('http://localhost:8080');
lReq := TJSONRPCRequest.Create; lReq := TJSONRPCRequest.Create;
lReq.Method := 'subtract'; lReq.Method := 'subtract';
lReq.RequestID := Random(1000); lReq.RequestID := Random(1000);
lReq.Params.Add(StrToInt(edtValue1.Text)); lReq.Params.Add(StrToInt(edtValue1.Text));
lReq.Params.Add(StrToInt(edtValue2.Text)); lReq.Params.Add(StrToInt(edtValue2.Text));
lExecutorAsync lExecutorAsync
.ExecuteRequestAsync('/jsonrpc', lReq, .ExecuteRequestAsync('/jsonrpc', lReq,
procedure(JSONRPCResp: IJSONRPCResponse) procedure(JSONRPCResp: IJSONRPCResponse)
begin begin
edtResult.Text := JSONRPCResp.Result.AsInteger.ToString; edtResult.Text := JSONRPCResp.Result.AsInteger.ToString;
end); end);
end; end;
procedure TMainForm.btnSubtractWithNamedParamsClick(Sender: TObject); procedure TMainForm.btnSubtractWithNamedParamsClick(Sender: TObject);
var var

View File

@ -82,16 +82,18 @@ begin
.AddController(TAdminController) .AddController(TAdminController)
.AddMiddleware( .AddMiddleware(
TMVCJWTAuthenticationMiddleware.Create( TMVCJWTAuthenticationMiddleware.Create(
TAuthenticationSample.Create, TAuthenticationSample.Create,
lClaimsSetup, lClaimsSetup,
'mys3cr37', 'mys3cr37',
'/login', '/login',
[ [
TJWTCheckableClaim.ExpirationTime, TJWTCheckableClaim.ExpirationTime,
TJWTCheckableClaim.NotBefore, TJWTCheckableClaim.NotBefore,
TJWTCheckableClaim.IssuedAt TJWTCheckableClaim.IssuedAt
], 300)) ], 300))
.AddMiddleware(TMVCJWTBlackListMiddleware.Create(lOnAcceptToken, lOnNewJWTToBlackList)); .AddMiddleware(
TMVCJWTBlackListMiddleware.Create(lOnAcceptToken, lOnNewJWTToBlackList)
);
end; end;
end. end.

View File

@ -1,65 +1,66 @@
unit SSEControllerU; unit SSEControllerU;
interface
interface
uses
MVCFramework, MVCFramework.Commons; uses
MVCFramework, MVCFramework.Commons;
type
type
[MVCPath('/')]
TSSEController = class(TMVCController) [MVCPath('/')]
public TSSEController = class(TMVCController)
[MVCPath('/stocks')] public
[MVCHTTPMethod([httpGET])] [MVCPath('/stocks')]
[MVCProduces('text/event-stream')] [MVCHTTPMethod([httpGET])]
procedure Index; [MVCProduces('text/event-stream')]
procedure Index;
protected
procedure OnBeforeAction(Context: TWebContext; const AActionName: string; protected
var Handled: Boolean); override; procedure OnBeforeAction(Context: TWebContext; const AActionName: string;
procedure OnAfterAction(Context: TWebContext; var Handled: Boolean); override;
const AActionName: string); override; procedure OnAfterAction(Context: TWebContext;
end; const AActionName: string); override;
end;
implementation
implementation
uses
MVCFramework.Logger, System.SysUtils, StorageU; uses
MVCFramework.Logger, System.SysUtils, StorageU;
procedure TSSEController.Index;
var procedure TSSEController.Index;
lLastEventID: Integer; var
lCurrentEventID: Integer; lLastEventID: Integer;
lMessage: string; lCurrentEventID: Integer;
begin lMessage: string;
// wait a little bit begin
Sleep(1000 + Random(2000)); // wait a little bit
Sleep(1000 + Random(2000));
// retrieve the last id received by the client reading the request header.
lLastEventID := StrToIntDef(Context.Request.Headers[TMVCConstants.SSE_LAST_EVENT_ID], 0); // retrieve the last id received by the client reading the request header.
lLastEventID := StrToIntDef(Context.Request.Headers[TMVCConstants.SSE_LAST_EVENT_ID], 0);
// get the next message to send based on the last id already received by the client
lMessage := GetNextDataToSend(lLastEventID, lCurrentEventID); // get the next message to send based on the last id already received by the client
lMessage := GetNextDataToSend(lLastEventID, lCurrentEventID);
RenderSSE(lCurrentEventID.ToString, lMessage, 'stockupdate');
end; RenderSSE(lCurrentEventID.ToString, lMessage, 'stockupdate');
end;
procedure TSSEController.OnAfterAction(Context: TWebContext;
const AActionName: string); procedure TSSEController.OnAfterAction(Context: TWebContext;
begin const AActionName: string);
{ Executed after each action } begin
inherited; { Executed after each action }
end; inherited;
end;
procedure TSSEController.OnBeforeAction(Context: TWebContext;
const AActionName: string; var Handled: Boolean); procedure TSSEController.OnBeforeAction(Context: TWebContext;
begin const AActionName: string; var Handled: Boolean);
{ Executed before each action begin
if handled is true (or an exception is raised) the actual { Executed before each action
action will not be called } if handled is true (or an exception is raised) the actual
inherited; action will not be called }
end; inherited;
end;
end. end.

View File

@ -33,7 +33,9 @@ end;
procedure TMyController.DoSum(const a, b: Integer); procedure TMyController.DoSum(const a, b: Integer);
begin begin
Render((Context.CustomIntfObject as ICalculator).DoCalc(a,b).ToString); var lSvc := Context.CustomIntfObject as ICalculator;
Render(lSvc.DoCalc(a,b).ToString);
end; end;
end. end.

View File

@ -71,6 +71,8 @@ type
AContext: TWebContext; AContext: TWebContext;
const AHandled: Boolean); const AHandled: Boolean);
public public
constructor Create(
const DefaultConnectionDefName: string); overload; virtual;
constructor Create( constructor Create(
const DefaultConnectionDefName: string; const DefaultConnectionDefName: string;
const ConnectionDefFileName: string{ = 'FDConnectionDefs.ini'}); overload; virtual; const ConnectionDefFileName: string{ = 'FDConnectionDefs.ini'}); overload; virtual;
@ -96,10 +98,7 @@ var
constructor TMVCActiveRecordMiddleware.Create(const DefaultConnectionDefName: string; constructor TMVCActiveRecordMiddleware.Create(const DefaultConnectionDefName: string;
const ConnectionDefFileName: string); const ConnectionDefFileName: string);
begin begin
inherited Create; Create(DefaultConnectionDefName, [], [], ConnectionDefFileName);
fConnectionLoaded := False;
fDefaultConnectionDefName := DefaultConnectionDefName;
fConnectionDefFileName := ConnectionDefFileName;
end; end;
constructor TMVCActiveRecordMiddleware.Create( constructor TMVCActiveRecordMiddleware.Create(
@ -107,6 +106,7 @@ constructor TMVCActiveRecordMiddleware.Create(
const AdditionalARConnectionNames, AdditionalConnectionDefNames: TArray<String>; const AdditionalARConnectionNames, AdditionalConnectionDefNames: TArray<String>;
const ConnectionDefFileName: string); const ConnectionDefFileName: string);
begin begin
inherited Create;
fConnectionLoaded := False; fConnectionLoaded := False;
fDefaultConnectionDefName := DefaultConnectionDefName; fDefaultConnectionDefName := DefaultConnectionDefName;
fConnectionDefFileName := ConnectionDefFileName; fConnectionDefFileName := ConnectionDefFileName;
@ -114,6 +114,12 @@ begin
fAdditionalConnectionDefNames := AdditionalConnectionDefNames; fAdditionalConnectionDefNames := AdditionalConnectionDefNames;
end; end;
constructor TMVCActiveRecordMiddleware.Create(
const DefaultConnectionDefName: string);
begin
Create(DefaultConnectionDefName, 'FDConnectionDefs.ini');
end;
procedure TMVCActiveRecordMiddleware.EnsureConnection; procedure TMVCActiveRecordMiddleware.EnsureConnection;
var var
I: Integer; I: Integer;