-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWebModule.pas
151 lines (123 loc) · 5.5 KB
/
WebModule.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
unit WebModule;
interface
uses
System.SysUtils, System.Classes, Web.HTTPApp, MVCFramework, System.JSON;
type
TVerifyWebModule = class(TWebModule)
procedure WebModuleCreate(Sender: TObject);
procedure WebModuleDestroy(Sender: TObject);
private
FMVC: TMVCEngine;
public
{ Public declarations }
end;
var
WebModuleClass: TComponentClass = TVerifyWebModule;
implementation
{$R *.dfm}
uses
VerifyController, System.IOUtils, MVCFramework.Commons,
MVCFramework.Middleware.ActiveRecord, MVCFramework.Middleware.StaticFiles,
MVCFramework.Middleware.Analytics, MVCFramework.Middleware.Trace,
MVCFramework.Middleware.CORS, MVCFramework.Middleware.ETag,
MVCFramework.Middleware.Compression;
procedure TVerifyWebModule.WebModuleCreate(Sender: TObject);
var
lExceptionHandler: TMVCExceptionHandlerProc;
begin
lExceptionHandler :=
procedure(E: Exception; SelectedController: TMVCController; WebContext: TWebContext; var ExceptionHandled: Boolean)
procedure CreateResponse(const iErrorCode: Integer; const sErrorName: string);
var
jo: TJSONObject;
begin
WebContext.Response.StatusCode := iErrorCode;
jo := TJSONObject.Create;
try
jo.AddPair('Verify', TJSONBool.Create(False));
jo.AddPair('Error', TJSONString.Create(sErrorName));
//jo.AddPair('Error', TJSONString.Create(IntToStr(iErrorCode) + ': ' + sErrorName));
WebContext.Response.Content := jo.ToString;
finally
jo.Free
end;
end;
begin
WebContext.Response.ContentType := TMVCMediaType.APPLICATION_JSON;
if E is ECustomException then
CreateResponse(ECustomException(E).Code, ECustomException(E).Details)
else if E is EMVCException then
CreateResponse(EMVCException(E).HTTPStatusCode, E.Message)
else
CreateResponse(500, E.Message);
ExceptionHandled := True;
end;
FMVC := TMVCEngine.Create(Self,
procedure(Config: TMVCConfig)
begin
Config.dotEnv := dotEnv;
// session timeout (0 means session cookie)
Config[TMVCConfigKey.SessionTimeout] := dotEnv.Env('dmvc.session_timeout', '0');
//default content-type
Config[TMVCConfigKey.DefaultContentType] := dotEnv.Env('dmvc.default.content_type', TMVCConstants.DEFAULT_CONTENT_TYPE);
//default content charset
Config[TMVCConfigKey.DefaultContentCharset] := dotEnv.Env('dmvc.default.content_charset', TMVCConstants.DEFAULT_CONTENT_CHARSET);
//unhandled actions are permitted?
Config[TMVCConfigKey.AllowUnhandledAction] := dotEnv.Env('dmvc.allow_unhandled_actions', 'false');
//enables or not system controllers loading (available only from localhost requests)
Config[TMVCConfigKey.LoadSystemControllers] := dotEnv.Env('dmvc.load_system_controllers', 'true');
//default view file extension
Config[TMVCConfigKey.DefaultViewFileExtension] := dotEnv.Env('dmvc.default.view_file_extension', 'html');
//view path
Config[TMVCConfigKey.ViewPath] := dotEnv.Env('dmvc.view_path', 'templates');
//use cache for server side views (use "false" in debug and "true" in production for faster performances
Config[TMVCConfigKey.ViewCache] := dotEnv.Env('dmvc.view_cache', 'false');
//Max Record Count for automatic Entities CRUD
Config[TMVCConfigKey.MaxEntitiesRecordCount] := dotEnv.Env('dmvc.max_entities_record_count', IntToStr(TMVCConstants.MAX_RECORD_COUNT));
//Enable Server Signature in response
Config[TMVCConfigKey.ExposeServerSignature] := dotEnv.Env('dmvc.expose_server_signature', 'false');
//Enable X-Powered-By Header in response
Config[TMVCConfigKey.ExposeXPoweredBy] := dotEnv.Env('dmvc.expose_x_powered_by', 'true');
// Max request size in bytes
Config[TMVCConfigKey.MaxRequestSize] := dotEnv.Env('dmvc.max_request_size', IntToStr(TMVCConstants.DEFAULT_MAX_REQUEST_SIZE * 5));
end);
FMVC.AddController(TVerifyController);
FMVC.SetExceptionHandler(lExceptionHandler);
// Analytics middleware generates a csv log, useful to do traffic analysis
//FMVC.AddMiddleware(TMVCAnalyticsMiddleware.Create(GetAnalyticsDefaultLogger));
// The folder mapped as documentroot for TMVCStaticFilesMiddleware must exists!
//FMVC.AddMiddleware(TMVCStaticFilesMiddleware.Create('/static', TPath.Combine(ExtractFilePath(GetModuleName(HInstance)), 'www')));
// Trace middlewares produces a much detailed log for debug purposes
//FMVC.AddMiddleware(TMVCTraceMiddleware.Create);
// CORS middleware handles... well, CORS
//FMVC.AddMiddleware(TMVCCORSMiddleware.Create);
// Simplifies TMVCActiveRecord connection definition
{
FMVC.AddMiddleware(TMVCActiveRecordMiddleware.Create(
dotEnv.Env('firedac.connection_definition_name', 'MyConnDef'),
dotEnv.Env('firedac.connection_definitions_filename', 'FDConnectionDefs.ini')
));
}
// Compression middleware must be the last in the chain, just before the ETag, if present.
//FMVC.AddMiddleware(TMVCCompressionMiddleware.Create);
// ETag middleware must be the latest in the chain
//FMVC.AddMiddleware(TMVCETagMiddleware.Create);
{
FMVC.OnWebContextCreate(
procedure(const Context: TWebContext)
begin
// Initialize services to make them accessibile from Context
// Context.CustomIntfObject := TMyService.Create;
end);
FMVC.OnWebContextDestroy(
procedure(const Context: TWebContext)
begin
//Cleanup services, if needed
end);
}
end;
procedure TVerifyWebModule.WebModuleDestroy(Sender: TObject);
begin
FMVC.Free;
end;
end.