delphimvcframework/sources/MVCFramework.SysControllers.pas

257 lines
7.7 KiB
ObjectPascal
Raw Normal View History

// ***************************************************************************
//
// Delphi MVC Framework
//
// Copyright (c) 2010-2016 Daniele Teti and the DMVCFramework Team
//
// 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;
interface
uses
MVCFramework;
type
[MVCPath('/system')]
[MVCDoc('Built-in DelphiMVCFramework System controller')]
2015-04-01 17:01:23 +02:00
TMVCSystemController = class(TMVCController)
protected
procedure OnBeforeAction(Context: TWebContext; const AActionNAme: string;
var Handled: Boolean); override;
function GetUpTime: string;
public
[MVCPath('/describeserver.info')]
[MVCHTTPMethods([httpGET, httpPOST])]
[MVCDoc('Describe controllers and actions published by the RESTful server per resources')
]
2015-04-01 17:01:23 +02:00
procedure DescribeServer(Context: TWebContext);
[MVCPath('/describeplatform.info')]
[MVCDoc('Describe the system where server is running')]
2015-04-01 17:01:23 +02:00
procedure DescribePlatform(Context: TWebContext);
[MVCPath('/serverconfig.info')]
[MVCDoc('Server configuration')]
2015-04-01 17:01:23 +02:00
procedure ServerConfig(Context: TWebContext);
end;
implementation
uses
{$IF CompilerVErsion < 27}
Data.DBXJSON,
{$ELSE}
System.JSON,
{$IFEND}
System.SysUtils, System.Rtti, MVCFramework.Commons, System.Classes,
Winapi.Windows, System.TypInfo;
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 }
procedure TMVCSystemController.DescribePlatform(Context: TWebContext);
var
LJRes: TJSONObject;
begin
LJRes := TJSONObject.Create;
try
LJRes.AddPair('OS', TOSVersion.ToString);
LJRes.AddPair('CPU_count', TJSONNumber.Create(TThread.ProcessorCount));
LJRes.AddPair('CPU_architecture',
GetEnumName(TypeInfo(TOSVersion.TArchitecture),
2015-04-01 17:01:23 +02:00
Ord(TOSVersion.Architecture)));
LJRes.AddPair('system_uptime', GetUpTime);
ContentType := TMVCMediaType.APPLICATION_JSON;
2015-04-01 17:01:23 +02:00
Render(LJRes, False);
finally
LJRes.Free;
end;
end;
procedure TMVCSystemController.DescribeServer(Context: TWebContext);
var
LJResp: TJSONObject;
LControllerRoutable: TMVCControllerRoutable;
2015-04-01 17:01:23 +02:00
ControllerInfo: TJSONObject;
LRTTIType: TRttiInstanceType;
LCTX: TRttiContext;
LAttribute: TCustomattribute;
LJMethods: TJSONArray;
LMethods: TArray<TRttiMethod>;
LMethod: TRttiMethod;
LFoundAttrib: Boolean;
LStrRelativePath: string;
LStrHTTPMethods: string;
LStrDoc: string;
2015-04-01 17:01:23 +02:00
LStrConsumes: string;
LStrProduces: string;
LJMethod: TJSONObject;
begin
LCTX := TRttiContext.Create;
2015-04-01 17:01:23 +02:00
try
LJResp := TJSONObject.Create;
try
for LControllerRoutable in GetMVCEngine.RegisteredControllers do
2015-04-01 17:01:23 +02:00
begin
ControllerInfo := TJSONObject.Create;
LJResp.AddPair(LControllerRoutable.&Class.QualifiedClassName, ControllerInfo);
2015-04-01 17:01:23 +02:00
LRTTIType := LCTX.GetType(LControllerRoutable.&Class) as TRttiInstanceType;
for LAttribute in LRTTIType.GetAttributes do
2015-04-01 17:01:23 +02:00
begin
if LAttribute is MVCPathAttribute then
ControllerInfo.AddPair('resource_path',
MVCPathAttribute(LAttribute).Path);
if LAttribute is MVCDocAttribute then
ControllerInfo.AddPair('description',
MVCDocAttribute(LAttribute).Value);
end;
LJMethods := TJSONArray.Create;
ControllerInfo.AddPair('actions', LJMethods);
LMethods := LRTTIType.GetDeclaredMethods;
for LMethod in LMethods do
begin
LFoundAttrib := False;
LStrRelativePath := '';
LStrHTTPMethods := '';
LStrConsumes := '';
LStrProduces := '';
LStrDoc := '';
LStrHTTPMethods :=
'httpGET,httpPOST,httpPUT,httpDELETE,httpHEAD,httpOPTIONS,httpPATCH,httpTRACE';
for LAttribute in LMethod.GetAttributes do
2015-04-01 17:01:23 +02:00
begin
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
LStrHTTPMethods := MVCHTTPMethodAttribute(LAttribute)
.MVCHTTPMethodsAsString;
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;
if LFoundAttrib then
2015-04-01 17:01:23 +02:00
begin
LJMethod := TJSONObject.Create;
LJMethod.AddPair('action_name', LMethod.Name);
LJMethod.AddPair('relative_path', LStrRelativePath);
LJMethod.AddPair('consumes', LStrConsumes);
LJMethod.AddPair('produces', LStrProduces);
LJMethod.AddPair('http_methods', LStrHTTPMethods);
LJMethod.AddPair('description', LStrDoc);
LJMethods.AddElement(LJMethod);
2015-04-01 17:01:23 +02:00
end;
end;
end;
ContentType := TMVCMediaType.APPLICATION_JSON;
Render(LJResp, False);
finally
LJResp.Free;
2015-04-01 17:01:23 +02:00
end;
finally
LCTX.Free;
2015-04-01 17:01:23 +02:00
end;
end;
function TMVCSystemController.GetUpTime: string;
begin
Result := MSecToTime(GetTickCount);
end;
procedure TMVCSystemController.OnBeforeAction(Context: TWebContext;
const AActionNAme: string; var Handled: Boolean);
2015-05-18 12:16:34 +02:00
var
LClientIP: string;
2015-04-01 17:01:23 +02:00
begin
inherited;
2015-05-18 12:16:34 +02:00
LClientIP := Context.Request.ClientIP;
Handled := not((LClientIP = '::1') or (LClientIP = '127.0.0.1') or
(LClientIP = '0:0:0:0:0:0:0:1') or (LClientIP.ToLower = 'localhost'));
2015-04-01 17:01:23 +02:00
end;
procedure TMVCSystemController.ServerConfig(Context: TWebContext);
var
LKeys: TArray<string>;
LKey: string;
LJRes: TJSONObject;
begin
ContentType := TMVCMediaType.APPLICATION_JSON;
2015-04-01 17:01:23 +02:00
LJRes := TJSONObject.Create;
try
LKeys := Config.Keys;
for LKey in LKeys do
begin
LJRes.AddPair(LKey, Config[LKey]);
end;
except
LJRes.Free;
raise;
end;
Render(LJRes, true);
end;
end.