2016-06-22 17:49:16 +02:00
|
|
|
// ***************************************************************************
|
|
|
|
//
|
|
|
|
// Delphi MVC Framework
|
|
|
|
//
|
2020-01-06 16:49:18 +01:00
|
|
|
// Copyright (c) 2010-2020 Daniele Teti and the DMVCFramework Team
|
2016-06-22 17:49:16 +02: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.
|
|
|
|
//
|
|
|
|
// *************************************************************************** }
|
2015-12-22 12:38:17 +01:00
|
|
|
|
2015-04-01 17:01:23 +02:00
|
|
|
unit MVCFramework.SysControllers;
|
|
|
|
|
2017-01-18 21:53:53 +01:00
|
|
|
{$I dmvcframework.inc}
|
|
|
|
|
2017-03-23 18:51:25 +01:00
|
|
|
interface
|
|
|
|
|
2015-04-01 17:01:23 +02:00
|
|
|
uses
|
2017-03-23 18:51:25 +01:00
|
|
|
System.SysUtils,
|
|
|
|
System.Classes,
|
|
|
|
System.TypInfo,
|
|
|
|
System.DateUtils,
|
|
|
|
System.Rtti,
|
2017-03-13 20:52:11 +01:00
|
|
|
MVCFramework,
|
2019-10-09 23:14:56 +02:00
|
|
|
MVCFramework.Commons, MVCFramework.Swagger.Commons;
|
2015-04-01 17:01:23 +02:00
|
|
|
|
|
|
|
type
|
|
|
|
|
|
|
|
[MVCPath('/system')]
|
2017-03-23 18:51:25 +01:00
|
|
|
[MVCDoc('Built-in DelphiMVCFramework System Controller')]
|
2015-04-01 17:01:23 +02:00
|
|
|
TMVCSystemController = class(TMVCController)
|
2017-03-23 18:51:25 +01:00
|
|
|
private
|
|
|
|
{ private declarations }
|
2015-04-01 17:01:23 +02:00
|
|
|
protected
|
2017-03-23 18:51:25 +01:00
|
|
|
procedure OnBeforeAction(AContext: TWebContext; const AActionName: string; var AHandled: Boolean); override;
|
2015-04-01 17:01:23 +02:00
|
|
|
function GetUpTime: string;
|
|
|
|
public
|
|
|
|
[MVCPath('/describeserver.info')]
|
2019-10-09 23:14:56 +02:00
|
|
|
[MVCHTTPMethods([httpGET])]
|
2017-03-13 20:52:11 +01:00
|
|
|
[MVCDoc('Describe controllers and actions published by the RESTful server per resources')]
|
2019-10-09 23:14:56 +02:00
|
|
|
[MVCSwagSummary('DMVCFramework System Controller', 'Describe controllers and actions published by the RESTful server per resources')]
|
2019-05-02 17:38:57 +02:00
|
|
|
procedure DescribeServer;
|
2015-04-01 17:01:23 +02:00
|
|
|
|
|
|
|
[MVCPath('/describeplatform.info')]
|
2015-12-16 15:57:20 +01:00
|
|
|
[MVCDoc('Describe the system where server is running')]
|
2019-10-09 23:14:56 +02:00
|
|
|
[MVCHTTPMethods([httpGET])]
|
|
|
|
[MVCSwagSummary('DMVCFramework System Controller', 'Describe the system where server is running')]
|
2019-05-02 17:38:57 +02:00
|
|
|
procedure DescribePlatform;
|
2015-04-01 17:01:23 +02:00
|
|
|
|
|
|
|
[MVCPath('/serverconfig.info')]
|
2019-10-09 23:14:56 +02:00
|
|
|
[MVCHTTPMethods([httpGET])]
|
2015-12-16 15:57:20 +01:00
|
|
|
[MVCDoc('Server configuration')]
|
2019-10-09 23:14:56 +02:00
|
|
|
[MVCSwagSummary('DMVCFramework System Controller', 'Server configuration')]
|
2019-05-02 17:38:57 +02:00
|
|
|
procedure ServerConfig;
|
2015-04-01 17:01:23 +02:00
|
|
|
end;
|
|
|
|
|
|
|
|
implementation
|
|
|
|
|
2018-11-24 16:56:21 +01:00
|
|
|
uses
|
|
|
|
JsonDataObjects;
|
|
|
|
|
2015-04-01 17:01:23 +02:00
|
|
|
function MSecToTime(mSec: Int64): string;
|
|
|
|
const
|
|
|
|
secondTicks = 1000;
|
|
|
|
minuteTicks = 1000 * 60;
|
|
|
|
hourTicks = 1000 * 60 * 60;
|
|
|
|
dayTicks = 1000 * 60 * 60 * 24;
|
|
|
|
var
|
|
|
|
D, H, M, S: string;
|
|
|
|
ZD, ZH, ZM, ZS: Integer;
|
|
|
|
begin
|
|
|
|
ZD := mSec div dayTicks;
|
|
|
|
Dec(mSec, ZD * dayTicks);
|
|
|
|
ZH := mSec div hourTicks;
|
|
|
|
Dec(mSec, ZH * hourTicks);
|
|
|
|
ZM := mSec div minuteTicks;
|
|
|
|
Dec(mSec, ZM * minuteTicks);
|
|
|
|
ZS := mSec div secondTicks;
|
|
|
|
D := IntToStr(ZD);
|
|
|
|
H := IntToStr(ZH);
|
|
|
|
M := IntToStr(ZM);
|
|
|
|
S := IntToStr(ZS);
|
|
|
|
Result := D + '.' + H + ':' + M + ':' + S;
|
|
|
|
end;
|
|
|
|
|
|
|
|
{ TMVCSystemController }
|
|
|
|
|
2019-05-02 17:38:57 +02:00
|
|
|
procedure TMVCSystemController.DescribePlatform;
|
2015-04-01 17:01:23 +02:00
|
|
|
var
|
2017-03-23 18:51:25 +01:00
|
|
|
Jo: TJSONObject;
|
2015-04-01 17:01:23 +02:00
|
|
|
begin
|
2017-03-23 18:51:25 +01:00
|
|
|
Jo := TJSONObject.Create;
|
2015-04-01 17:01:23 +02:00
|
|
|
try
|
2018-11-24 16:56:21 +01:00
|
|
|
Jo.S['OS'] := TOSVersion.ToString;
|
|
|
|
Jo.I['CPU_count'] := TThread.ProcessorCount;
|
|
|
|
Jo.S['CPU_architecture'] := GetEnumName(TypeInfo(TOSVersion.TArchitecture), Ord(TOSVersion.Architecture));
|
|
|
|
Jo.DUtc['system_time'] := Now;
|
2016-01-01 23:00:22 +01:00
|
|
|
ContentType := TMVCMediaType.APPLICATION_JSON;
|
2017-05-10 23:28:13 +02:00
|
|
|
Render(Jo, False);
|
2015-04-01 17:01:23 +02:00
|
|
|
finally
|
2017-03-23 18:51:25 +01:00
|
|
|
Jo.Free;
|
2015-04-01 17:01:23 +02:00
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
2019-05-02 17:38:57 +02:00
|
|
|
procedure TMVCSystemController.DescribeServer;
|
2015-04-01 17:01:23 +02:00
|
|
|
var
|
2017-03-23 18:51:25 +01:00
|
|
|
LJoResp: TJSONObject;
|
2017-03-13 20:52:11 +01:00
|
|
|
LController: TMVCControllerDelegate;
|
2017-03-23 18:51:25 +01:00
|
|
|
LRttiType: TRttiInstanceType;
|
|
|
|
LRttiCtx: TRttiContext;
|
2015-04-01 17:01:23 +02:00
|
|
|
LAttribute: TCustomattribute;
|
2017-03-23 18:51:25 +01:00
|
|
|
LJaMethods: TJSONArray;
|
2015-04-01 17:01:23 +02:00
|
|
|
LMethods: TArray<TRttiMethod>;
|
|
|
|
LMethod: TRttiMethod;
|
|
|
|
LFoundAttrib: Boolean;
|
|
|
|
LStrRelativePath: string;
|
|
|
|
LStrHTTPMethods: string;
|
2015-12-16 15:57:20 +01:00
|
|
|
LStrDoc: string;
|
2015-04-01 17:01:23 +02:00
|
|
|
LStrConsumes: string;
|
|
|
|
LStrProduces: string;
|
2017-03-23 18:51:25 +01:00
|
|
|
LJoMethod: TJSONObject;
|
2018-11-24 16:56:21 +01:00
|
|
|
lControllerClassName: string;
|
2015-04-01 17:01:23 +02:00
|
|
|
begin
|
2017-03-23 18:51:25 +01:00
|
|
|
LRttiCtx := TRttiContext.Create;
|
2015-04-01 17:01:23 +02:00
|
|
|
try
|
2017-03-23 18:51:25 +01:00
|
|
|
LJoResp := TJSONObject.Create;
|
2016-06-23 15:18:33 +02:00
|
|
|
try
|
2017-03-13 20:52:11 +01:00
|
|
|
for LController in Engine.Controllers do
|
2015-04-01 17:01:23 +02:00
|
|
|
begin
|
2018-11-24 16:56:21 +01:00
|
|
|
lControllerClassName := LController.Clazz.QualifiedClassName;
|
2017-03-23 18:51:25 +01:00
|
|
|
LRttiType := LRttiCtx.GetType(LController.Clazz) as TRttiInstanceType;
|
|
|
|
for LAttribute in LRttiType.GetAttributes do
|
2015-04-01 17:01:23 +02:00
|
|
|
begin
|
|
|
|
if LAttribute is MVCPathAttribute then
|
2018-11-24 16:56:21 +01:00
|
|
|
LJoResp.O[lControllerClassName].S['resource_path'] := MVCPathAttribute(LAttribute).Path;
|
2016-06-23 15:18:33 +02:00
|
|
|
if LAttribute is MVCDocAttribute then
|
2018-11-24 16:56:21 +01:00
|
|
|
LJoResp.O[lControllerClassName].S['description'] := MVCDocAttribute(LAttribute).Value;
|
2016-06-23 15:18:33 +02:00
|
|
|
end;
|
|
|
|
|
2018-11-24 16:56:21 +01:00
|
|
|
LJaMethods := LJoResp.O[lControllerClassName].A['actions']; // TJSONArray.Create;
|
|
|
|
// LJoControllerInfo.AddPair('actions', LJaMethods);
|
2017-03-23 18:51:25 +01:00
|
|
|
LMethods := LRttiType.GetDeclaredMethods;
|
2016-06-23 15:18:33 +02:00
|
|
|
for LMethod in LMethods do
|
|
|
|
begin
|
|
|
|
LFoundAttrib := False;
|
|
|
|
LStrRelativePath := '';
|
|
|
|
LStrHTTPMethods := '';
|
|
|
|
LStrConsumes := '';
|
|
|
|
LStrProduces := '';
|
2016-10-03 18:18:48 +02:00
|
|
|
LStrDoc := '';
|
2017-03-23 18:51:25 +01:00
|
|
|
LStrHTTPMethods := 'httpGET,httpPOST,httpPUT,httpDELETE,httpHEAD,httpOPTIONS,httpPATCH,httpTRACE';
|
2016-06-23 15:18:33 +02:00
|
|
|
for LAttribute in LMethod.GetAttributes do
|
2015-04-01 17:01:23 +02:00
|
|
|
begin
|
2016-06-23 15:18:33 +02:00
|
|
|
if LAttribute is MVCDocAttribute then
|
|
|
|
begin
|
|
|
|
LStrDoc := MVCDocAttribute(LAttribute).Value;
|
|
|
|
LFoundAttrib := true;
|
|
|
|
end;
|
|
|
|
if LAttribute is MVCPathAttribute then
|
|
|
|
begin
|
|
|
|
LStrRelativePath := MVCPathAttribute(LAttribute).Path;
|
|
|
|
LFoundAttrib := true;
|
|
|
|
end;
|
|
|
|
if LAttribute is MVCHTTPMethodAttribute then
|
|
|
|
begin
|
2017-03-23 18:51:25 +01:00
|
|
|
LStrHTTPMethods := MVCHTTPMethodAttribute(LAttribute).MVCHTTPMethodsAsString;
|
2016-06-23 15:18:33 +02:00
|
|
|
LFoundAttrib := true;
|
|
|
|
end;
|
|
|
|
if LAttribute is MVCConsumesAttribute then
|
|
|
|
begin
|
|
|
|
LStrConsumes := MVCConsumesAttribute(LAttribute).Value;
|
|
|
|
LFoundAttrib := true;
|
|
|
|
end;
|
|
|
|
if LAttribute is MVCProducesAttribute then
|
|
|
|
begin
|
|
|
|
LStrProduces := MVCProducesAttribute(LAttribute).Value;
|
|
|
|
LFoundAttrib := true;
|
|
|
|
end;
|
2015-04-01 17:01:23 +02:00
|
|
|
end;
|
2016-06-23 15:18:33 +02:00
|
|
|
|
|
|
|
if LFoundAttrib then
|
2015-04-01 17:01:23 +02:00
|
|
|
begin
|
2018-11-24 16:56:21 +01:00
|
|
|
LJoMethod := LJaMethods.AddObject;
|
|
|
|
LJoMethod.S['action_name'] := LMethod.Name;
|
|
|
|
LJoMethod.S['relative_path'] := LStrRelativePath;
|
|
|
|
LJoMethod.S['consumes'] := LStrConsumes;
|
|
|
|
LJoMethod.S['produces'] := LStrProduces;
|
|
|
|
LJoMethod.S['http_methods'] := LStrHTTPMethods;
|
|
|
|
LJoMethod.S['description'] := LStrDoc;
|
2015-04-01 17:01:23 +02:00
|
|
|
end;
|
|
|
|
end;
|
|
|
|
end;
|
2016-06-23 15:18:33 +02:00
|
|
|
ContentType := TMVCMediaType.APPLICATION_JSON;
|
2017-05-10 23:28:13 +02:00
|
|
|
Render(LJoResp, False);
|
2016-06-23 15:18:33 +02:00
|
|
|
finally
|
2017-03-23 18:51:25 +01:00
|
|
|
LJoResp.Free;
|
2015-04-01 17:01:23 +02:00
|
|
|
end;
|
|
|
|
finally
|
2017-03-23 18:51:25 +01:00
|
|
|
LRttiCtx.Free;
|
2015-04-01 17:01:23 +02:00
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
|
|
|
function TMVCSystemController.GetUpTime: string;
|
|
|
|
begin
|
2017-03-23 18:51:25 +01:00
|
|
|
Result := MSecToTime(MilliSecondsBetween(Now, 0));
|
2015-04-01 17:01:23 +02:00
|
|
|
end;
|
|
|
|
|
2017-03-23 18:51:25 +01:00
|
|
|
procedure TMVCSystemController.OnBeforeAction(AContext: TWebContext; const AActionName: string; var AHandled: Boolean);
|
2015-05-18 12:16:34 +02:00
|
|
|
var
|
2017-03-23 18:51:25 +01:00
|
|
|
ClientIp: string;
|
2015-04-01 17:01:23 +02:00
|
|
|
begin
|
|
|
|
inherited;
|
2017-03-23 18:51:25 +01:00
|
|
|
ClientIp := Context.Request.ClientIp;
|
2018-11-24 16:56:21 +01:00
|
|
|
AHandled := not((ClientIp = '::1') or (ClientIp = '127.0.0.1') or (ClientIp = '0:0:0:0:0:0:0:1') or
|
|
|
|
(ClientIp.ToLower = 'localhost'));
|
2019-05-02 17:38:57 +02:00
|
|
|
if AHandled then
|
|
|
|
begin
|
|
|
|
AContext.Response.StatusCode := HTTP_STATUS.Forbidden;
|
|
|
|
end;
|
2015-04-01 17:01:23 +02:00
|
|
|
end;
|
|
|
|
|
2019-05-02 17:38:57 +02:00
|
|
|
procedure TMVCSystemController.ServerConfig;
|
2015-04-01 17:01:23 +02:00
|
|
|
var
|
2017-03-23 18:51:25 +01:00
|
|
|
Keys: TArray<string>;
|
|
|
|
Key: string;
|
|
|
|
Jo: TJSONObject;
|
2015-04-01 17:01:23 +02:00
|
|
|
begin
|
2017-03-23 18:51:25 +01:00
|
|
|
Jo := TJSONObject.Create;
|
2015-04-01 17:01:23 +02:00
|
|
|
try
|
2017-03-23 18:51:25 +01:00
|
|
|
Keys := Config.Keys;
|
|
|
|
for Key in Keys do
|
2018-11-24 16:56:21 +01:00
|
|
|
Jo.S[Key] := Config[Key];
|
2017-05-10 23:28:13 +02:00
|
|
|
ContentType := TMVCMediaType.APPLICATION_JSON;
|
|
|
|
Render(Jo, False);
|
2017-02-10 14:19:55 +01:00
|
|
|
finally
|
2017-03-23 18:51:25 +01:00
|
|
|
Jo.Free;
|
2015-04-01 17:01:23 +02:00
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
|
|
|
end.
|