-
Notifications
You must be signed in to change notification settings - Fork 3
/
Server.Ignition.pas
131 lines (115 loc) · 2.97 KB
/
Server.Ignition.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
(*
Copyright 2016, MARS-Curiosity - REST Library
Home: https://github.com/andrea-magni/MARS
*)
unit Server.Ignition;
{$I MARS.inc}
interface
uses
Classes, SysUtils, Rtti
, MARS.Core.Engine
;
type
TServerEngine=class
private
class var FEngine: TMARSEngine;
{$IFDEF MARS_FIREDAC}
class var FAvailableConnectionDefs: TArray<string>;
{$ENDIF}
public
class constructor CreateEngine;
class destructor DestroyEngine;
class property Default: TMARSEngine read FEngine;
end;
implementation
uses
MARS.Core.Activation, MARS.Core.Activation.Interfaces
, MARS.Core.Application, MARS.Core.Utils, MARS.Utils.Parameters.IniFile
, MARS.Core.MessageBodyWriter, MARS.Core.MessageBodyWriters
, MARS.Core.MessageBodyReaders, MARS.Data.MessageBodyWriters
{$IFDEF MARS_FIREDAC} , MARS.Data.FireDAC {$ENDIF}
{$IFDEF MSWINDOWS} , MARS.mORMotJWT.Token {$ELSE} , MARS.JOSEJWT.Token {$ENDIF}
, Server.Resources
;
{ TServerEngine }
class constructor TServerEngine.CreateEngine;
begin
FEngine := TMARSEngine.Create;
try
// Engine configuration
FEngine.Parameters.LoadFromIniFile;
// Application configuration
FEngine.AddApplication('DefaultApp', '/api', [ 'Server.Resources.*']);
{$IFDEF MARS_FIREDAC}
FAvailableConnectionDefs := TMARSFireDAC.LoadConnectionDefs(FEngine.Parameters, 'FireDAC');
{$ENDIF}
{$REGION 'OnBeforeHandleRequest example'}
(*
FEngine.OnBeforeHandleRequest :=
function (AEngine: TMARSEngine; AURL: TMARSURL;
ARequest: TWebRequest; AResponse: TWebResponse; var Handled: Boolean
): Boolean
begin
Result := True;
{
// skip favicon requests (browser)
if SameText(AURL.Document, 'favicon.ico') then
begin
Result := False;
Handled := True;
end;
}
{
// Handle CORS and PreFlight
if SameText(ARequest.Method, 'OPTIONS') then
begin
Handled := True;
Result := False;
end;
}
end;
*)
{$ENDREGION}
{$REGION 'Global BeforeInvoke handler example'}
(*
// to execute something before each activation
TMARSActivation.RegisterBeforeInvoke(
procedure (const AActivation: IMARSActivation; out AIsAllowed: Boolean)
begin
end
);
*)
{$ENDREGION}
{$REGION 'Global AfterInvoke handler example'}
(*
// to execute something after each activation
TMARSActivation.RegisterAfterInvoke(
procedure (const AActivation: IMARSActivation)
begin
end
);
*)
{$ENDREGION}
{$REGION 'Global InvokeError handler example'}
(*
// to execute something on error
TMARSActivation.RegisterInvokeError(
procedure (const AActivation: IMARSActivation; const AException: Exception; var AHandled: Boolean)
begin
end
);
*)
{$ENDREGION}
except
FreeAndNil(FEngine);
raise;
end;
end;
class destructor TServerEngine.DestroyEngine;
begin
{$IFDEF MARS_FIREDAC}
TMARSFireDAC.CloseConnectionDefs(FAvailableConnectionDefs);
{$ENDIF}
FreeAndNil(FEngine);
end;
end.