Harden bot, storage and web API
This commit is contained in:
parent
e974bc6660
commit
20624a5c9c
43 changed files with 1265 additions and 1012 deletions
|
|
@ -1,4 +1,7 @@
|
|||
import express from "express";
|
||||
import helmet from "helmet";
|
||||
import { rateLimit } from "express-rate-limit";
|
||||
import multer from "multer";
|
||||
import path from "path";
|
||||
import { config } from "../config.js";
|
||||
import { authMiddleware } from "./middlewares/auth.js";
|
||||
|
|
@ -6,19 +9,56 @@ import { libraryRouter } from "./routes/library.js";
|
|||
import { playerRouter } from "./routes/player.js";
|
||||
import { queueRouter } from "./routes/queue.js";
|
||||
import { uploadsRouter } from "./routes/uploads.js";
|
||||
import { ensureStorageLayout } from "./storage/storageManager.js";
|
||||
import {
|
||||
cleanupTemporaryFiles,
|
||||
ensureStorageLayout,
|
||||
} from "./storage/storageManager.js";
|
||||
|
||||
const PUBLIC_DIR = path.join(process.cwd(), "public");
|
||||
|
||||
function startWebServer(dependencies) {
|
||||
ensureStorageLayout();
|
||||
cleanupTemporaryFiles();
|
||||
|
||||
const app = express();
|
||||
app.use(express.json());
|
||||
app.disable("x-powered-by");
|
||||
app.use(helmet());
|
||||
app.use(express.json({ limit: "32kb" }));
|
||||
|
||||
app.get("/healthz", (_req, res) => {
|
||||
res.json({ status: "ok" });
|
||||
});
|
||||
|
||||
const apiRouter = express.Router();
|
||||
apiRouter.use(
|
||||
rateLimit({
|
||||
windowMs: 60_000,
|
||||
limit: 120,
|
||||
standardHeaders: "draft-8",
|
||||
legacyHeaders: false,
|
||||
})
|
||||
);
|
||||
apiRouter.use(
|
||||
rateLimit({
|
||||
windowMs: 15 * 60_000,
|
||||
limit: 20,
|
||||
skipSuccessfulRequests: true,
|
||||
standardHeaders: "draft-8",
|
||||
legacyHeaders: false,
|
||||
message: { error: "Trop de tentatives d'authentification" },
|
||||
})
|
||||
);
|
||||
apiRouter.use(authMiddleware);
|
||||
apiRouter.use("/uploads", uploadsRouter(dependencies));
|
||||
apiRouter.use(
|
||||
"/uploads",
|
||||
rateLimit({
|
||||
windowMs: 15 * 60_000,
|
||||
limit: 10,
|
||||
standardHeaders: "draft-8",
|
||||
legacyHeaders: false,
|
||||
}),
|
||||
uploadsRouter(dependencies)
|
||||
);
|
||||
apiRouter.use("/queue", queueRouter(dependencies));
|
||||
apiRouter.use("/library", libraryRouter(dependencies));
|
||||
apiRouter.use("/player", playerRouter(dependencies));
|
||||
|
|
@ -29,15 +69,28 @@ function startWebServer(dependencies) {
|
|||
);
|
||||
app.use(express.static(PUBLIC_DIR));
|
||||
|
||||
app.use((error, _req, res, _next) => {
|
||||
let status = Number(error?.status) || 500;
|
||||
if (error instanceof multer.MulterError) {
|
||||
status = error.code === "LIMIT_FILE_SIZE" ? 413 : 400;
|
||||
}
|
||||
if (status >= 500) {
|
||||
console.error("Unhandled HTTP error", error);
|
||||
}
|
||||
res.status(status).json({
|
||||
error: status >= 500 ? "Erreur interne du serveur" : error.message,
|
||||
});
|
||||
});
|
||||
|
||||
const port = Number(process.env.PORT || config.webServerPort || 3000);
|
||||
|
||||
app.listen(port, () => {
|
||||
const server = app.listen(port, () => {
|
||||
console.log(
|
||||
`[${new Date().toLocaleString()}] Web control server listening on port ${port}`
|
||||
);
|
||||
});
|
||||
|
||||
return app;
|
||||
return { app, server };
|
||||
}
|
||||
|
||||
export { startWebServer };
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue