2016-06-22 17:49:16 +02:00
|
|
|
// ***************************************************************************
|
|
|
|
//
|
|
|
|
// Delphi MVC Framework
|
|
|
|
//
|
2017-01-05 12:44:34 +01:00
|
|
|
// Copyright (c) 2010-2017 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,
|
2017-03-23 18:51:25 +01:00
|
|
|
MVCFramework.Commons,
|
|
|
|
MVCFramework.TypesAliases;
|
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')]
|
2015-12-16 15:57:20 +01:00
|
|
|
[MVCHTTPMethods([httpGET, httpPOST])]
|
2017-03-13 20:52:11 +01:00
|
|
|
[MVCDoc('Describe controllers and actions published by the RESTful server per resources')]
|
2017-03-23 18:51:25 +01:00
|
|
|
procedure DescribeServer(AContext: TWebContext);
|
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')]
|
2017-03-23 18:51:25 +01:00
|
|
|
procedure DescribePlatform(AContext: TWebContext);
|
2015-04-01 17:01:23 +02:00
|
|
|
|
|
|
|
[MVCPath('/serverconfig.info')]
|
2015-12-16 15:57:20 +01:00
|
|
|
[MVCDoc('Server configuration')]
|
2017-03-23 18:51:25 +01:00
|
|
|
procedure ServerConfig(AContext: TWebContext);
|
2015-04-01 17:01:23 +02:00
|
|
|
end;
|
|
|
|
|
|
|
|
implementation
|
|
|
|
|
|
|
|
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 }
|
|
|
|
|
2017-03-23 18:51:25 +01:00
|
|
|
procedure TMVCSystemController.DescribePlatform(AContext: TWebContext);
|
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
|
2017-03-23 18:51:25 +01:00
|
|
|
Jo.AddPair('OS', TOSVersion.ToString);
|
|
|
|
Jo.AddPair('CPU_count', TJSONNumber.Create(TThread.ProcessorCount));
|
|
|
|
Jo.AddPair('CPU_architecture', GetEnumName(TypeInfo(TOSVersion.TArchitecture), Ord(TOSVersion.Architecture)));
|
|
|
|
Jo.AddPair('system_time', FormatDateTime('YYYY-MM-DD HH:NN:SS', Now));
|
2016-01-01 23:00:22 +01:00
|
|
|
ContentType := TMVCMediaType.APPLICATION_JSON;
|
2017-03-23 18:51:25 +01:00
|
|
|
Serializer.SerializeObject(Jo);
|
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;
|
|
|
|
|
2017-03-23 18:51:25 +01:00
|
|
|
procedure TMVCSystemController.DescribeServer(AContext: TWebContext);
|
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
|
|
|
LJoControllerInfo: TJSONObject;
|
|
|
|
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;
|
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
|
2017-03-23 18:51:25 +01:00
|
|
|
LJoControllerInfo := TJSONObject.Create;
|
|
|
|
LJoResp.AddPair(LController.Clazz.QualifiedClassName, LJoControllerInfo);
|
2015-04-01 17:01:23 +02:00
|
|
|
|
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
|
2017-03-23 18:51:25 +01:00
|
|
|
LJoControllerInfo.AddPair('resource_path', MVCPathAttribute(LAttribute).Path);
|
2016-06-23 15:18:33 +02:00
|
|
|
if LAttribute is MVCDocAttribute then
|
2017-03-23 18:51:25 +01:00
|
|
|
LJoControllerInfo.AddPair('description', MVCDocAttribute(LAttribute).Value);
|
2016-06-23 15:18:33 +02:00
|
|
|
end;
|
|
|
|
|
2017-03-23 18:51:25 +01:00
|
|
|
LJaMethods := TJSONArray.Create;
|
|
|
|
LJoControllerInfo.AddPair('actions', LJaMethods);
|
|
|
|
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
|
2017-03-23 18:51:25 +01:00
|
|
|
LJoMethod := TJSONObject.Create;
|
|
|
|
LJoMethod.AddPair('action_name', LMethod.Name);
|
|
|
|
LJoMethod.AddPair('relative_path', LStrRelativePath);
|
|
|
|
LJoMethod.AddPair('consumes', LStrConsumes);
|
|
|
|
LJoMethod.AddPair('produces', LStrProduces);
|
|
|
|
LJoMethod.AddPair('http_methods', LStrHTTPMethods);
|
|
|
|
LJoMethod.AddPair('description', LStrDoc);
|
|
|
|
LJaMethods.AddElement(LJoMethod);
|
2015-04-01 17:01:23 +02:00
|
|
|
end;
|
|
|
|
end;
|
|
|
|
end;
|
2016-06-23 15:18:33 +02:00
|
|
|
ContentType := TMVCMediaType.APPLICATION_JSON;
|
2017-03-23 18:51:25 +01:00
|
|
|
Serializer.SerializeObject(LJoResp);
|
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;
|
|
|
|
AHandled := not((ClientIp = '::1') or (ClientIp = '127.0.0.1') or (ClientIp = '0:0:0:0:0:0:0:1') or (ClientIp.ToLower = 'localhost'));
|
2015-04-01 17:01:23 +02:00
|
|
|
end;
|
|
|
|
|
2017-03-23 18:51:25 +01:00
|
|
|
procedure TMVCSystemController.ServerConfig(AContext: TWebContext);
|
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
|
|
|
|
Jo.AddPair(Key, Config[Key]);
|
|
|
|
Serializer.SerializeObject(Jo);
|
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.
|