94 lines
2.8 KiB
TypeScript
94 lines
2.8 KiB
TypeScript
import * as Log from "@proxnet/undead-logging";
|
|
import * as Config from "./config.ts";
|
|
// @ts-types = 'npm:@types/express'
|
|
import express from "express";
|
|
import { Database } from "./db.ts";
|
|
import { APIUtils } from "./apiutils.ts";
|
|
import { Discord } from "./discord.ts";
|
|
import { User } from "./data/users.ts";
|
|
|
|
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.secrets.authSecret == Config.defaultConfig.secrets.authSecret) {
|
|
log.e(`Cannot start: Auth secret is default. Please change 'secrets.authSecret' in 'config.json'`);
|
|
Deno.exit(1);
|
|
}
|
|
|
|
Log.MessageTypeVisibility.Network = config.logging.network;
|
|
Log.MessageTypeVisibility.Debug = config.logging.debug;
|
|
|
|
const port = config.web.port;
|
|
const host = config.web.host;
|
|
|
|
log.i(`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.locals.auth = null;
|
|
log.n(`${APIUtils.getSrcIpDefault(rq)} ${rq.method} ${rq.originalUrl}`);
|
|
nxt();
|
|
});
|
|
|
|
app.get('/', APIUtils.genericResponse(false, `${config?.public.serverName} - ${config?.public.motd}`));
|
|
|
|
app.get('/debug', async (_rq, rs) => {
|
|
const user = await User.init({ username: "testuser123", password: "foopass123" });
|
|
log.i(String(user == null));
|
|
|
|
rs.sendStatus(200);
|
|
});
|
|
|
|
// content routes
|
|
const nameserverRouter = await import('./routes/nameserver.ts');
|
|
const apiRouter = await import('./routes/api.ts');
|
|
const userRouter = await import('./routes/user.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((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 {
|
|
Database.connect();
|
|
} catch (err) {
|
|
log.e(`Cannot start: Redis could not be initialized. ${err}`);
|
|
Deno.exit(1);
|
|
}
|
|
|
|
try {
|
|
const http = app.listen(config.web.port, config.web.host, () => {
|
|
log.n(`Listening on http://${config.web.host}`);
|
|
|
|
let shuttingDown = false;
|
|
Deno.addSignalListener('SIGINT', () => {
|
|
if (shuttingDown) return;
|
|
shuttingDown = true;
|
|
log.i(`Shutting down`);
|
|
|
|
http.close();
|
|
http.removeAllListeners();
|
|
});
|
|
});
|
|
} catch (err) {
|
|
log.e(`Cannot start: Network could not be initalized. ${err}`);
|
|
Deno.exit(1);
|
|
}
|
|
|
|
Discord.login(); |