feat: add optional HTTPS server alongside HTTP
@@ -1,5 +1,9 @@
1
1
# Technical Reference
2
2
3
## server/index.js
4
- start() - Initialises DB/keys, loads config, starts HTTP server (config.port) and optional HTTPS server (config.httpsPort + config.tlsCert + config.tlsKey). Both share the same Express app.
5
- shutdown(...servers) - Graceful teardown: closes DB, closes all provided http/https Server instances, force-exits after 5 s.
6
3
7
## server/db.js
4
8
- initDb() - Initializes SQLite schema for users, file metadata, and auth_sessions (idle timeout/revocation support).
5
9
- getDb() - Returns singleton DB handle with WAL and foreign key pragmas.
@@ -1,5 +1,9 @@1
# Technical Reference1
# Technical Reference2
2
3
## server/index.js4
- start() - Initialises DB/keys, loads config, starts HTTP server (config.port) and optional HTTPS server (config.httpsPort + config.tlsCert + config.tlsKey). Both share the same Express app.5
- shutdown(...servers) - Graceful teardown: closes DB, closes all provided http/https Server instances, force-exits after 5 s.6
3
## server/db.js7
## server/db.js4
- initDb() - Initializes SQLite schema for users, file metadata, and auth_sessions (idle timeout/revocation support).8
- initDb() - Initializes SQLite schema for users, file metadata, and auth_sessions (idle timeout/revocation support).5
- getDb() - Returns singleton DB handle with WAL and foreign key pragmas.9
- getDb() - Returns singleton DB handle with WAL and foreign key pragmas.@@ -2,6 +2,9 @@
2
2
3
3
const express = require('express');
4
4
const path = require('path');
5
const fs = require('fs');
6
const http = require('http');
7
const https = require('https');
5
8
const { initDb, closeDb } = require('./db');
6
9
const { ensureServerKeys } = require('./crypto');
7
10
const { loadConfig } = require('./config');
@@ -25,18 +28,22 @@ app.get('*', (req, res) =>
25
28
});
26
29
27
30
// Exposed so the shutdown route can trigger a graceful stop.
28
function shutdown (server)
31
function shutdown (...servers)
29
32
{
30
if (!server || server.__isShuttingDown) return;
31
server.__isShuttingDown = true;
33
const active = servers.filter(Boolean);
34
if (active.length === 0 || active[0].__isShuttingDown) return;
35
active.forEach(s => { s.__isShuttingDown = true; });
32
36
33
37
// Close DB first so file locks are released as early as possible.
34
38
closeDb();
35
39
36
server.close(() =>
40
let closed = 0;
41
function onClose ()
37
42
{
38
process.exit(0);
39
});
43
if (++closed === active.length) process.exit(0);
44
}
45
46
active.forEach(s => s.close(onClose));
40
47
41
48
// Force-exit after 5 s if connections don't drain in time.
42
49
setTimeout(() =>
@@ -53,17 +60,42 @@ function start ()
53
60
const config = loadConfig();
54
61
const port = config.port || 3000;
55
62
56
const server = app.listen(port, '0.0.0.0', () =>
63
const httpServer = http.createServer(app);
64
httpServer.listen(port, '0.0.0.0', () =>
57
65
{
58
66
console.log(`File manager running at http://localhost:${port}`);
59
67
});
60
68
69
let httpsServer = null;
70
if (config.httpsPort && config.tlsCert && config.tlsKey)
71
{
72
let tlsOptions;
73
try
74
{
75
tlsOptions = {
76
cert: fs.readFileSync(config.tlsCert),
77
key: fs.readFileSync(config.tlsKey),
78
};
79
} catch (err)
80
{
81
console.error('Failed to read TLS certificate/key:', err.message);
82
process.exit(1);
83
}
84
httpsServer = https.createServer(tlsOptions, app);
85
httpsServer.listen(config.httpsPort, '0.0.0.0', () =>
86
{
87
console.log(`File manager running at https://localhost:${config.httpsPort}`);
88
});
89
}
90
91
const shutdownAll = () => shutdown(httpServer, httpsServer);
92
61
93
// Give the admin route access to the server handle for graceful shutdown.
62
app.set('server', server);
63
app.set('shutdown', () => shutdown(server));
94
app.set('server', httpServer);
95
app.set('shutdown', shutdownAll);
64
96
65
process.on('SIGINT', () => shutdown(server));
66
process.on('SIGTERM', () => shutdown(server));
97
process.on('SIGINT', shutdownAll);
98
process.on('SIGTERM', shutdownAll);
67
99
}
68
100
69
101
try
@@ -2,6 +2,9 @@2
2
3
const express = require('express');3
const express = require('express');4
const path = require('path');4
const path = require('path');5
const fs = require('fs');6
const http = require('http');7
const https = require('https');5
const { initDb, closeDb } = require('./db');8
const { initDb, closeDb } = require('./db');6
const { ensureServerKeys } = require('./crypto');9
const { ensureServerKeys } = require('./crypto');7
const { loadConfig } = require('./config');10
const { loadConfig } = require('./config');@@ -25,18 +28,22 @@ app.get('*', (req, res) =>25
});28
});26
29
27
// Exposed so the shutdown route can trigger a graceful stop.30
// Exposed so the shutdown route can trigger a graceful stop.28
function shutdown (server)31
function shutdown (...servers)29
{32
{30
if (!server || server.__isShuttingDown) return;33
const active = servers.filter(Boolean);31
server.__isShuttingDown = true;34
if (active.length === 0 || active[0].__isShuttingDown) return;35
active.forEach(s => { s.__isShuttingDown = true; });32
36
33
// Close DB first so file locks are released as early as possible.37
// Close DB first so file locks are released as early as possible.34
closeDb();38
closeDb();35
39
36
server.close(() =>40
let closed = 0;41
function onClose ()37
{42
{38
process.exit(0);43
if (++closed === active.length) process.exit(0);39
});44
}45
46
active.forEach(s => s.close(onClose));40
47
41
// Force-exit after 5 s if connections don't drain in time.48
// Force-exit after 5 s if connections don't drain in time.42
setTimeout(() =>49
setTimeout(() =>@@ -53,17 +60,42 @@ function start ()53
const config = loadConfig();60
const config = loadConfig();54
const port = config.port || 3000;61
const port = config.port || 3000;55
62
56
const server = app.listen(port, '0.0.0.0', () =>63
const httpServer = http.createServer(app);64
httpServer.listen(port, '0.0.0.0', () =>57
{65
{58
console.log(`File manager running at http://localhost:${port}`);66
console.log(`File manager running at http://localhost:${port}`);59
});67
});60
68
69
let httpsServer = null;70
if (config.httpsPort && config.tlsCert && config.tlsKey)71
{72
let tlsOptions;73
try74
{75
tlsOptions = {76
cert: fs.readFileSync(config.tlsCert),77
key: fs.readFileSync(config.tlsKey),78
};79
} catch (err)80
{81
console.error('Failed to read TLS certificate/key:', err.message);82
process.exit(1);83
}84
httpsServer = https.createServer(tlsOptions, app);85
httpsServer.listen(config.httpsPort, '0.0.0.0', () =>86
{87
console.log(`File manager running at https://localhost:${config.httpsPort}`);88
});89
}90
91
const shutdownAll = () => shutdown(httpServer, httpsServer);92
61
// Give the admin route access to the server handle for graceful shutdown.93
// Give the admin route access to the server handle for graceful shutdown.62
app.set('server', server);94
app.set('server', httpServer);63
app.set('shutdown', () => shutdown(server));95
app.set('shutdown', shutdownAll);64
96
65
process.on('SIGINT', () => shutdown(server));97
process.on('SIGINT', shutdownAll);66
process.on('SIGTERM', () => shutdown(server));98
process.on('SIGTERM', shutdownAll);67
}99
}68
100
69
try101
try