2021-01-04 16:10:12 +01:00
|
|
|
import { Server, ServerOptions } from "lambert-server";
|
2021-08-29 16:58:56 +02:00
|
|
|
import { Config, initDatabase } from "@fosscord/util";
|
2021-05-27 18:54:15 +02:00
|
|
|
import path from "path";
|
2021-08-07 13:15:35 +02:00
|
|
|
import avatarsRoute from "./routes/avatars";
|
2021-09-09 16:55:02 +02:00
|
|
|
import bodyParser from "body-parser";
|
2020-12-30 22:49:01 +01:00
|
|
|
|
2021-05-27 18:54:15 +02:00
|
|
|
export interface CDNServerOptions extends ServerOptions {}
|
2021-01-04 16:10:12 +01:00
|
|
|
|
|
|
|
export class CDNServer extends Server {
|
2021-08-15 14:40:59 +02:00
|
|
|
public declare options: CDNServerOptions;
|
2021-01-04 16:10:12 +01:00
|
|
|
|
2021-05-27 18:54:15 +02:00
|
|
|
constructor(options?: Partial<CDNServerOptions>) {
|
2021-01-04 16:10:12 +01:00
|
|
|
super(options);
|
2020-12-30 22:49:01 +01:00
|
|
|
}
|
|
|
|
|
2021-01-04 16:10:12 +01:00
|
|
|
async start() {
|
2021-08-29 16:58:56 +02:00
|
|
|
await initDatabase();
|
2021-05-27 18:54:15 +02:00
|
|
|
await Config.init();
|
2021-08-07 15:51:46 +02:00
|
|
|
this.app.use((req, res, next) => {
|
|
|
|
res.set("Access-Control-Allow-Origin", "*");
|
|
|
|
// TODO: use better CSP policy
|
|
|
|
res.set(
|
|
|
|
"Content-security-policy",
|
|
|
|
"default-src * data: blob: filesystem: about: ws: wss: 'unsafe-inline' 'unsafe-eval'; script-src * data: blob: 'unsafe-inline' 'unsafe-eval'; connect-src * data: blob: 'unsafe-inline'; img-src * data: blob: 'unsafe-inline'; frame-src * data: blob: ; style-src * data: blob: 'unsafe-inline'; font-src * data: blob: 'unsafe-inline';"
|
|
|
|
);
|
|
|
|
res.set("Access-Control-Allow-Headers", req.header("Access-Control-Request-Headers") || "*");
|
|
|
|
res.set("Access-Control-Allow-Methods", req.header("Access-Control-Request-Methods") || "*");
|
|
|
|
next();
|
|
|
|
});
|
2021-09-09 16:55:02 +02:00
|
|
|
this.app.use(bodyParser.json({ inflate: true, limit: "10mb" }));
|
2021-05-27 18:54:15 +02:00
|
|
|
|
2021-05-28 21:19:19 +02:00
|
|
|
await this.registerRoutes(path.join(__dirname, "routes/"));
|
2021-08-07 15:44:56 +02:00
|
|
|
|
2021-08-07 13:15:35 +02:00
|
|
|
this.app.use("/icons/", avatarsRoute);
|
2021-08-13 12:59:59 +02:00
|
|
|
this.log("verbose", "[Server] Route /icons registered");
|
2021-08-07 15:44:56 +02:00
|
|
|
|
|
|
|
this.app.use("/emojis/", avatarsRoute);
|
2021-08-13 12:59:59 +02:00
|
|
|
this.log("verbose", "[Server] Route /emojis registered");
|
2021-08-07 15:44:56 +02:00
|
|
|
|
2021-08-07 18:52:05 +02:00
|
|
|
this.app.use("/stickers/", avatarsRoute);
|
2021-08-13 12:59:59 +02:00
|
|
|
this.log("verbose", "[Server] Route /stickers registered");
|
2021-08-07 15:44:56 +02:00
|
|
|
|
|
|
|
this.app.use("/banners/", avatarsRoute);
|
2021-08-13 12:59:59 +02:00
|
|
|
this.log("verbose", "[Server] Route /banners registered");
|
|
|
|
|
2021-08-07 21:13:34 +02:00
|
|
|
this.app.use("/splashes/", avatarsRoute);
|
2021-08-13 12:59:59 +02:00
|
|
|
this.log("verbose", "[Server] Route /splashes registered");
|
2021-08-07 15:44:56 +02:00
|
|
|
|
2021-08-07 18:52:05 +02:00
|
|
|
this.app.use("/app-icons/", avatarsRoute);
|
2021-08-13 12:59:59 +02:00
|
|
|
this.log("verbose", "[Server] Route /app-icons registered");
|
2021-08-07 18:52:05 +02:00
|
|
|
|
|
|
|
this.app.use("/app-assets/", avatarsRoute);
|
2021-08-13 12:59:59 +02:00
|
|
|
this.log("verbose", "[Server] Route /app-assets registered");
|
2021-08-07 18:52:05 +02:00
|
|
|
|
2021-08-07 15:44:56 +02:00
|
|
|
this.app.use("/discover-splashes/", avatarsRoute);
|
2021-08-13 12:59:59 +02:00
|
|
|
this.log("verbose", "[Server] Route /discover-splashes registered");
|
2021-08-07 15:44:56 +02:00
|
|
|
|
|
|
|
this.app.use("/team-icons/", avatarsRoute);
|
2021-08-13 12:59:59 +02:00
|
|
|
this.log("verbose", "[Server] Route /team-icons registered");
|
2021-08-07 15:44:56 +02:00
|
|
|
|
2021-09-17 18:29:02 +02:00
|
|
|
this.app.use("/channel-icons/", avatarsRoute);
|
|
|
|
this.log("verbose", "[Server] Route /channel-icons registered");
|
|
|
|
|
2021-01-04 16:10:12 +01:00
|
|
|
return super.start();
|
2020-12-30 22:49:01 +01:00
|
|
|
}
|
|
|
|
|
2021-01-04 16:10:12 +01:00
|
|
|
async stop() {
|
|
|
|
return super.stop();
|
2020-12-30 22:49:01 +01:00
|
|
|
}
|
|
|
|
}
|