2017-03-01 21:40:57 +01:00
|
|
|
// ***************************************************************************
|
|
|
|
//
|
|
|
|
// Delphi MVC Framework
|
|
|
|
//
|
2023-05-26 18:47:17 +02:00
|
|
|
// Copyright (c) 2010-2023 Daniele Teti and the DMVCFramework Team
|
2017-03-01 21:40:57 +01:00
|
|
|
//
|
|
|
|
// https://github.com/danieleteti/delphimvcframework
|
|
|
|
//
|
|
|
|
// ***************************************************************************
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
//
|
|
|
|
// *************************************************************************** }
|
|
|
|
|
|
|
|
unit SpeedMiddlewareU;
|
|
|
|
|
|
|
|
interface
|
|
|
|
|
|
|
|
uses
|
|
|
|
MVCFramework;
|
|
|
|
|
|
|
|
type
|
|
|
|
TMVCSpeedMiddleware = class(TInterfacedObject, IMVCMiddleware)
|
|
|
|
public
|
|
|
|
procedure OnBeforeRouting(Context: TWebContext; var Handled: Boolean);
|
2021-09-26 18:17:13 +02:00
|
|
|
procedure OnAfterControllerAction(Context: TWebContext;
|
2022-08-13 10:35:53 +02:00
|
|
|
const AControllerQualifiedClassName: string; const AActionNAme: string;
|
|
|
|
const Handled: Boolean);
|
2017-03-01 21:40:57 +01:00
|
|
|
procedure OnBeforeControllerAction(Context: TWebContext;
|
|
|
|
const AControllerQualifiedClassName: string; const AActionNAme: string;
|
|
|
|
var Handled: Boolean);
|
2020-04-29 01:59:41 +02:00
|
|
|
procedure OnAfterRouting(AContext: TWebContext; const AHandled: Boolean);
|
2017-03-01 21:40:57 +01:00
|
|
|
end;
|
|
|
|
|
|
|
|
implementation
|
|
|
|
|
|
|
|
uses
|
2017-05-25 16:57:49 +02:00
|
|
|
MVCFramework.Serializer.Commons, System.SysUtils, DateUtils;
|
2017-03-01 21:40:57 +01:00
|
|
|
|
2021-09-26 18:17:13 +02:00
|
|
|
type
|
|
|
|
ISpeedData = interface
|
|
|
|
['{72B0C1CA-00D1-431F-B093-C91A90147F90}']
|
|
|
|
procedure SetData(value: TDateTime);
|
|
|
|
function GetData: TDateTime;
|
|
|
|
end;
|
|
|
|
|
|
|
|
TSpeedData = class(TInterfacedObject, ISpeedData)
|
|
|
|
private
|
|
|
|
fData: TDateTime;
|
|
|
|
protected
|
|
|
|
procedure SetData(value: TDateTime);
|
|
|
|
function GetData: TDateTime;
|
|
|
|
public
|
|
|
|
destructor Destroy; override;
|
|
|
|
end;
|
2017-03-01 21:40:57 +01:00
|
|
|
|
2021-09-26 18:17:13 +02:00
|
|
|
{ TMVCSpeedMiddleware }
|
|
|
|
|
|
|
|
procedure TMVCSpeedMiddleware.OnAfterControllerAction(Context: TWebContext;
|
2022-08-13 10:35:53 +02:00
|
|
|
const AControllerQualifiedClassName: string; const AActionNAme: string;
|
|
|
|
const Handled: Boolean);
|
2017-03-01 21:40:57 +01:00
|
|
|
begin
|
2021-09-26 18:17:13 +02:00
|
|
|
// Context.Response.CustomHeaders.Values['request_gen_time'] :=
|
|
|
|
// MilliSecondsBetween(Now, ISOTimeStampToDateTime(Context.Data[classname + 'startup'])).ToString
|
2017-03-01 21:40:57 +01:00
|
|
|
Context.Response.CustomHeaders.Values['request_gen_time'] :=
|
2021-09-26 18:17:13 +02:00
|
|
|
MilliSecondsBetween(Now, ISOTimeStampToDateTime(Context.Data['start_req_timestamp'])).ToString;
|
2017-03-01 21:40:57 +01:00
|
|
|
end;
|
|
|
|
|
2021-09-26 18:17:13 +02:00
|
|
|
procedure TMVCSpeedMiddleware.OnAfterRouting(AContext: TWebContext;
|
|
|
|
const AHandled: Boolean);
|
2020-04-29 01:59:41 +02:00
|
|
|
begin
|
|
|
|
|
|
|
|
end;
|
|
|
|
|
2017-03-01 21:40:57 +01:00
|
|
|
procedure TMVCSpeedMiddleware.OnBeforeControllerAction(Context: TWebContext;
|
|
|
|
const AControllerQualifiedClassName, AActionNAme: string;
|
|
|
|
var Handled: Boolean);
|
|
|
|
begin
|
|
|
|
|
|
|
|
end;
|
|
|
|
|
2021-09-26 18:17:13 +02:00
|
|
|
procedure TMVCSpeedMiddleware.OnBeforeRouting(Context: TWebContext;
|
|
|
|
var Handled: Boolean);
|
2017-03-01 21:40:57 +01:00
|
|
|
begin
|
|
|
|
if Context.Request.PathInfo = '/handledbymiddleware' then
|
|
|
|
begin
|
|
|
|
Handled := True;
|
|
|
|
Context.Response.RawWebResponse.Content := 'This is a middleware response';
|
|
|
|
Context.Response.StatusCode := 200;
|
|
|
|
end
|
|
|
|
else
|
2021-09-26 18:17:13 +02:00
|
|
|
begin
|
|
|
|
Context.Data['start_req_timestamp'] := DateTimeToISOTimeStamp(now);
|
|
|
|
end;
|
|
|
|
end;
|
2017-03-01 21:40:57 +01:00
|
|
|
|
2021-09-26 18:17:13 +02:00
|
|
|
{ TSpeedData }
|
|
|
|
|
|
|
|
destructor TSpeedData.Destroy;
|
|
|
|
begin
|
|
|
|
|
|
|
|
inherited;
|
|
|
|
end;
|
|
|
|
|
|
|
|
function TSpeedData.GetData: TDateTime;
|
|
|
|
begin
|
|
|
|
Result := fData;
|
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TSpeedData.SetData(value: TDateTime);
|
|
|
|
begin
|
|
|
|
fData := value;
|
2017-03-01 21:40:57 +01:00
|
|
|
end;
|
|
|
|
|
|
|
|
end.
|