52 lines
1.9 KiB
TypeScript
Raw Normal View History

2023-05-19 15:22:48 -03:00
import express from "express";
import bodyParser from "body-parser";
import { unknownEndpointHandler } from "./middleware/middleware.ts";
import { requestLogger } from "./middleware/morgenMiddleware.ts";
import { errorHandler } from "./middleware/errorHandler.ts";
2023-05-19 15:22:48 -03:00
import { apiRouter } from "./routes/api.ts";
import { cacheRouter } from "./routes/cache.ts";
import { customRouter } from "./routes/custom.ts";
import { dynamicController } from "./routes/dynamic.ts";
import { payRouter } from "./routes/pay.ts";
import { statsRouter } from "./routes/stats.ts";
import { webuiRouter } from "./routes/webui.ts";
2023-05-19 15:22:48 -03:00
const app = express();
app.use((req, _res, next) => {
// 38.5.0 introduced "ezip" for encrypted body blobs and "e" for request verification only (encrypted body blobs with no application data).
// The client patch is expected to decrypt it for us but having an unsupported Content-Encoding here would still be an issue for Express, so removing it.
if (req.headers["content-encoding"] == "ezip" || req.headers["content-encoding"] == "e") {
req.headers["content-encoding"] = undefined;
}
// U18 uses application/x-www-form-urlencoded even tho the data is JSON which Express doesn't like.
// U17 sets no Content-Type at all, which Express also doesn't like.
if (!req.headers["content-type"] || req.headers["content-type"] == "application/x-www-form-urlencoded") {
req.headers["content-type"] = "application/octet-stream";
}
next();
});
2023-05-19 15:22:48 -03:00
app.use(bodyParser.raw());
2025-01-24 16:12:39 +01:00
app.use(express.json({ limit: "4mb" }));
app.use(bodyParser.text({ limit: "4mb" }));
2024-01-06 16:26:58 +01:00
app.use(requestLogger);
2023-05-19 15:22:48 -03:00
app.use("/api", apiRouter);
app.use("/", cacheRouter);
app.use("/custom", customRouter);
app.use("/dynamic", dynamicController);
2023-05-19 15:22:48 -03:00
app.use("/:id/dynamic", dynamicController);
app.use("/pay", payRouter);
app.use("/stats", statsRouter);
2024-05-04 14:44:23 +02:00
app.use("/", webuiRouter);
2023-05-19 15:22:48 -03:00
app.use(unknownEndpointHandler);
app.use(errorHandler);
2023-05-19 15:22:48 -03:00
export { app };