-
Notifications
You must be signed in to change notification settings - Fork 3
/
server-settings-store.ts
172 lines (150 loc) · 5.3 KB
/
server-settings-store.ts
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
/**
* SudoSOS back-end API service.
* Copyright (C) 2024 Study association GEWIS
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* @license
*/
/**
* This is the module page of the server-settings store.
*
* @module internal/server-settings
*/
import { Repository } from 'typeorm';
import ServerSetting, { ISettings } from '../entity/server-setting';
import SettingsDefaults from './setting-defaults';
import { AppDataSource } from '../database/database';
/**
* Store of global server settings, which are key-value pairs stored in the database.
* Used for settings that fit a database store better than an environment variable,
* as the latter should contain mostly secrets to get things to work, not to
* configure stuff.
*/
export default class ServerSettingsStore<T extends keyof ISettings = keyof ISettings> {
private static instance: ServerSettingsStore;
private _initialized = false;
private repo: Repository<ServerSetting>;
private settings: ISettings;
constructor() {
this.repo = ServerSetting.getRepository();
}
/**
* Singleton, because there is only one copy of the core running at a time.
* We can therefore simply initialize the store once and keep it up to date
* from memory.
*/
public static getInstance() {
if (!this.instance) {
this.instance = new ServerSettingsStore();
}
return this.instance;
}
get initialized() {
return this._initialized;
}
private isInitialized() {
if (!this._initialized) throw new Error('ServerSettingsStore has not been initialized.');
}
/**
* Fetch all key-value pairs from the database
*/
public async initialize() {
if (this._initialized) {
throw new Error('ServerSettingsStore already initialized!');
}
const settings = await this.repo.find();
const promises: Promise<ServerSetting>[] = [];
// Save any new key-value pairs to the database if they don't yet exist
Object.entries(SettingsDefaults).forEach((entry) => {
const key = entry[0] as keyof ISettings;
const value = entry[1];
const setting = settings.find((s) => s.key === key);
if (!setting) {
const promise = this.repo.save({ key, value });
// Add the missing setting key with its default value
promises.push(promise);
}
});
// The settings object now contains all key-value pairs
settings.push(...(await Promise.all(promises)));
const map = new Map<ServerSetting['key'], ServerSetting['value']>();
Object.keys(SettingsDefaults).forEach((key) => {
const setting = settings.find((s) => s.key === key);
// Sanity check
if (!setting) throw new Error(`Setting "${key}" missing during initialization`);
map.set(setting.key, setting.value);
});
this.settings = Object.fromEntries(map) as any as ISettings;
this._initialized = true;
return this;
}
/**
* Get a server setting. If the setting is subject to change during runtime,
* use the "getSettingFromDatabase" method instead.
* @param key
*/
public getSetting(key: T): ISettings[T] {
this.isInitialized();
if (this.settings[key] === undefined) {
throw new Error(`Setting with key "${key}" does not exist.`);
}
return this.settings[key];
}
/**
* Get a server setting from the database. This ensures it is always up to date,
* but adds some latency due to a database query.
* @param key
*/
public async getSettingFromDatabase(key: T): Promise<ISettings[T]> {
this.isInitialized();
const value = await ServerSettingsStore.getSettingFromDatabase(key);
if (value == null) {
throw new Error(`Setting with key "${key}" does not exist.`);
}
this.settings[key] = value;
return value;
}
/**
* Get a server setting from the database. Returns null if it does not exist.
* Compared to the class method, this method does not update the internal cache.
* @param key
*/
public static async getSettingFromDatabase<T extends keyof ISettings>(key: T): Promise<ISettings[T] | null> {
const record = await AppDataSource.manager.findOne(ServerSetting, { where: { key } });
if (!record) return null;
return record.value as ISettings[T];
}
/**
* Update a server setting
* @param key
* @param value
*/
public async setSetting(key: T, value: ISettings[T]) {
this.isInitialized();
const setting = await this.repo.findOne({ where: { key } });
if (!setting) {
throw new Error(`Setting with key "${key}" does not exist.`);
}
setting!.value = value;
this.settings[key] = value;
return this.repo.save(setting!);
}
/**
* Only for testing, never use in production
*/
public static deleteInstance() {
this.instance = undefined;
}
}