-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathapp.js
More file actions
110 lines (88 loc) · 2.98 KB
/
app.js
File metadata and controls
110 lines (88 loc) · 2.98 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
// Load .env for Node.js
require("dotenv").config();
// Main application entry point
const express = require("express");
const fs = require("fs");
const { generateKeyPair } = require("daku");
// Import modules
const {
PORT,
HOST,
UPLOAD_TEMP_DIR,
ALLOWED_USERS,
} = require("./modules/config");
const { authMiddleware } = require("./modules/auth");
const { setupMiddleware, upload, errorHandler } = require("./modules/middleware");
const {
healthHandler,
statusHandler,
uploadHandler,
uploadZipHandler,
pinsHandler,
remoteUploadHandler,
pinAddHandler,
pinListHandler,
pinRemoveHandler,
} = require("./modules/routes");
const { refreshGateways } = require("./modules/gateways");
// Ensure temp directory exists
if (!fs.existsSync(UPLOAD_TEMP_DIR)) {
fs.mkdirSync(UPLOAD_TEMP_DIR, { recursive: true });
}
// Initialize Express app
const app = express();
// Setup middleware
setupMiddleware(app);
// Auto-generate a Daku keypair when no ALLOWED_USERS provided
if (ALLOWED_USERS.length === 0) {
const keys = generateKeyPair();
ALLOWED_USERS.push(keys.publicKey);
console.log("[PIN] ALLOWED_USERS not set. Generated a Daku keypair for you:");
console.log(`[PIN] publicKey=${keys.publicKey}`);
console.log(`[PIN] privateKey=${keys.privateKey}`);
console.log("[PIN] Pin management routes enabled for this public key.");
}
// API Routes
app.get("/health", healthHandler);
app.get("/status", statusHandler);
app.get("/api/pins", pinsHandler);
app.post("/upload", upload.single("file"), uploadHandler);
app.post("/uploadzip", upload.single("file"), uploadZipHandler);
app.post("/remoteupload", remoteUploadHandler);
// Authenticated Pin Routes (only when ALLOWED_USERS is configured)
if (ALLOWED_USERS.length > 0) {
app.post("/pin/add", authMiddleware, pinAddHandler);
app.get("/pin/list", authMiddleware, pinListHandler);
app.post("/pin/remove", authMiddleware, pinRemoveHandler);
} else {
console.log("[PIN] Pin management routes disabled (ALLOWED_USERS not set)");
}
// Apply error handler
app.use(errorHandler);
// Start server
const server = app.listen(PORT, HOST, () => {
console.log(`[STARTUP] SERVER_LISTENING host=${HOST} port=${PORT} url=http://${HOST}:${PORT}`);
});
// Warm gateway cache and probe every minute
const scheduleGatewayRefresh = () => {
refreshGateways().catch((err) => {
console.warn(`[GATEWAY] Refresh failed: ${err.message}`);
});
};
scheduleGatewayRefresh();
setInterval(scheduleGatewayRefresh, 60 * 1000);
// Graceful shutdown handler
const gracefulShutdown = (signal) => {
console.log(`[SHUTDOWN] SIGNAL_RECEIVED signal=${signal} action=graceful_shutdown`);
// Stop accepting new connections
server.close(() => {
console.log(`[SHUTDOWN] HTTP_SERVER_CLOSED`);
});
// Give active requests 5 seconds to complete
setTimeout(() => {
console.log(`[SHUTDOWN] FORCE_EXIT timeout_sec=5`);
process.exit(0);
}, 5000);
};
process.on("SIGTERM", () => gracefulShutdown("SIGTERM"));
process.on("SIGINT", () => gracefulShutdown("SIGINT"));