| 1 | 'use strict'; |
| 2 | |
| 3 | const express = require('express'); |
| 4 | const path = require('path'); |
| 5 | const fs = require('fs'); |
| 6 | const http = require('http'); |
| 7 | const https = require('https'); |
| 8 | const { initDb, closeDb } = require('./db'); |
| 9 | const { ensureServerKeys } = require('./crypto'); |
| 10 | const { loadConfig } = require('./config'); |
| 11 | const authRoutes = require('./routes/auth'); |
| 12 | const filesRoutes = require('./routes/files'); |
| 13 | const adminRoutes = require('./routes/admin'); |
| 14 | |
| 15 | const app = express(); |
| 16 | |
| 17 | app.use(express.json({ limit: '50mb' })); |
| 18 | app.use(express.static(path.join(__dirname, '../public'))); |
| 19 | |
| 20 | app.use('/api/auth', authRoutes); |
| 21 | app.use('/api/files', filesRoutes); |
| 22 | app.use('/api/admin', adminRoutes); |
| 23 | |
| 24 | // SPA catch-all (only for non-API routes) |
| 25 | app.get('*', (req, res) => |
| 26 | { |
| 27 | res.sendFile(path.join(__dirname, '../public/index.html')); |
| 28 | }); |
| 29 | |
| 30 | // Exposed so the shutdown route can trigger a graceful stop. |
| 31 | function shutdown (opts = {}, ...servers) |
| 32 | { |
| 33 | const exitCode = Number.isInteger(opts.exitCode) ? opts.exitCode : 0; |
| 34 | const active = servers.filter(Boolean); |
| 35 | if (active.length === 0 || active[0].__isShuttingDown) return; |
| 36 | active.forEach(s => { s.__isShuttingDown = true; }); |
| 37 | |
| 38 | // Close DB first so file locks are released as early as possible. |
| 39 | closeDb(); |
| 40 | |
| 41 | let closed = 0; |
| 42 | function onClose () |
| 43 | { |
| 44 | if (++closed === active.length) process.exit(exitCode); |
| 45 | } |
| 46 | |
| 47 | active.forEach(s => s.close(onClose)); |
| 48 | |
| 49 | // Force-exit after 5 s if connections don't drain in time. |
| 50 | setTimeout(() => |
| 51 | { |
| 52 | process.exit(exitCode); |
| 53 | }, 5000).unref(); |
| 54 | } |
| 55 | |
| 56 | function start () |
| 57 | { |
| 58 | initDb(); |
| 59 | ensureServerKeys(); |
| 60 | |
| 61 | const config = loadConfig(); |
| 62 | const port = config.port || 3000; |
| 63 | |
| 64 | const httpServer = http.createServer(app); |
| 65 | httpServer.listen(port, '0.0.0.0', () => |
| 66 | { |
| 67 | console.log(`File manager running at http://localhost:${port}`); |
| 68 | }); |
| 69 | |
| 70 | let httpsServer = null; |
| 71 | if (config.httpsPort && config.tlsCert && config.tlsKey) |
| 72 | { |
| 73 | let tlsOptions; |
| 74 | try |
| 75 | { |
| 76 | tlsOptions = { |
| 77 | cert: fs.readFileSync(config.tlsCert), |
| 78 | key: fs.readFileSync(config.tlsKey), |
| 79 | }; |
| 80 | } catch (err) |
| 81 | { |
| 82 | console.error('Failed to read TLS certificate/key:', err.message); |
| 83 | process.exit(1); |
| 84 | } |
| 85 | httpsServer = https.createServer(tlsOptions, app); |
| 86 | httpsServer.listen(config.httpsPort, '0.0.0.0', () => |
| 87 | { |
| 88 | console.log(`File manager running at https://localhost:${config.httpsPort}`); |
| 89 | }); |
| 90 | } |
| 91 | |
| 92 | const shutdownAll = () => shutdown({}, httpServer, httpsServer); |
| 93 | const shutdownForRestart = () => shutdown({ exitCode: 1 }, httpServer, httpsServer); |
| 94 | |
| 95 | // Give the admin route access to the server handle for graceful shutdown. |
| 96 | app.set('server', httpServer); |
| 97 | app.set('shutdown', shutdownAll); |
| 98 | app.set('shutdownForRestart', shutdownForRestart); |
| 99 | |
| 100 | process.on('SIGINT', shutdownAll); |
| 101 | process.on('SIGTERM', shutdownAll); |
| 102 | } |
| 103 | |
| 104 | try |
| 105 | { |
| 106 | start(); |
| 107 | } catch (err) |
| 108 | { |
| 109 | console.error('Failed to start server:', err); |
| 110 | process.exit(1); |
| 111 | } |