easy-devops 1.1.0 → 1.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +2 -2
- package/package.json +6 -3
- package/src/cli/index.js +13 -1
- package/src/cli/menus/dashboard.js +1 -1
- package/src/core/channels/discord.js +77 -0
- package/src/core/channels/telegram.js +120 -0
- package/src/core/db.js +8 -1
- package/src/core/domainNotifier.js +298 -0
- package/src/core/events.js +130 -0
- package/src/core/notifier.js +73 -0
- package/src/core/shell.js +59 -7
- package/src/dashboard/controllers/authController.js +29 -0
- package/src/dashboard/controllers/domainsController.js +416 -0
- package/src/dashboard/controllers/nginxController.js +128 -0
- package/src/dashboard/controllers/settingsController.js +335 -0
- package/src/dashboard/controllers/sslController.js +291 -0
- package/src/dashboard/public/img/icon.png +0 -0
- package/src/dashboard/public/img/icon_b.png +0 -0
- package/src/dashboard/public/js/app.js +234 -2
- package/src/dashboard/routes/auth.js +12 -22
- package/src/dashboard/routes/domains.js +11 -363
- package/src/dashboard/routes/nginx.js +13 -146
- package/src/dashboard/routes/settings.js +16 -189
- package/src/dashboard/routes/ssl.js +9 -276
- package/src/dashboard/server.js +24 -27
- package/src/dashboard/socketManager.js +282 -0
- package/src/dashboard/views/index.ejs +52 -0
- package/src/dashboard/views/partials/domain-form.ejs +64 -1
- package/src/dashboard/views/partials/domains-panel.ejs +4 -0
- package/src/dashboard/views/partials/login.ejs +1 -1
- package/src/dashboard/views/partials/logs-panel.ejs +67 -0
- package/src/dashboard/views/partials/settings-panel.ejs +263 -87
- package/src/dashboard/views/partials/sidebar.ejs +3 -1
- /package/src/{dashboard/routes → core/channels}/.gitkeep +0 -0
|
@@ -0,0 +1,416 @@
|
|
|
1
|
+
import fs from 'fs/promises';
|
|
2
|
+
import path from 'path';
|
|
3
|
+
import { run } from '../../core/shell.js';
|
|
4
|
+
import { loadConfig } from '../../core/config.js';
|
|
5
|
+
import { getDomains, saveDomains, findDomain, createDomain, DOMAIN_DEFAULTS } from '../lib/domains-db.js';
|
|
6
|
+
import { generateConf } from '../lib/nginx-conf-generator.js';
|
|
7
|
+
import { getCertExpiry } from '../lib/cert-reader.js';
|
|
8
|
+
import { isWindows, nginxTestCmd, nginxReloadCmd, isNginxTestOk } from '../../core/platform.js';
|
|
9
|
+
import {
|
|
10
|
+
validateDomainName, validatePort, validateUpstreamType,
|
|
11
|
+
validateMaxBodySize,
|
|
12
|
+
} from '../../core/validators.js';
|
|
13
|
+
import { getDomainNotifConfig, saveDomainNotifConfig } from '../../core/domainNotifier.js';
|
|
14
|
+
|
|
15
|
+
class DomainsController {
|
|
16
|
+
async list(req, res, next) {
|
|
17
|
+
try {
|
|
18
|
+
const domains = getDomains();
|
|
19
|
+
const result = await Promise.all(
|
|
20
|
+
domains.map(async (domain) => {
|
|
21
|
+
const { expiry, daysLeft } = await getCertExpiry(domain.name);
|
|
22
|
+
const notifications = getDomainNotifConfig(domain.name);
|
|
23
|
+
return { ...domain, certExpiry: expiry, daysLeft, notifications };
|
|
24
|
+
})
|
|
25
|
+
);
|
|
26
|
+
res.json(result);
|
|
27
|
+
} catch (error) {
|
|
28
|
+
next(error);
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
async create(req, res, next) {
|
|
33
|
+
try {
|
|
34
|
+
const body = req.body ?? {};
|
|
35
|
+
|
|
36
|
+
// Validate required fields
|
|
37
|
+
const nameError = validateDomainName(body.name);
|
|
38
|
+
if (nameError) {
|
|
39
|
+
return res.status(400).json({ error: nameError });
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
// Port validation is skipped when backendHost is a full external URL
|
|
43
|
+
const backendIsUrl = /^https?:\/\//i.test(body.backendHost ?? '');
|
|
44
|
+
if (!backendIsUrl) {
|
|
45
|
+
const portError = validatePort(body.port);
|
|
46
|
+
if (portError) {
|
|
47
|
+
return res.status(400).json({ error: portError });
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
// Validate optional fields
|
|
52
|
+
const upstreamError = validateUpstreamType(body.upstreamType);
|
|
53
|
+
if (upstreamError) {
|
|
54
|
+
return res.status(400).json({ error: upstreamError });
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
const maxSizeError = validateMaxBodySize(body.performance?.maxBodySize);
|
|
58
|
+
if (maxSizeError) {
|
|
59
|
+
return res.status(400).json({ error: maxSizeError });
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
// Check for duplicate
|
|
63
|
+
if (findDomain(body.name)) {
|
|
64
|
+
return res.status(409).json({ error: `Domain already exists: ${body.name}` });
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
// Build domain object with defaults
|
|
68
|
+
const domain = createDomain({
|
|
69
|
+
name: body.name,
|
|
70
|
+
port: Number(body.port),
|
|
71
|
+
backendHost: body.backendHost ?? DOMAIN_DEFAULTS.backendHost,
|
|
72
|
+
upstreamType: body.upstreamType ?? DOMAIN_DEFAULTS.upstreamType,
|
|
73
|
+
www: body.www ?? false,
|
|
74
|
+
ssl: {
|
|
75
|
+
enabled: body.ssl?.enabled ?? false,
|
|
76
|
+
certPath: body.ssl?.certPath ?? '',
|
|
77
|
+
keyPath: body.ssl?.keyPath ?? '',
|
|
78
|
+
redirect: body.ssl?.redirect ?? true,
|
|
79
|
+
hsts: body.ssl?.hsts ?? false,
|
|
80
|
+
hstsMaxAge: body.ssl?.hstsMaxAge ?? DOMAIN_DEFAULTS.ssl.hstsMaxAge,
|
|
81
|
+
},
|
|
82
|
+
performance: {
|
|
83
|
+
maxBodySize: body.performance?.maxBodySize ?? DOMAIN_DEFAULTS.performance.maxBodySize,
|
|
84
|
+
readTimeout: body.performance?.readTimeout ?? DOMAIN_DEFAULTS.performance.readTimeout,
|
|
85
|
+
connectTimeout: body.performance?.connectTimeout ?? DOMAIN_DEFAULTS.performance.connectTimeout,
|
|
86
|
+
proxyBuffers: body.performance?.proxyBuffers ?? false,
|
|
87
|
+
gzip: body.performance?.gzip ?? true,
|
|
88
|
+
gzipTypes: body.performance?.gzipTypes ?? DOMAIN_DEFAULTS.performance.gzipTypes,
|
|
89
|
+
},
|
|
90
|
+
security: {
|
|
91
|
+
rateLimit: body.security?.rateLimit ?? false,
|
|
92
|
+
rateLimitRate: body.security?.rateLimitRate ?? DOMAIN_DEFAULTS.security.rateLimitRate,
|
|
93
|
+
rateLimitBurst: body.security?.rateLimitBurst ?? DOMAIN_DEFAULTS.security.rateLimitBurst,
|
|
94
|
+
securityHeaders: body.security?.securityHeaders ?? false,
|
|
95
|
+
custom404: body.security?.custom404 ?? false,
|
|
96
|
+
custom50x: body.security?.custom50x ?? false,
|
|
97
|
+
},
|
|
98
|
+
advanced: {
|
|
99
|
+
accessLog: body.advanced?.accessLog ?? true,
|
|
100
|
+
customLocations: body.advanced?.customLocations ?? '',
|
|
101
|
+
},
|
|
102
|
+
wildcard: body.wildcard ?? false,
|
|
103
|
+
enabled: true,
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
// Cert existence check (FR-001): prevent saving a config that references non-existent cert files
|
|
107
|
+
if (domain.ssl.enabled && domain.ssl.certPath) {
|
|
108
|
+
try {
|
|
109
|
+
await fs.access(domain.ssl.certPath, fs.constants.F_OK);
|
|
110
|
+
} catch {
|
|
111
|
+
return res.status(422).json({
|
|
112
|
+
error: 'cert_missing',
|
|
113
|
+
certPath: domain.ssl.certPath,
|
|
114
|
+
keyPath: domain.ssl.keyPath,
|
|
115
|
+
hint: 'The SSL certificate files do not exist at the configured paths. Create the certificate first, or disable SSL.',
|
|
116
|
+
});
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
const { nginxDir } = loadConfig();
|
|
121
|
+
|
|
122
|
+
try {
|
|
123
|
+
await generateConf(domain);
|
|
124
|
+
} catch (err) {
|
|
125
|
+
return res.status(500).json({ error: 'Failed to write nginx conf', details: err.message });
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
const testCmd = isWindows ? nginxTestCmd(nginxDir) : 'nginx -t';
|
|
129
|
+
const testResult = await run(testCmd, { cwd: nginxDir });
|
|
130
|
+
if (!isNginxTestOk(testResult)) {
|
|
131
|
+
try { await fs.unlink(domain.configFile); } catch { /* ignore */ }
|
|
132
|
+
const output = testResult.stderr || testResult.stdout;
|
|
133
|
+
const error = !isWindows && output.includes('password is required')
|
|
134
|
+
? 'Linux permissions not configured. Open Settings → Setup Linux Permissions.'
|
|
135
|
+
: 'nginx config test failed';
|
|
136
|
+
return res.status(500).json({ error, output });
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
const domains = getDomains();
|
|
140
|
+
domains.push(domain);
|
|
141
|
+
saveDomains(domains);
|
|
142
|
+
|
|
143
|
+
// Save per-domain notification config if provided
|
|
144
|
+
if (body.notifications && typeof body.notifications === 'object') {
|
|
145
|
+
try {
|
|
146
|
+
saveDomainNotifConfig(domain.name, body.notifications);
|
|
147
|
+
} catch { /* ignore validation errors — fall back to defaults */ }
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
res.status(201).json({ ...domain, notifications: getDomainNotifConfig(domain.name) });
|
|
151
|
+
} catch (error) {
|
|
152
|
+
next(error);
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
async update(req, res, next) {
|
|
157
|
+
try {
|
|
158
|
+
const { name } = req.params;
|
|
159
|
+
const existing = findDomain(name);
|
|
160
|
+
if (!existing) {
|
|
161
|
+
return res.status(404).json({ error: `Domain not found: ${name}` });
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
const body = req.body ?? {};
|
|
165
|
+
|
|
166
|
+
// Validate port if provided (skip when backendHost is a full external URL)
|
|
167
|
+
const backendIsUrl = /^https?:\/\//i.test(body.backendHost ?? existing.backendHost ?? '');
|
|
168
|
+
if (body.port !== undefined && !backendIsUrl) {
|
|
169
|
+
const portError = validatePort(body.port);
|
|
170
|
+
if (portError) {
|
|
171
|
+
return res.status(400).json({ error: portError });
|
|
172
|
+
}
|
|
173
|
+
body.port = Number(body.port);
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
// Validate upstreamType if provided
|
|
177
|
+
if (body.upstreamType !== undefined) {
|
|
178
|
+
const upstreamError = validateUpstreamType(body.upstreamType);
|
|
179
|
+
if (upstreamError) {
|
|
180
|
+
return res.status(400).json({ error: upstreamError });
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
// name is immutable — merge everything except name, configFile, and notifications
|
|
185
|
+
const { name: _ignored, configFile: _cf, notifications, ...updates } = body;
|
|
186
|
+
|
|
187
|
+
// Deep merge nested objects
|
|
188
|
+
const updatedDomain = {
|
|
189
|
+
...existing,
|
|
190
|
+
...updates,
|
|
191
|
+
ssl: { ...existing.ssl, ...updates.ssl },
|
|
192
|
+
performance: { ...existing.performance, ...updates.performance },
|
|
193
|
+
security: { ...existing.security, ...updates.security },
|
|
194
|
+
advanced: { ...existing.advanced, ...updates.advanced },
|
|
195
|
+
wildcard: updates.wildcard ?? existing.wildcard ?? false,
|
|
196
|
+
updatedAt: new Date().toISOString(),
|
|
197
|
+
};
|
|
198
|
+
|
|
199
|
+
const { nginxDir } = loadConfig();
|
|
200
|
+
const bakPath = existing.configFile ? `${existing.configFile}.bak` : null;
|
|
201
|
+
|
|
202
|
+
// Backup existing conf
|
|
203
|
+
if (bakPath && existing.configFile) {
|
|
204
|
+
try {
|
|
205
|
+
await fs.copyFile(existing.configFile, bakPath);
|
|
206
|
+
} catch { /* file absent — skip backup */ }
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
try {
|
|
210
|
+
await generateConf(updatedDomain);
|
|
211
|
+
} catch (err) {
|
|
212
|
+
if (bakPath) {
|
|
213
|
+
try { await fs.copyFile(bakPath, existing.configFile); } catch { /* ignore restore failure */ }
|
|
214
|
+
}
|
|
215
|
+
return res.status(500).json({ error: 'Failed to write nginx conf', details: err.message });
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
const testCmd = isWindows ? nginxTestCmd(nginxDir) : 'nginx -t';
|
|
219
|
+
const testResult = await run(testCmd, { cwd: nginxDir });
|
|
220
|
+
if (!isNginxTestOk(testResult)) {
|
|
221
|
+
if (bakPath) {
|
|
222
|
+
try { await fs.rename(bakPath, existing.configFile); } catch { /* ignore */ }
|
|
223
|
+
}
|
|
224
|
+
const output = testResult.stderr || testResult.stdout;
|
|
225
|
+
const error = !isWindows && output.includes('password is required')
|
|
226
|
+
? 'Linux permissions not configured. Open Settings → Setup Linux Permissions.'
|
|
227
|
+
: 'nginx config test failed';
|
|
228
|
+
return res.status(500).json({ error, output });
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
const domains = getDomains();
|
|
232
|
+
const idx = domains.findIndex((d) => d.name === name);
|
|
233
|
+
domains[idx] = updatedDomain;
|
|
234
|
+
saveDomains(domains);
|
|
235
|
+
|
|
236
|
+
// Save per-domain notification config if provided
|
|
237
|
+
if (notifications && typeof notifications === 'object') {
|
|
238
|
+
try {
|
|
239
|
+
saveDomainNotifConfig(name, notifications);
|
|
240
|
+
} catch { /* ignore validation errors — keep existing config */ }
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
res.json({ ...updatedDomain, notifications: getDomainNotifConfig(name) });
|
|
244
|
+
} catch (error) {
|
|
245
|
+
next(error);
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
async delete(req, res, next) {
|
|
250
|
+
try {
|
|
251
|
+
const { name } = req.params;
|
|
252
|
+
const deleteCert = req.query.deleteCert === 'true';
|
|
253
|
+
const domain = findDomain(name);
|
|
254
|
+
if (!domain) {
|
|
255
|
+
return res.status(404).json({ error: `Domain not found: ${name}` });
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
// Delete nginx conf file
|
|
259
|
+
if (domain.configFile) {
|
|
260
|
+
try {
|
|
261
|
+
await fs.unlink(domain.configFile);
|
|
262
|
+
} catch (err) {
|
|
263
|
+
if (err.code !== 'ENOENT') {
|
|
264
|
+
return res.status(500).json({ error: 'Failed to delete conf file', details: err.message });
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
// Optionally delete the SSL certificate directory for this domain
|
|
270
|
+
if (deleteCert && domain.ssl?.enabled) {
|
|
271
|
+
const { sslDir } = loadConfig();
|
|
272
|
+
const certDir = path.join(sslDir, name);
|
|
273
|
+
try {
|
|
274
|
+
await fs.rm(certDir, { recursive: true, force: true });
|
|
275
|
+
} catch { /* ignore — cert dir may not exist */ }
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
const domains = getDomains().filter((d) => d.name !== name);
|
|
279
|
+
saveDomains(domains);
|
|
280
|
+
|
|
281
|
+
res.json({ deleted: name, certDeleted: deleteCert && domain.ssl?.enabled });
|
|
282
|
+
} catch (error) {
|
|
283
|
+
next(error);
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
async toggle(req, res, next) {
|
|
288
|
+
try {
|
|
289
|
+
const { name } = req.params;
|
|
290
|
+
const domain = findDomain(name);
|
|
291
|
+
if (!domain) {
|
|
292
|
+
return res.status(404).json({ error: `Domain not found: ${name}` });
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
const { nginxDir } = loadConfig();
|
|
296
|
+
const isEnabled = domain.enabled !== false;
|
|
297
|
+
const confPath = domain.configFile;
|
|
298
|
+
|
|
299
|
+
if (!confPath) {
|
|
300
|
+
return res.status(400).json({ error: 'Domain has no config file path stored' });
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
// Normalise paths regardless of current stored extension
|
|
304
|
+
const basePath = confPath.replace(/\.disabled$/, '');
|
|
305
|
+
const enabledPath = basePath;
|
|
306
|
+
const disabledPath = `${basePath}.disabled`;
|
|
307
|
+
|
|
308
|
+
if (isEnabled) {
|
|
309
|
+
// Disable: rename .conf → .conf.disabled, then reload nginx
|
|
310
|
+
try {
|
|
311
|
+
await fs.rename(enabledPath, disabledPath);
|
|
312
|
+
} catch (err) {
|
|
313
|
+
return res.status(500).json({ error: 'Failed to rename config file', details: err.message });
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
const domains = getDomains();
|
|
317
|
+
const idx = domains.findIndex(d => d.name === name);
|
|
318
|
+
domains[idx] = { ...domain, enabled: false, configFile: disabledPath, updatedAt: new Date().toISOString() };
|
|
319
|
+
saveDomains(domains);
|
|
320
|
+
|
|
321
|
+
// Reload so nginx stops serving this domain
|
|
322
|
+
await run(isWindows ? nginxReloadCmd(nginxDir) : 'sudo -n /usr/bin/systemctl reload nginx', { cwd: nginxDir });
|
|
323
|
+
|
|
324
|
+
return res.json({ enabled: false });
|
|
325
|
+
} else {
|
|
326
|
+
// Enable: rename .conf.disabled → .conf, test, reload
|
|
327
|
+
try {
|
|
328
|
+
await fs.rename(disabledPath, enabledPath);
|
|
329
|
+
} catch (err) {
|
|
330
|
+
return res.status(500).json({ error: 'Failed to rename config file', details: err.message });
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
const testCmd = isWindows ? nginxTestCmd(nginxDir) : 'nginx -t';
|
|
334
|
+
const testResult = await run(testCmd, { cwd: nginxDir });
|
|
335
|
+
if (!isNginxTestOk(testResult)) {
|
|
336
|
+
// Roll back rename
|
|
337
|
+
await fs.rename(enabledPath, disabledPath).catch(() => {});
|
|
338
|
+
const output = testResult.stderr || testResult.stdout;
|
|
339
|
+
const error = !isWindows && output.includes('password is required')
|
|
340
|
+
? 'Linux permissions not configured. Open Settings → Setup Linux Permissions.'
|
|
341
|
+
: 'nginx config test failed';
|
|
342
|
+
return res.status(500).json({ error, output });
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
const domains = getDomains();
|
|
346
|
+
const idx = domains.findIndex(d => d.name === name);
|
|
347
|
+
domains[idx] = { ...domain, enabled: true, configFile: enabledPath, updatedAt: new Date().toISOString() };
|
|
348
|
+
saveDomains(domains);
|
|
349
|
+
|
|
350
|
+
await run(isWindows ? nginxReloadCmd(nginxDir) : 'sudo -n /usr/bin/systemctl reload nginx', { cwd: nginxDir });
|
|
351
|
+
|
|
352
|
+
return res.json({ enabled: true });
|
|
353
|
+
}
|
|
354
|
+
} catch (error) {
|
|
355
|
+
next(error);
|
|
356
|
+
}
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
async reload(req, res, next) {
|
|
360
|
+
try {
|
|
361
|
+
const { name } = req.params;
|
|
362
|
+
if (!findDomain(name)) {
|
|
363
|
+
return res.status(404).json({ error: `Domain not found: ${name}` });
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
const { nginxDir } = loadConfig();
|
|
367
|
+
|
|
368
|
+
const testCmd = isWindows ? nginxTestCmd(nginxDir) : 'nginx -t';
|
|
369
|
+
const testResult = await run(testCmd, { cwd: nginxDir });
|
|
370
|
+
if (!isNginxTestOk(testResult)) {
|
|
371
|
+
const output = testResult.stderr || testResult.stdout;
|
|
372
|
+
const error = !isWindows && output.includes('password is required')
|
|
373
|
+
? 'Linux permissions not configured. Open Settings → Setup Linux Permissions.'
|
|
374
|
+
: 'nginx config test failed';
|
|
375
|
+
return res.status(500).json({ error, output });
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
const reloadCmd = isWindows ? nginxReloadCmd(nginxDir) : 'sudo -n /usr/bin/systemctl reload nginx';
|
|
379
|
+
const reloadResult = await run(reloadCmd, { cwd: nginxDir });
|
|
380
|
+
if (!reloadResult.success) {
|
|
381
|
+
const output = reloadResult.stderr || reloadResult.stdout;
|
|
382
|
+
const error = !isWindows && output.includes('password is required')
|
|
383
|
+
? 'Linux permissions not configured. Open Settings → Setup Linux Permissions.'
|
|
384
|
+
: 'nginx reload failed';
|
|
385
|
+
return res.status(500).json({ error, output });
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
res.json({ message: 'nginx reloaded successfully' });
|
|
389
|
+
} catch (error) {
|
|
390
|
+
next(error);
|
|
391
|
+
}
|
|
392
|
+
}
|
|
393
|
+
|
|
394
|
+
async getNotifications(req, res, next) {
|
|
395
|
+
try {
|
|
396
|
+
const config = getDomainNotifConfig(req.params.name);
|
|
397
|
+
res.json(config);
|
|
398
|
+
} catch (error) {
|
|
399
|
+
next(error);
|
|
400
|
+
}
|
|
401
|
+
}
|
|
402
|
+
|
|
403
|
+
async updateNotifications(req, res, next) {
|
|
404
|
+
try {
|
|
405
|
+
saveDomainNotifConfig(req.params.name, req.body ?? {});
|
|
406
|
+
res.json(getDomainNotifConfig(req.params.name));
|
|
407
|
+
} catch (error) {
|
|
408
|
+
if (error.message?.startsWith('Invalid') || error.message?.startsWith('Config') || error.message?.startsWith('Entry')) {
|
|
409
|
+
return res.status(400).json({ error: error.message });
|
|
410
|
+
}
|
|
411
|
+
next(error);
|
|
412
|
+
}
|
|
413
|
+
}
|
|
414
|
+
}
|
|
415
|
+
|
|
416
|
+
export default DomainsController;
|
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
import {
|
|
2
|
+
getStatus,
|
|
3
|
+
reload,
|
|
4
|
+
restart,
|
|
5
|
+
start,
|
|
6
|
+
stop,
|
|
7
|
+
test,
|
|
8
|
+
listConfigs,
|
|
9
|
+
getConfig,
|
|
10
|
+
saveConfig,
|
|
11
|
+
getLogs,
|
|
12
|
+
NginxNotFoundError,
|
|
13
|
+
NginxConfigError,
|
|
14
|
+
InvalidFilenameError,
|
|
15
|
+
} from '../lib/nginx-service.js';
|
|
16
|
+
|
|
17
|
+
class NginxController {
|
|
18
|
+
handleError(err, res) {
|
|
19
|
+
if (err instanceof NginxNotFoundError) {
|
|
20
|
+
return res.status(503).json({ error: 'nginx not installed' });
|
|
21
|
+
}
|
|
22
|
+
if (err instanceof NginxConfigError) {
|
|
23
|
+
return res.status(400).json({ success: false, output: err.output });
|
|
24
|
+
}
|
|
25
|
+
if (err instanceof InvalidFilenameError) {
|
|
26
|
+
return res.status(400).json({ error: 'Invalid filename' });
|
|
27
|
+
}
|
|
28
|
+
if (err.code === 'ENOENT') {
|
|
29
|
+
return res.status(404).json({ error: 'Config file not found' });
|
|
30
|
+
}
|
|
31
|
+
return res.status(500).json({ error: 'Internal server error' });
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
async status(req, res, next) {
|
|
35
|
+
try {
|
|
36
|
+
const status = await getStatus();
|
|
37
|
+
res.json(status);
|
|
38
|
+
} catch (err) {
|
|
39
|
+
this.handleError(err, res);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
async reload(req, res, next) {
|
|
44
|
+
try {
|
|
45
|
+
const result = await reload();
|
|
46
|
+
res.json(result);
|
|
47
|
+
} catch (err) {
|
|
48
|
+
this.handleError(err, res);
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
async restart(req, res, next) {
|
|
53
|
+
try {
|
|
54
|
+
const result = await restart();
|
|
55
|
+
res.json(result);
|
|
56
|
+
} catch (err) {
|
|
57
|
+
this.handleError(err, res);
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
async start(req, res, next) {
|
|
62
|
+
try {
|
|
63
|
+
const result = await start();
|
|
64
|
+
res.json(result);
|
|
65
|
+
} catch (err) {
|
|
66
|
+
this.handleError(err, res);
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
async stop(req, res, next) {
|
|
71
|
+
try {
|
|
72
|
+
const result = await stop();
|
|
73
|
+
res.json(result);
|
|
74
|
+
} catch (err) {
|
|
75
|
+
this.handleError(err, res);
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
async test(req, res, next) {
|
|
80
|
+
try {
|
|
81
|
+
const result = await test();
|
|
82
|
+
res.json(result);
|
|
83
|
+
} catch (err) {
|
|
84
|
+
this.handleError(err, res);
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
async listConfigs(req, res, next) {
|
|
89
|
+
try {
|
|
90
|
+
const configs = await listConfigs();
|
|
91
|
+
res.json(configs);
|
|
92
|
+
} catch (err) {
|
|
93
|
+
this.handleError(err, res);
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
async getConfig(req, res, next) {
|
|
98
|
+
try {
|
|
99
|
+
const result = await getConfig(req.params.filename);
|
|
100
|
+
res.json(result);
|
|
101
|
+
} catch (err) {
|
|
102
|
+
this.handleError(err, res);
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
async saveConfig(req, res, next) {
|
|
107
|
+
try {
|
|
108
|
+
if (typeof req.body.content !== 'string') {
|
|
109
|
+
return res.status(400).json({ error: 'content must be a string' });
|
|
110
|
+
}
|
|
111
|
+
const result = await saveConfig(req.params.filename, req.body.content);
|
|
112
|
+
res.json(result);
|
|
113
|
+
} catch (err) {
|
|
114
|
+
this.handleError(err, res);
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
async getLogs(req, res, next) {
|
|
119
|
+
try {
|
|
120
|
+
const result = await getLogs(100);
|
|
121
|
+
res.json(result);
|
|
122
|
+
} catch (err) {
|
|
123
|
+
this.handleError(err, res);
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
export default NginxController;
|