-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathServer.Service.pas
123 lines (101 loc) · 2.88 KB
/
Server.Service.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
(*
Copyright 2016, MARS-Curiosity - REST Library
Home: https://github.com/andrea-magni/MARS
*)
unit Server.Service;
{$I MARS.inc}
interface
uses
{$ifdef DelphiXE3_UP}
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Classes, Vcl.Graphics
, Vcl.Controls, Vcl.SvcMgr, Vcl.Dialogs
//, IPPeerServer, IPPeerAPI
, IdHTTPWebBrokerBridge, Web.WebReq, Web.WebBroker
{$else}
Windows, Messages, SysUtils, Classes, Graphics
, Controls, SvcMgr, Dialogs
//, IPPeerServer, IPPeerAPI
, IdHTTPWebBrokerBridge, WebReq, WebBroker
{$endif}
, IdContext
;
type
TServerService = class(TService)
procedure ServiceCreate(Sender: TObject);
procedure ServiceDestroy(Sender: TObject);
procedure ServiceStart(Sender: TService; var Started: Boolean);
procedure ServiceStop(Sender: TService; var Stopped: Boolean);
private
FServer: TIdHTTPWebBrokerBridge;
procedure ParseAuthenticationHandler(AContext: TIdContext;
const AAuthType, AAuthData: String; var VUsername, VPassword: String;
var VHandled: Boolean); virtual;
public
function GetServiceController: TServiceController; override;
const DEFAULT_PORT = 8080;
end;
var
ServerService: TServerService;
implementation
{$R *.dfm}
uses
IdSchedulerOfThreadPool
, Server.Ignition
, Server.WebModule
;
procedure ServiceController(CtrlCode: DWord); stdcall;
begin
ServerService.Controller(CtrlCode);
end;
function TServerService.GetServiceController: TServiceController;
begin
Result := ServiceController;
end;
procedure TServerService.ParseAuthenticationHandler(AContext: TIdContext;
const AAuthType, AAuthData: String; var VUsername, VPassword: String;
var VHandled: Boolean);
begin
// Allow JWT Bearer authentication's scheme
if SameText(AAuthType, 'Bearer') then
VHandled := True;
end;
procedure TServerService.ServiceCreate(Sender: TObject);
var
LScheduler: TIdSchedulerOfThreadPool;
begin
if WebRequestHandler <> nil then
WebRequestHandler.WebModuleClass := WebModuleClass;
FServer := TIdHTTPWebBrokerBridge.Create(nil);
try
FServer.DefaultPort := TServerEngine.Default.Port;
LScheduler := TIdSchedulerOfThreadPool.Create(FServer);
try
LScheduler.PoolSize := TServerEngine.Default.ThreadPoolSize;
FServer.Scheduler := LScheduler;
FServer.MaxConnections := LScheduler.PoolSize;
FServer.OnParseAuthentication := ParseAuthenticationHandler;
except
FServer.Scheduler.Free;
FServer.Scheduler := nil;
raise;
end;
except
FServer.Free;
raise;
end;
end;
procedure TServerService.ServiceDestroy(Sender: TObject);
begin
FreeAndNil(FServer);
end;
procedure TServerService.ServiceStart(Sender: TService; var Started: Boolean);
begin
FServer.Active := True;
Started := FServer.Active;
end;
procedure TServerService.ServiceStop(Sender: TService; var Stopped: Boolean);
begin
FServer.Active := False;
Stopped := not FServer.Active;
end;
end.