-
Notifications
You must be signed in to change notification settings - Fork 422
Expand file tree
/
Copy pathworker.js
More file actions
121 lines (102 loc) · 3.96 KB
/
worker.js
File metadata and controls
121 lines (102 loc) · 3.96 KB
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
import fs from 'node:fs';
import path from 'node:path';
import { fileURLToPath } from 'node:url';
import express from 'express';
import dotenv from 'dotenv';
import morgan from 'morgan';
import { createStream } from 'rotating-file-stream';
import { createApp } from './src/backend/app.js';
import { resolveRuntimeDbPath } from './src/backend/db.js';
const __dirname = path.dirname(fileURLToPath(import.meta.url));
dotenv.config({ path: path.join(__dirname, '.env') });
const PORT = process.env.PORT;
const isProduction = process.env.NODE_ENV === 'production';
async function startServer() {
const { db, app: apiApp } = await createApp();
const app = express();
// Ensure logs directory exists
const logDirectory = path.join(__dirname, 'logs');
if (!fs.existsSync(logDirectory)) {
fs.mkdirSync(logDirectory, { recursive: true });
}
// Filename generator for daily rotation: prefix-dd-mm-yyyy.log
const createLogFilename = (prefix) => (time, index) => {
const t = time || new Date();
const d = String(t.getDate()).padStart(2, '0');
const m = String(t.getMonth() + 1).padStart(2, '0');
const y = t.getFullYear();
const idx = index > 1 ? `-${index}` : '';
return `${prefix}-${d}-${m}-${y}${idx}.log`;
};
// Create daily rotating write streams with GZIP compression
const accessLogStream = createStream(createLogFilename('access'), {
interval: '1d', // rotate daily
size: '100M', // also rotate if file exceeds 100MB
path: logDirectory,
compress: 'gzip', // natively gzip archives (~90% compression)
maxFiles: 14 // standard 2 weeks retention for access logs
});
const errorLogStream = createStream(createLogFilename('error'), {
interval: '1d',
size: '20M',
path: logDirectory,
compress: 'gzip',
maxFiles: 30 // keep silent errors longer for debugging
});
// Use 'short' format to drastically save horizontal log size without losing crucial endpoint info
app.use(morgan('short', { stream: accessLogStream }));
// Log only errors (status >= 400) to error stream
app.use(morgan('short', {
stream: errorLogStream,
skip: (req, res) => res.statusCode < 400
}));
// Cleaner, colorized format for the immediate console
app.use(morgan('dev'));
// Mount the secure API under root (the routers inside already prefix /api)
app.use('/', apiApp);
if (!isProduction) {
// Development mode: use Vite's middleware
const { createServer } = await import('vite');
const vite = await createServer({
server: { middlewareMode: true },
appType: 'spa',
});
app.use(vite.middlewares);
console.log('[Worker] Vite development server attached.');
} else {
// Production mode: serve static files natively via Express
const distPath = path.join(__dirname, 'dist');
if (!fs.existsSync(distPath)) {
console.warn('[Worker] WARNING: dist directory not found! Run `npm run build` for production.');
} else {
app.use(express.static(distPath));
app.get(/(.*)/, (req, res) => {
res.sendFile(path.join(distPath, 'index.html'));
});
console.log('[Worker] Static production files served from /dist');
}
}
const server = app.listen(PORT, () => {
console.log(`[Worker] Orchestrator listening on http://127.0.0.1:${PORT}`);
console.log(`[Worker] Environment: ${isProduction ? 'Production' : 'Development'}`);
console.log(`[Worker] SQLite Runtime Path: ${resolveRuntimeDbPath()}`);
});
function shutdown(signal) {
console.log(`\n[Worker] ${signal} received, shutting down...`);
server.close(() => {
db.close();
console.log('[Worker] DB closed. Exiting.');
process.exit(0);
});
setTimeout(() => {
console.error('[Worker] Force closing...');
process.exit(1);
}, 5000).unref();
}
process.on('SIGINT', () => shutdown('SIGINT'));
process.on('SIGTERM', () => shutdown('SIGTERM'));
}
startServer().catch(err => {
console.error('[Worker] Fatal error starting server:', err);
process.exit(1);
});