-
Notifications
You must be signed in to change notification settings - Fork 32
/
Quick.ORM.Server.Config.pas
202 lines (169 loc) · 6.04 KB
/
Quick.ORM.Server.Config.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
{ ***************************************************************************
Copyright (c) 2016-2019 Kike Pérez
Unit : Quick.ORM.RestServer.Config
Description : Rest ORM Server allows access by http, httpapi or websockets
Author : Kike Pérez
Version : 1.5
Created : 09/06/2017
Modified : 08/05/2019
This file is part of QuickORM: https://github.com/exilon/QuickORM
Uses Synopse mORMot framework. Copyright (C) 2017 Arnaud Bouchez
Synopse Informatique - https://synopse.info
***************************************************************************
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*************************************************************************** }
unit Quick.ORM.Server.Config;
{$i QuickORM.inc}
interface
uses
Classes,
SysUtils,
{$IFNDEF FPC}
System.IOUtils,
{$ELSE}
Quick.Files,
{$ENDIF}
SynCommons,
mORMot,
Quick.ORM.Engine,
Quick.ORM.Security,
Quick.FileMonitor;
type
TConfigChangedEvent = procedure of object;
//Basic Server config file
TORMCustomConfig = class
private
fConfigFilename : RawUTF8;
fOnLoadConfig : TLoadConfigEvent;
fDebugMode : Boolean;
fDBFilename : RawUTF8;
fFileMonitor : TQuickFileMonitor;
fOnConfigChanged : TConfigChangedEvent;
fReloaded : Boolean;
procedure FileChangedNotify(MonitorNotify : TMonitorNotify);
published
property DebugMode : Boolean read fDebugMode write fDebugMode;
property DBFilename : RawUTF8 read fDBFilename write fDBFilename;
public
constructor Create;
destructor Destroy; override;
property OnLoadConfig : TLoadConfigEvent read fOnLoadConfig write fOnLoadConfig;
property OnConfigChanged : TConfigChangedEvent read fOnConfigChanged write fOnConfigChanged;
function Load : Boolean;
function Save : Boolean;
end;
TORMCustomConfigClass = class of TORMCustomConfig;
TORMRestDBConfig = class(TORMCustomConfig);
TORMRestDBFullConfig = class(TORMCustomConfig);
TORMRestServerConfig = class(TORMCustomConfig)
private
fHost : RawUTF8;
fPort : Integer;
fIPRestrictionDefaultRule : RawUTF8;
fIPRestrictionExcludedIP : TArrayOfRawUTF8;
fAPIKeys : TArrayOfRawUTF8;
function GetDefaultSecurityRule : TSecurityAccess;
procedure SetDefaultSecurityRule(cSecurityAccess : TSecurityAccess);
published
property ServerHost : RawUTF8 read fHost write fHost;
property ServerPort : Integer read fPort write fPort;
property IPRestrictionDefaultRule : RawUTF8 read fIPRestrictionDefaultRule write fIPRestrictionDefaultRule;
property IPRestrictionExcludedIP : TArrayOfRawUTF8 read fIPRestrictionExcludedIP write fIPRestrictionExcludedIP;
property APIKeys : TArrayOfRawUTF8 read fAPIKeys write fAPIKeys;
public
property DefaultSecurityRule : TSecurityAccess read GetDefaultSecurityRule write SetDefaultSecurityRule;
constructor Create;
end;
implementation
{TORMCustomConfig Class}
constructor TORMCustomConfig.Create;
begin
fConfigFilename := Format('%s\%s.config',[TPath.GetDirectoryName(ParamStr(0)),TPath.GetFileNameWithoutExtension(ParamStr(0))]);
fOnLoadConfig := nil;
fOnConfigChanged := nil;
fReloaded := False;
fDebugMode := False;
fDBFilename := '';
fFileMonitor := TQuickFileMonitor.Create;
fFileMonitor.FileName := fConfigFilename;
fFileMonitor.Interval := 2000;
fFileMonitor.Notifies := [TMonitorNotify.mnFileModified];
fFileMonitor.OnFileChange := FileChangedNotify;
fFileMonitor.Enabled := True;
end;
destructor TORMCustomConfig.Destroy;
begin
if Assigned(fFileMonitor) then
begin
fFileMonitor.Enabled := False;
fFileMonitor.Free;
end;
end;
procedure TORMCustomConfig.FileChangedNotify(MonitorNotify : TMonitorNotify);
begin
if MonitorNotify = TMonitorNotify.mnFileModified then
begin
fReloaded := True;
Self.Load;
if Assigned(fOnConfigChanged) then fOnConfigChanged;
end;
end;
function TORMCustomConfig.Load : Boolean;
begin
fFileMonitor.Enabled := False;
try
if FileExists(fConfigFilename) then
begin
try
Result := JSONFileToObject(fConfigFilename,Self,nil,[j2oIgnoreUnknownProperty]);
if (not fReloaded) and (Result) and (Assigned(fOnLoadConfig)) then fOnLoadConfig;
//saves to update possible new fields in json file
if Result then Result := Self.Save
else raise Exception.Create('Error reading Config File!');
except
on E : Exception do raise Exception.Create(Format('Can''t load/save Server Config! [%s]',[e.Message]));
end;
end
else Result := Self.Save;
finally
fFileMonitor.Enabled := True;
end;
//if (not fReloaded) and (Result) and (Assigned(fOnLoadConfig)) then fOnLoadConfig;
end;
function TORMCustomConfig.Save : Boolean;
begin
try
Result := ObjectToJSONFile(Self,fConfigFilename);
except
on E : Exception do raise Exception.Create(Format('Can''t save Server Config! [%s]',[e.Message]));
end;
end;
{TORMRestServerConfig Class}
constructor TORMRestServerConfig.Create;
begin
inherited;
fHost := '127.0.0.1';
fPort := 8090;
fDBFilename := '';
fIPRestrictionDefaultRule := 'Allow';
fIPRestrictionExcludedIP := [];
end;
function TORMRestServerConfig.GetDefaultSecurityRule : TSecurityAccess;
begin
if LowerCase(fIPRestrictionDefaultRule) = 'allow' then Result := TSecurityAccess.saAllowed
else Result := TSecurityAccess.saDenied;
end;
procedure TORMRestServerConfig.SetDefaultSecurityRule(cSecurityAccess : TSecurityAccess);
begin
if cSecurityAccess = TSecurityAccess.saAllowed then fIPRestrictionDefaultRule := 'allow'
else fIPRestrictionDefaultRule := 'denied';
end;
end.