-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathWebModuleUnit.pas
282 lines (256 loc) · 10 KB
/
WebModuleUnit.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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
unit WebModuleUnit;
interface
uses
System.SysUtils, System.Classes, Web.HTTPApp, System.Types;
type
TWebModule1 = class(TWebModule)
procedure WebModule1DefaultHandlerAction(Sender: TObject;
Request: TWebRequest; Response: TWebResponse; var Handled: Boolean);
procedure WebModule1NumberActionAction(Sender: TObject;
Request: TWebRequest; Response: TWebResponse; var Handled: Boolean);
procedure WebModule1InformationAction(Sender: TObject; Request: TWebRequest;
Response: TWebResponse; var Handled: Boolean);
procedure WebModule1GetKeyValueAction(Sender: TObject; Request: TWebRequest;
Response: TWebResponse; var Handled: Boolean);
procedure WebModule1PosttKeyValueAction(Sender: TObject; Request: TWebRequest;
Response: TWebResponse; var Handled: Boolean);
procedure WebModule1PutKeyValueAction(Sender: TObject; Request: TWebRequest;
Response: TWebResponse; var Handled: Boolean);
procedure WebModule1DeleteValueAction(Sender: TObject; Request: TWebRequest;
Response: TWebResponse; var Handled: Boolean);
procedure WebModule1JavaScriptAction(Sender: TObject; Request: TWebRequest;
Response: TWebResponse; var Handled: Boolean);
private
{ Private declarations }
function GetParameters(const aActionPath, aRequestPath: String): TStringDynArray;
public
{ Public declarations }
end;
var
WebModuleClass: TComponentClass = TWebModule1;
implementation
{%CLASSGROUP 'Vcl.Controls.TControl'}
{$R *.dfm}
uses
System.Generics.Collections, System.StrUtils, System.SyncObjs;
Var
// global dictionary variable
gKeyValueStore: TDictionary<String, String>;
career, contactUs, documentation, javascript: string;
gLock: TObject;
{______________________________________________________________________________}
{______________________________________________________________________________}
{______________________________________________________________________________} // Parse Parameters of URL
function TWebModule1.GetParameters(const aActionPath,
aRequestPath: String): TStringDynArray;
Var
IActionPathLength: Integer;
IRequestPathLength: Integer;
IParameter: String;
IParameters: TStringDynArray;
begin
Setlength(Result, 0);
IActionPathLength:= aActionPath.Length;
IRequestPathLength:= aRequestPath.Length;
if (IRequestPathLength > IActionPathLength) then begin
IParameter:= RightStr(aRequestPath, IRequestPathLength - IActionPathLength);
IParameters:= SplitString(IParameter, '/');
if (Length(IParameter) > 0) then
Result:= IParameters;
end;
end;
{______________________________________________________________________________} // Get Request Executor - Default
procedure TWebModule1.WebModule1DefaultHandlerAction(Sender: TObject;
Request: TWebRequest; Response: TWebResponse; var Handled: Boolean);
begin
Response.ContentType := 'text/html; charset=utf-8';
Response.Content :=
'<html>' +
'<head><title>Web Server Application</title></head>' +
'<body>Web Server is up...</body>' +
'</html>';
end;
{______________________________________________________________________________} // DELETE Request Executor
procedure TWebModule1.WebModule1DeleteValueAction(Sender: TObject;
Request: TWebRequest; Response: TWebResponse; var Handled: Boolean);
Var
IParameters: TStringDynArray;
IKey: String;
begin
IParameters:= GetParameters((Sender as TWebActionItem).PathInfo, Request.PathInfo);
if (Length(IParameters) > 0) then begin
IKey := IParameters[0];
Response.ContentType := 'application/json; charset=utf-8';
gKeyValueStore.Remove(IKey);
Response.Content := '{"result":"success"}';
Handled := True;
end else Handled := False;
end;
{______________________________________________________________________________} // GET Request Executor.
procedure TWebModule1.WebModule1GetKeyValueAction(Sender: TObject;
Request: TWebRequest; Response: TWebResponse; var Handled: Boolean);
Var
IParameters: TStringDynArray;
IKey: String;
IValue: String;
begin
IKey:='';
{ URL: http://localhost:8080/KeyValue/0 }
IParameters:= GetParameters((Sender as TWebActionItem).PathInfo, Request.PathInfo);
if Length(IParameters) > 0 then
IKey:=IParameters[0];
if not (IKey.IsEmpty) then begin
Response.ContentType := 'application/json; charset=utf-8';
gKeyValueStore.TryGetValue(IKey, IValue);
if not (IValue.IsEmpty) then
Response.Content := '{"result":['+IValue+']}' else
Response.Content := '{"error":"item not found"}';
Handled:= true;
end else Handled:= false;
end;
{______________________________________________________________________________} // POST Request Executor.
procedure TWebModule1.WebModule1PosttKeyValueAction(Sender: TObject;
Request: TWebRequest; Response: TWebResponse; var Handled: Boolean);
Var
IParameters: TStringDynArray;
IKey: String;
IValue: String;
begin
IParameters:= GetParameters((Sender as TWebActionItem).PathInfo, Request.PathInfo);
if Length(IParameters) > 0 then begin
IKey := IParameters[0];
IValue :='';
if Length(IParameters) > 1 then
IValue := IParameters[1] else
IValue := Request.Content;
if not (IValue.IsEmpty) then begin
Response.ContentType := 'application/json; charset=utf-8';
if gKeyValueStore.ContainsKey(IKey) then begin
gKeyValueStore[IKey] := IValue; // See difference in PUT
Response.Content := '{"result":"success"}';
end else
Response.Content := '{"error":"item not found"}';
Handled := True;
end else Handled := False;
end;
end;
{______________________________________________________________________________} // PUT Request Executor.
procedure TWebModule1.WebModule1PutKeyValueAction(Sender: TObject;
Request: TWebRequest; Response: TWebResponse; var Handled: Boolean);
Var
IParameters: TStringDynArray;
IKey: String;
IValue: String;
begin
IParameters:= GetParameters((Sender as TWebActionItem).PathInfo, Request.PathInfo);
if Length(IParameters) > 0 then begin
IKey := IParameters[0];
IValue :='';
if Length(IParameters) > 1 then
IValue := IParameters[1] else
IValue := Request.Content;
if not (IValue.IsEmpty) then begin
Response.ContentType := 'application/json; charset='+TEncoding.UTF8.MIMEName;
// apply gLock
if TMonitor.Enter(gLock, 500) then begin
try
gKeyValueStore.AddOrSetValue(IKey, IValue);
finally
TMonitor.Exit(gLock);
end;
end;
Response.Content := '{"result":"success"}';
Handled := True;
end else Handled := False;
end;
end;
{______________________________________________________________________________}
procedure TWebModule1.WebModule1InformationAction(Sender: TObject;
Request: TWebRequest; Response: TWebResponse; var Handled: Boolean);
begin
Response.ContentType := 'text/html; charset=utf-8';
Response.Content :=
'<html>'+
'<header><h2>Your Personal Information</h2></html>'+
'<body>'+
'<p>Detailed summary of your personal information</p>'+
'<ul>'+
'<li>Name: Isuru Sathsara</li>'+
'<li>Age : 24 years</li>'+
'<li>Job Title: Associate Software Engineer</li>'+
'<li>Company: ITelligence Services Private Limited, Kirulapone</li>'+
'</ul>'+
'<footer> All rights received @2022 </footer>'+
'</body>'+
'</html>';
end;
procedure TWebModule1.WebModule1JavaScriptAction(Sender: TObject;
Request: TWebRequest; Response: TWebResponse; var Handled: Boolean);
begin
end;
{_____________________________________________________________________________}
procedure TWebModule1.WebModule1NumberActionAction(Sender: TObject;
Request: TWebRequest; Response: TWebResponse; var Handled: Boolean);
begin
Response.ContentType := 'application/json; charset=utf-8';
Response.Content:= Random(100).toString;
end;
{______________________________________________________________________________}
{______________________________________________________________________________}
{______________________________________________________________________________}
initialization // Global Initializations
career :=
'<html>'+
'<header><h2>Get to know about us...</h2></html>'+
'<body>'+
'<p>We are outstanding software development company in Sri Lanka, which outsource products to European region.</p>'+
'<p>Your Requirements as follows'+
'<ul>'+
'<li>Solid Knowlegde on Delphi</li>'+
'<li>Strong knowledge on FireDAC, InterBase, SQLite</li>'+
'<li>Hands-on Experience on Laravel Framework</li>'+
'<li>Knowledge on AWS and Azure is plus point</li>'+
'</ul>'+
'<footer> All rights received @2022 ITelligence Services Pvt Ltd</footer>'+
'</body>'+
'</html>';
contactUs :=
'<html>'+
'<header><h2>Contact Us</h2></html>'+
'<body>'+
'<p>Hire Any Developer? Just Hangout with Us!</p>'+
'<p>Our Contacts'+
'<ul>'+
'<li>Address: No 115, Maya Avenue, Kirulapone, Colombo 05</li>'+
'<li>Email: [email protected]</li>'+
'</ul>'+
'<p>Write Us'+
'<form>'+
'<label>Name: </label><input type="text"><br>'+
'<label>Email: </label><input type="text"/><br>'+
'<label>Descr: </label><input type="text"/><br>'+
'<input type="submit">'+
'</form>'+
'<footer> All rights received @2022 ITelligence Services Pvt Ltd</footer>'+
'</body>'+
'</html>';
javascript:=
'<html>'+
'<header><h2>Call Number with JavaScript</h2></header>'+
'<body>'+
'<button onclick="getNumber()">Get Number in Console log (view Ctrl+Shift+I)</button>'+
'<script type="text/javascript">'+
'function getNumber() {'+
' let url = ''http://localhost:8080/Number'';'+
' fetch(url).then(resp=> resp.json().then(j=> console.log(''\nNumber:'',j)))'+
'}'+
'</script>'+
'</body>'+
'</html>';
// lock the dictionary, avoid deadlock kind of situations...
gLock := TObject.Create;
gKeyValueStore := TDictionary<String, String>.Create;
gKeyValueStore.Add('0','Zero');
gKeyValueStore.Add('career', career);
gKeyValueStore.Add('contact',contactUs);
end.