2015-12-22 12:38:17 +01:00
|
|
|
{***************************************************************************}
|
|
|
|
{ }
|
|
|
|
{ Delphi MVC Framework }
|
|
|
|
{ }
|
|
|
|
{ Copyright (c) 2010-2015 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-04-01 17:01:23 +02:00
|
|
|
unit MVCFramework.SysControllers;
|
|
|
|
|
|
|
|
interface
|
|
|
|
|
|
|
|
uses
|
|
|
|
MVCFramework;
|
|
|
|
|
|
|
|
type
|
|
|
|
|
|
|
|
[MVCPath('/system')]
|
2015-12-16 15:57:20 +01:00
|
|
|
[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')]
|
2015-12-16 15:57:20 +01:00
|
|
|
[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')]
|
2015-12-16 15:57:20 +01:00
|
|
|
[MVCDoc('Describe the system where server is running')]
|
2015-04-01 17:01:23 +02:00
|
|
|
procedure DescribePlatform(Context: TWebContext);
|
|
|
|
|
|
|
|
[MVCPath('/serverconfig.info')]
|
2015-12-16 15:57:20 +01:00
|
|
|
[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));
|
2015-12-16 15:57:20 +01:00
|
|
|
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 := TMVCMimeType.APPLICATION_JSON;
|
|
|
|
Render(LJRes, False);
|
|
|
|
finally
|
|
|
|
LJRes.Free;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TMVCSystemController.DescribeServer(Context: TWebContext);
|
|
|
|
var
|
|
|
|
LJResp: TJSONObject;
|
|
|
|
LController: TMVCControllerClass;
|
|
|
|
ControllerInfo: TJSONObject;
|
|
|
|
LRTTIType: TRttiInstanceType;
|
|
|
|
LCTX: TRttiContext;
|
|
|
|
LAttribute: TCustomattribute;
|
|
|
|
LJMethods: TJSONArray;
|
|
|
|
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;
|
|
|
|
LJMethod: TJSONObject;
|
|
|
|
begin
|
|
|
|
LJResp := TJSONObject.Create;
|
|
|
|
try
|
|
|
|
for LController in GetMVCEngine.RegisteredControllers do
|
|
|
|
begin
|
|
|
|
ControllerInfo := TJSONObject.Create;
|
|
|
|
LJResp.AddPair(LController.QualifiedClassName, ControllerInfo);
|
|
|
|
|
|
|
|
LRTTIType := LCTX.GetType(LController) as TRttiInstanceType;
|
|
|
|
for LAttribute in LRTTIType.GetAttributes do
|
|
|
|
begin
|
|
|
|
if LAttribute is MVCPathAttribute then
|
2015-12-16 15:57:20 +01:00
|
|
|
ControllerInfo.AddPair('resource_path',
|
|
|
|
MVCPathAttribute(LAttribute).Path);
|
|
|
|
if LAttribute is MVCDocAttribute then
|
|
|
|
ControllerInfo.AddPair('description',
|
|
|
|
MVCDocAttribute(LAttribute).Value);
|
2015-04-01 17:01:23 +02:00
|
|
|
end;
|
|
|
|
|
|
|
|
LJMethods := TJSONArray.Create;
|
|
|
|
ControllerInfo.AddPair('actions', LJMethods);
|
|
|
|
LMethods := LRTTIType.GetDeclaredMethods;
|
|
|
|
for LMethod in LMethods do
|
|
|
|
begin
|
|
|
|
LFoundAttrib := False;
|
|
|
|
LStrRelativePath := '';
|
|
|
|
LStrHTTPMethods := '';
|
|
|
|
LStrConsumes := '';
|
|
|
|
LStrProduces := '';
|
2015-12-16 15:57:20 +01:00
|
|
|
LStrHTTPMethods :=
|
|
|
|
'httpGET,httpPOST,httpPUT,httpDELETE,httpHEAD,httpOPTIONS,httpPATCH,httpTRACE';
|
2015-04-01 17:01:23 +02:00
|
|
|
for LAttribute in LMethod.GetAttributes do
|
|
|
|
begin
|
2015-12-16 15:57:20 +01:00
|
|
|
if LAttribute is MVCDocAttribute then
|
|
|
|
begin
|
|
|
|
LStrDoc := MVCDocAttribute(LAttribute).Value;
|
|
|
|
LFoundAttrib := true;
|
|
|
|
end;
|
2015-04-01 17:01:23 +02:00
|
|
|
if LAttribute is MVCPathAttribute then
|
|
|
|
begin
|
|
|
|
LStrRelativePath := MVCPathAttribute(LAttribute).Path;
|
|
|
|
LFoundAttrib := true;
|
|
|
|
end;
|
|
|
|
if LAttribute is MVCHTTPMethodAttribute then
|
|
|
|
begin
|
2015-12-16 15:57:20 +01:00
|
|
|
LStrHTTPMethods := MVCHTTPMethodAttribute(LAttribute)
|
|
|
|
.MVCHTTPMethodsAsString;
|
2015-04-01 17:01:23 +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;
|
|
|
|
end;
|
|
|
|
|
|
|
|
if LFoundAttrib then
|
|
|
|
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);
|
2015-12-16 15:57:20 +01:00
|
|
|
LJMethod.AddPair('description', LStrDoc);
|
2015-04-01 17:01:23 +02:00
|
|
|
LJMethods.AddElement(LJMethod);
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
ContentType := TMVCMimeType.APPLICATION_JSON;
|
|
|
|
Render(LJResp, False);
|
|
|
|
finally
|
|
|
|
LJResp.Free;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
|
|
|
function TMVCSystemController.GetUpTime: string;
|
|
|
|
begin
|
|
|
|
Result := MSecToTime(GetTickCount);
|
|
|
|
end;
|
|
|
|
|
2015-12-16 15:57:20 +01:00
|
|
|
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;
|
2015-12-16 15:57:20 +01:00
|
|
|
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 := TMVCMimeType.APPLICATION_JSON;
|
|
|
|
|
|
|
|
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.
|