-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMyService.pas
More file actions
131 lines (98 loc) · 2.96 KB
/
Copy pathMyService.pas
File metadata and controls
131 lines (98 loc) · 2.96 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
unit MyService;
{$WARN IMPLICIT_STRING_CAST OFF}
{$WARN IMPLICIT_STRING_CAST_LOSS OFF}
interface
uses
System.SysUtils,
WinApi.Windows,
SynCommons,
SynTable,
SynCrypto,
mORMot,
mORMotHTTPServer,
mORMotService,
Constants,
MyService.Interfaces,
MyService.Model;
type
/// class implementing the background Service
TMyWinService = class(TServiceSingle)
public
/// the associated database model
Model: TSQLModel;
/// the associated DB/
Rest: TSQLRestServer;
/// the background Server processing all requests
Server: TSQLHttpServer;
/// event triggered to start the service
// - e.g. create the Server instance
procedure DoStart(Sender: TService); virtual;
/// event triggered to stop the service
// - e.g. destroy the Server instance
procedure DoStop(Sender: TService); virtual;
constructor Create(const aServiceName, aDisplayName: String); reintroduce;
constructor CreateAsConsole; reintroduce;
destructor Destroy; override;
class procedure WriteHelpContent;
end;
function getAlgo(const Value: RawUTF8): TSignAlgo;
implementation
uses Base.RestServer;
function getAlgo(const Value: RawUTF8): TSignAlgo;
var i : TSignAlgo;
begin
Result := saSha256;
for i := low(JWT_TEXT) to High(JWT_TEXT) do
if SameTextU(Value, JWT_TEXT[i]) then begin
Result := i;
Break;
end;
end;
{ TMyWinService }
constructor TMyWinService.Create(const aServiceName, aDisplayName: String);
begin
inherited Create(aServiceName, aDisplayName);
OnStart := {$ifdef FPC}@{$endif}DoStart;
OnStop := {$ifdef FPC}@{$endif}DoStop;
OnResume := {$ifdef FPC}@{$endif}DoStart; // trivial Pause/Resume actions
OnPause := {$ifdef FPC}@{$endif}DoStop;
end;
constructor TMyWinService.CreateAsConsole;
begin
// manual switch to console mode
AllocConsole;
end;
destructor TMyWinService.Destroy;
begin
if Server<>nil then
DoStop(nil); // should not happen
inherited Destroy;
end;
procedure TMyWinService.DoStart(Sender: TService);
var SFS: TServiceFactoryServer;
begin
if Server<>nil then
DoStop(nil); // should never happen
Model := TSQLModel.Create([]);
Rest := TSQLRestServer.Create(Model, True);
Rest.CreateMissingTables;
SFS := Rest.ServiceDefine(TMyService, [IMyService], sicShared, SERVICE_CONTRACT_NONE_EXPECTED);
SFS.ResultAsJSONObjectWithoutResult := True;
SFS.ByPassAuthentication := True;
SFS.SetOptions([], [optErrorOnMissingParam]);
Server := TSQLHttpServer.Create('8080',[Rest],'+',useHttpApiRegisteringURI);
Server.AccessControlAllowOrigin := '*';
end;
procedure TMyWinService.DoStop(Sender: TService);
begin
end;
class procedure TMyWinService.WriteHelpContent;
begin
WriteLn('To install your service please type /install');
WriteLn('To uninstall your service please type /uninstall');
WriteLn('To start your service please type /start');
WriteLn('To stop your service please type /stop');
WriteLn('');
WriteLn('For help please type /? or /h');
end;
end.