EUS/index.ts
2025-01-05 14:22:18 +00:00

103 lines
No EOL
3 KiB
TypeScript

import Fastify from "fastify";
import FastifyFormBody from "@fastify/formbody";
import FastifyMultipart from "@fastify/multipart";
import FastifyCookie from "@fastify/cookie";
import FastifyView from "@fastify/view";
import FastifyStatic from "@fastify/static";
import Config from "./objects/Config";
import EJS from "ejs";
import { Console } from "hsconsole";
import Controller from "./controllers/Controller";
import HomeController from "./controllers/HomeController";
import Database from "./objects/Database";
import { join } from "path";
import AccountController from "./controllers/AccountController";
import { magenta, blue, cyan, green, red } from "dyetty";
import ConsoleUtility from "./utilities/ConsoleUtility";
import HashFS from "./objects/HashFS";
import { existsSync, mkdirSync, rmSync } from "fs";
Console.customHeader(`EUS server started at ${new Date()}`);
const fastify = Fastify({
logger: false
});
fastify.register(FastifyView, {
engine: {
ejs: EJS
}
});
fastify.register(FastifyFormBody);
fastify.register(FastifyMultipart);
fastify.register(FastifyCookie, {
secret: Config.session.secret,
parseOptions: {
path: "/",
secure: true
}
});
fastify.register(FastifyStatic, {
root: join(__dirname, "wwwroot"),
preCompressed: true
//prefix: `${Config.ports.web}/static/`
});
fastify.addHook("preValidation", (req, res, done) => {
// @ts-ignore
req.startTime = Date.now();
// * Take usual controller path if this path is registered.
if (Controller.RegisteredPaths.includes(req.url)) {
// @ts-ignore
req.logType = cyan("CONTROLLER");
return done();
} else {
// @ts-ignore
req.logType = magenta("STATIC");
}
done();
});
fastify.addHook("onSend", (req, res, _payload, done) => {
// @ts-ignore
Console.printInfo(`[ ${req.logType} ] [ ${ConsoleUtility.StatusColor(res.statusCode)} ] [ ${blue(`${Date.now() - req.startTime}ms`)} ] > ${req.url}`);
done();
});
fastify.setNotFoundHandler(async (req, res) => {
return res.status(404).view("views/404.ejs", { session: null });
});
HashFS.STARTUP_DIR = __dirname;
new HashFS("images");
if (Config.database.enabled) {
new Database(Config.database.address, Config.database.port, Config.database.username, Config.database.password, Config.database.name);
} else {
Console.printInfo(`[ ${red("DATABASE")} ] Database is disabled.`);
}
if (Config.controllers.enabled && Config.database.enabled) {
Controller.FastifyInstance = fastify;
new AccountController();
new HomeController();
} else {
Console.printInfo(`[ ${red("CONTROLLER")} ] Controllers are disabled${Config.controllers.enabled && !Config.database.enabled ? " because the database is disabled but required by the controllers." : "."} Server will operate in static mode only.`);
}
fastify.listen({ port: Config.hosts.webPort, host: Config.hosts.webHost }, (err, address) => {
if (err) {
Console.printError(`Error occured while spinning up fastify:\n${err}`);
process.exit(1);
}
Console.printInfo(`[ ${green("MAIN")} ] Listening at ${address.replace("0.0.0.0", "localhost").replace("127.0.0.1", "localhost")}`);
});