Include resource directory Ran `deno fmt` with 4 space indent, that changed every file (!!!!!) various changes
119 lines
3.4 KiB
TypeScript
119 lines
3.4 KiB
TypeScript
import * as Log from "@proxnet/undead-logging";
|
|
import * as Config from "./config.ts";
|
|
import { Database } from "./db.ts";
|
|
import { APIUtils } from "./apiutils.ts";
|
|
import { Discord } from "./discord.ts";
|
|
import { generateRandomString } from "./apiutils.ts";
|
|
// @ts-types = "npm:@types/express"
|
|
import express from "express";
|
|
|
|
const instanceId = generateRandomString(64);
|
|
|
|
const log = new Log.default("Main");
|
|
|
|
log.i(`Starting Galvanic Corrosion..`);
|
|
|
|
const config = Config.getConfig();
|
|
|
|
if (typeof config == "undefined") {
|
|
log.e("Cannot start: Configuration is undefined");
|
|
Deno.exit(1);
|
|
}
|
|
if (config.auth.secret == Config.defaultConfig.auth.secret) {
|
|
log.e(
|
|
`Cannot start: Auth secret is default. Please change 'secrets.authSecret' in 'config.json'`,
|
|
);
|
|
Deno.exit(1);
|
|
}
|
|
if (config.public.serverId == Config.defaultConfig.public.serverId) {
|
|
log.e(
|
|
`Cannot start: Server ID is default. Please change 'public.serverId' in 'config.json'`,
|
|
);
|
|
Deno.exit(1);
|
|
}
|
|
|
|
Log.MessageTypeVisibility.Network = config.logging.network;
|
|
Log.MessageTypeVisibility.Debug = config.logging.debug;
|
|
|
|
try {
|
|
Database.connect();
|
|
} catch (err) {
|
|
log.e(`Cannot start: Redis could not be initialized. ${err}`);
|
|
Deno.exit(1);
|
|
}
|
|
|
|
const port = config.web.port;
|
|
const host = config.web.host;
|
|
|
|
log.n(`Starting HTTP server on http://${host}:${port}`);
|
|
|
|
const app = express();
|
|
|
|
app.disable("etag");
|
|
app.disable("x-powered-by");
|
|
|
|
app.use(
|
|
(rq: express.Request, rs: express.Response, nxt: express.NextFunction) => {
|
|
rs.setHeader("Instance", instanceId);
|
|
log.n(`${APIUtils.getSrcIpDefault(rq)} ${rq.method} ${rq.originalUrl}`);
|
|
nxt();
|
|
},
|
|
);
|
|
|
|
app.get("/info", (_rq, rs) => {
|
|
rs.json({
|
|
name: config.public.serverName,
|
|
id: config.public.serverId,
|
|
motd: config.public.motd,
|
|
patches: config.public.patches,
|
|
});
|
|
});
|
|
|
|
// content routes
|
|
const nameserverRouter = await import("./routes/nameserver.ts");
|
|
const apiRouter = await import("./routes/api.ts");
|
|
const userRouter = await import("./routes/user.ts");
|
|
const authRouter = await import("./routes/auth.ts");
|
|
const accountRouter = await import("./routes/account.ts");
|
|
const imgRouter = await import("./routes/img.ts");
|
|
|
|
app.use(nameserverRouter.route.path, nameserverRouter.route.router);
|
|
app.use(apiRouter.route.path, apiRouter.route.router);
|
|
app.use(userRouter.route.path, userRouter.route.router);
|
|
app.use(authRouter.route.path, authRouter.route.router);
|
|
app.use(accountRouter.route.path, accountRouter.route.router);
|
|
app.use(imgRouter.route.path, imgRouter.route.router);
|
|
|
|
app.use((rq: express.Request, rs: express.Response) => {
|
|
log.e(
|
|
`${APIUtils.getSrcIpDefault(rq)} 404 ${rq.method} ${rq.url.toString()}`,
|
|
);
|
|
rs.statusCode = 404;
|
|
rs.json(
|
|
APIUtils.genericResponseFormat(
|
|
true,
|
|
"Endpoint not found. Check your syntax and/or method.",
|
|
),
|
|
);
|
|
});
|
|
|
|
try {
|
|
const http = app.listen(config.web.port, config.web.host, () => {
|
|
log.n(`Listening on http://${config.web.host}:${config.web.port}`);
|
|
|
|
let shuttingDown = false;
|
|
Deno.addSignalListener("SIGINT", () => {
|
|
if (shuttingDown) return;
|
|
shuttingDown = true;
|
|
log.i(`Shutting down`);
|
|
|
|
http.close();
|
|
});
|
|
});
|
|
} catch (err) {
|
|
log.e(`Cannot start: Network could not be initalized. ${err}`);
|
|
Deno.exit(1);
|
|
}
|
|
|
|
Discord.login();
|