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
Result := '';
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, fCode.ValueOrDefault, fCompanyName.ValueOrDefault, fCity, fRating.ValueOrDefault, fNote]);
end;

View File

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

View File

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

View File

@ -1,65 +1,66 @@
unit SSEControllerU;
interface
uses
MVCFramework, MVCFramework.Commons;
type
[MVCPath('/')]
TSSEController = class(TMVCController)
public
[MVCPath('/stocks')]
[MVCHTTPMethod([httpGET])]
[MVCProduces('text/event-stream')]
procedure Index;
protected
procedure OnBeforeAction(Context: TWebContext; const AActionName: string;
var Handled: Boolean); override;
procedure OnAfterAction(Context: TWebContext;
const AActionName: string); override;
end;
implementation
uses
MVCFramework.Logger, System.SysUtils, StorageU;
procedure TSSEController.Index;
var
lLastEventID: Integer;
lCurrentEventID: Integer;
lMessage: string;
begin
// 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);
// 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;
procedure TSSEController.OnAfterAction(Context: TWebContext;
const AActionName: string);
begin
{ Executed after each action }
inherited;
end;
procedure TSSEController.OnBeforeAction(Context: TWebContext;
const AActionName: string; var Handled: Boolean);
begin
{ Executed before each action
if handled is true (or an exception is raised) the actual
action will not be called }
inherited;
end;
interface
uses
MVCFramework, MVCFramework.Commons;
type
[MVCPath('/')]
TSSEController = class(TMVCController)
public
[MVCPath('/stocks')]
[MVCHTTPMethod([httpGET])]
[MVCProduces('text/event-stream')]
procedure Index;
protected
procedure OnBeforeAction(Context: TWebContext; const AActionName: string;
var Handled: Boolean); override;
procedure OnAfterAction(Context: TWebContext;
const AActionName: string); override;
end;
implementation
uses
MVCFramework.Logger, System.SysUtils, StorageU;
procedure TSSEController.Index;
var
lLastEventID: Integer;
lCurrentEventID: Integer;
lMessage: string;
begin
// 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);
// 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;
procedure TSSEController.OnAfterAction(Context: TWebContext;
const AActionName: string);
begin
{ Executed after each action }
inherited;
end;
procedure TSSEController.OnBeforeAction(Context: TWebContext;
const AActionName: string; var Handled: Boolean);
begin
{ Executed before each action
if handled is true (or an exception is raised) the actual
action will not be called }
inherited;
end;
end.

View File

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

View File

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