-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDelphiAPIStarterKit.dpr
More file actions
242 lines (223 loc) · 7.19 KB
/
Copy pathDelphiAPIStarterKit.dpr
File metadata and controls
242 lines (223 loc) · 7.19 KB
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
program DelphiAPIStarterKit;
{$APPTYPE CONSOLE}
uses
System.SysUtils,
System.Types,
IPPeerServer,
IPPeerAPI,
IdHTTPWebBrokerBridge,
Web.WebReq,
Web.WebBroker,
App.WebModule in 'sources\app\App.WebModule.pas' {WM: TWebModule},
BFA.Core.Constants in 'sources\core\BFA.Core.Constants.pas',
BFA.Helper.Strings in 'sources\shared\helpers\BFA.Helper.Strings.pas',
BFA.Helper.Validator in 'sources\shared\helpers\BFA.Helper.Validator.pas',
BFA.Helper.Transaction in 'sources\shared\helpers\BFA.Helper.Transaction.pas',
uDM in 'uDM.pas' {DM: TDataModule},
BFA.Core.Request in 'sources\core\BFA.Core.Request.pas',
RestAPI.Sample in 'sources\modules\sample\RestAPI.Sample.pas',
RestAPI.Auth in 'sources\modules\auth\RestAPI.Auth.pas',
BFA.Helper.Dataset in 'sources\shared\helpers\BFA.Helper.Dataset.pas',
BFA.Core.Messages in 'sources\core\BFA.Core.Messages.pas',
BFA.Core.Response in 'sources\core\BFA.Core.Response.pas',
BFA.Core.Rest in 'sources\core\BFA.Core.Rest.pas',
BFA.Core.Endpoint in 'sources\core\BFA.Core.Endpoint.pas',
BFA.Core.Config in 'sources\core\BFA.Core.Config.pas',
RestAPI.User in 'sources\modules\users\RestAPI.User.pas',
DB.ConnectionFactory in 'sources\infrastructure\database\DB.ConnectionFactory.pas',
DB.Helper.Query in 'sources\infrastructure\database\DB.Helper.Query.pas',
Auth.DTO in 'sources\modules\auth\Auth.DTO.pas',
Auth.Repository in 'sources\modules\auth\Auth.Repository.pas',
Auth.Service in 'sources\modules\auth\Auth.Service.pas',
Auth.Validator in 'sources\modules\auth\Auth.Validator.pas',
User.DTO in 'sources\modules\users\User.DTO.pas',
User.Repository in 'sources\modules\users\User.Repository.pas',
User.Service in 'sources\modules\users\User.Service.pas',
User.Validator in 'sources\modules\users\User.Validator.pas',
RestAPI.Product in 'sources\modules\products\RestAPI.Product.pas',
Product.DTO in 'sources\modules\products\Product.DTO.pas',
Product.Repository in 'sources\modules\products\Product.Repository.pas',
Product.Service in 'sources\modules\products\Product.Service.pas',
Product.Validator in 'sources\modules\products\Product.Validator.pas',
RestAPI.Category in 'sources\modules\category\RestAPI.Category.pas',
Category.DTO in 'sources\modules\category\Category.DTO.pas',
Category.Repository in 'sources\modules\category\Category.Repository.pas',
Category.Service in 'sources\modules\category\Category.Service.pas',
Category.Validator in 'sources\modules\category\Category.Validator.pas',
RestAPI.Customer in 'sources\modules\customers\RestAPI.Customer.pas',
Customer.DTO in 'sources\modules\customers\Customer.DTO.pas',
Customer.Repository in 'sources\modules\customers\Customer.Repository.pas',
Customer.Service in 'sources\modules\customers\Customer.Service.pas',
Customer.Validator in 'sources\modules\customers\Customer.Validator.pas',
BFA.Core.Helper in 'sources\core\BFA.Core.Helper.pas',
BFA.Security.Token in 'sources\infrastructure\security\BFA.Security.Token.pas';
{$R *.res}
procedure TerminateThreads;
begin
end;
function BindPort(APort: Integer): Boolean;
var
LTestServer: IIPTestServer;
begin
Result := True;
try
LTestServer := PeerFactory.CreatePeer('', IIPTestServer) as IIPTestServer;
LTestServer.TestOpenPort(APort, nil);
except
Result := False;
end;
end;
function CheckPort(APort: Integer): Integer;
begin
if BindPort(APort) then
Result := APort
else
Result := 0;
end;
procedure SetPort(const AServer: TIdHTTPWebBrokerBridge; APort: String);
begin
if not AServer.Active then
begin
APort := APort.Replace(cCommandSetPort, '').Trim;
if CheckPort(APort.ToInteger) > 0 then
begin
AServer.DefaultPort := APort.ToInteger;
Writeln(Format(sPortSet, [APort]));
end
else
Writeln(Format(sPortInUse, [APort]));
end
else
Writeln(sServerRunning);
Write(cArrow);
end;
procedure StartServer(const AServer: TIdHTTPWebBrokerBridge);
begin
if not AServer.Active then
begin
if CheckPort(AServer.DefaultPort) > 0 then
begin
Writeln(Format(sStartingServer, [AServer.DefaultPort]));
AServer.Bindings.Clear;
AServer.Active := True;
end
else
Writeln(Format(sPortInUse, [AServer.DefaultPort.ToString]));
end
else
Writeln(sServerRunning);
Write(cArrow);
end;
procedure StopServer(const AServer: TIdHTTPWebBrokerBridge);
begin
if AServer.Active then
begin
Writeln(sStoppingServer);
TerminateThreads;
AServer.Active := False;
AServer.Bindings.Clear;
Writeln(sServerStopped);
end
else
Writeln(sServerNotRunning);
Write(cArrow);
end;
procedure WriteCommands;
begin
Writeln(sCommands);
Write(cArrow);
end;
procedure WriteStatus(const AServer: TIdHTTPWebBrokerBridge);
begin
Writeln(sIndyVersion + AServer.SessionList.Version);
Writeln(sActive + AServer.Active.ToString(TUseBoolStrs.True));
Writeln(sPort + AServer.DefaultPort.ToString);
Writeln(sSessionID + AServer.SessionIDCookieName);
Write(cArrow);
end;
//procedure RunServer(APort: Integer);
//var
// LServer: TIdHTTPWebBrokerBridge;
// LResponse: string;
//begin
// WriteCommands;
// LServer := TIdHTTPWebBrokerBridge.Create(nil);
// try
// LServer.DefaultPort := APort;
// while True do
// begin
// Readln(LResponse);
// LResponse := LowerCase(LResponse);
// if LResponse.StartsWith(cCommandSetPort) then
// SetPort(LServer, LResponse)
// else if sametext(LResponse, cCommandStart) then
// StartServer(LServer)
// else if sametext(LResponse, cCommandStatus) then
// WriteStatus(LServer)
// else if sametext(LResponse, cCommandStop) then
// StopServer(LServer)
// else if sametext(LResponse, cCommandHelp) then
// WriteCommands
// else if sametext(LResponse, cCommandExit) then
// if LServer.Active then
// begin
// StopServer(LServer);
// break
// end
// else
// break
// else
// begin
// Writeln(sInvalidCommand);
// Write(cArrow);
// end;
// end;
// TerminateThreads();
// finally
// LServer.Free;
// end;
//end;
procedure RunServer(APort: Integer);
var
LServer: TIdHTTPWebBrokerBridge;
// LServer : TsgcWSHTTPWebBrokerBridgeServer;
begin
LServer := TIdHTTPWebBrokerBridge.Create(nil);
DM := TDM.Create(nil);
try
LServer.DefaultPort := APort;
if CheckPort(APort) = 0 then
raise Exception.CreateFmt('Port %d already in use', [APort]);
try
TGlobalFunction.LoadFile('');
{$IF DEFINED (LINUX)}
DM.FDPhysMySQLDriverLink.VendorHome := '/www/server/mysql/';
{$ELSE IF DEFINED (MSWINDOWS)}
DM.FDPhysMySQLDriverLink.VendorHome := GetCurrentDir;
{$ENDIF}
// DM.Con.Connected := True;
except on E: Exception do
Writeln(E.Message);
end;
LServer.Bindings.Clear;
LServer.Active := True;
Writeln('Server started on port ', APort);
while True do
Sleep(1000);
finally
TerminateThreads;
LServer.Active := False;
LServer.Free;
DM.Free;
end;
end;
begin
try
if WebRequestHandler <> nil then
WebRequestHandler.WebModuleClass := WebModuleClass;
RunServer(9381);
except
on E: Exception do
Writeln(E.ClassName, ': ', E.Message);
end
end.