forked from zamronypj/fano
/
CoreAppImpl.pas
219 lines (194 loc) · 7.7 KB
/
CoreAppImpl.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
{*!
* Fano Web Framework (https://fanoframework.github.io)
*
* @link https://github.com/fanoframework/fano
* @copyright Copyright (c) 2018 Zamrony P. Juhara
* @license https://github.com/fanoframework/fano/blob/master/LICENSE (MIT)
*}
unit CoreAppImpl;
interface
{$MODE OBJFPC}
uses
RunnableIntf,
DependencyContainerIntf,
AppIntf,
DispatcherIntf,
EnvironmentIntf,
ErrorHandlerIntf,
CoreAppConsts;
type
(*!-----------------------------------------------
* Base abstract class that implements IWebApplication
*
* @author Zamrony P. Juhara <zamronypj@yahoo.com>
*-----------------------------------------------*)
TCoreWebApplication = class(TInterfacedObject, IWebApplication, IRunnable)
protected
dependencyContainer : IDependencyContainer;
dispatcher : IDispatcher;
environment : ICGIEnvironment;
errorHandler : IErrorHandler;
(*!-----------------------------------------------
* execute application and write response
*------------------------------------------------
* @return current application instance
*-----------------------------------------------
* TODO: need to think about how to execute when
* application is run as daemon.
*-----------------------------------------------*)
function execute() : IRunnable;
procedure reset();
(*!-----------------------------------------------
* initialize application dependencies
*------------------------------------------------
* @param container dependency container
* @return true if application dependency succesfully
* constructed
*-----------------------------------------------*)
function initialize(const container : IDependencyContainer) : boolean; virtual;
(*!-----------------------------------------------
* Build application route dispatcher
*------------------------------------------------
* @param container dependency container
*-----------------------------------------------*)
procedure buildDispatcher(const container : IDependencyContainer);
(*!-----------------------------------------------
* Build application dependencies
*------------------------------------------------
* @param container dependency container
*-----------------------------------------------*)
procedure buildDependencies(const container : IDependencyContainer); virtual; abstract;
(*!-----------------------------------------------
* Build application routes
*------------------------------------------------
* @param container dependency container
*-----------------------------------------------*)
procedure buildRoutes(const container : IDependencyContainer); virtual; abstract;
(*!-----------------------------------------------
* initialize application route dispatcher
*------------------------------------------------
* @param container dependency container
* @return dispatcher instance
*-----------------------------------------------*)
function initDispatcher(const container : IDependencyContainer) : IDispatcher; virtual; abstract;
public
(*!-----------------------------------------------
* constructor
*------------------------------------------------
* @param container dependency container
* @param env CGI environment instance
* @param errHandler error handler
*-----------------------------------------------*)
constructor create(
const container : IDependencyContainer;
const env : ICGIEnvironment;
const errHandler : IErrorHandler
);
destructor destroy(); override;
function run() : IRunnable; virtual; abstract;
end;
implementation
uses
SysUtils,
ResponseIntf,
///exception-related units
EInvalidDispatcherImpl;
procedure TCoreWebApplication.reset();
begin
dispatcher := nil;
environment := nil;
errorHandler := nil;
dependencyContainer := nil;
end;
(*!-----------------------------------------------
* constructor
*------------------------------------------------
* @param container dependency container
* @param env CGI environment instance
* @param errHandler error handler
*-----------------------------------------------
* errHandler is injected as application dependencies
* instead of using dependency container because
* we need to make sure that during building
* application dependencies and routes, if something
* goes wrong, we can be sure that there is error handler
* to handle the exception
*-----------------------------------------------*)
constructor TCoreWebApplication.create(
const container : IDependencyContainer;
const env : ICGIEnvironment;
const errHandler : IErrorHandler
);
begin
randomize();
reset();
environment := env;
errorHandler := errHandler;
dependencyContainer := container;
end;
(*!-----------------------------------------------
* destructor
*-----------------------------------------------*)
destructor TCoreWebApplication.destroy();
begin
inherited destroy();
reset();
end;
(*!-----------------------------------------------
* Build application route dispatcher
*------------------------------------------------
* @param container dependency container
* @throws EInvalidDispatcher
*-----------------------------------------------
* route dispatcher is essentials but because
* we allow user to use IDispatcher
* implementation they like, we need to be informed
* about it.
*-----------------------------------------------*)
procedure TCoreWebApplication.buildDispatcher(const container : IDependencyContainer);
begin
dispatcher := initDispatcher(container);
if (dispatcher = nil) then
begin
raise EInvalidDispatcher.create(sErrInvalidDispatcher);
end;
end;
(*!-----------------------------------------------
* initialize application dependencies
*------------------------------------------------
* @param container dependency container
* @return true if application dependency succesfully
* constructed
*-----------------------------------------------
* TODO: need to think about how to initialize when
* application is run as daemon. Current implementation
* is we put this in run() method which maybe not right
* place.
*-----------------------------------------------*)
function TCoreWebApplication.initialize(const container : IDependencyContainer) : boolean;
begin
buildDependencies(container);
buildRoutes(container);
buildDispatcher(container);
result := true;
end;
(*!-----------------------------------------------
* execute application and write response
*------------------------------------------------
* @return current application instance
*-----------------------------------------------
* TODO: need to think about how to execute when
* application is run as daemon.
*-----------------------------------------------*)
function TCoreWebApplication.execute() : IRunnable;
var response : IResponse;
begin
response := dispatcher.dispatchRequest(environment);
try
response.write();
result := self;
finally
response := nil;
end;
end;
end.