Removed web project (galvanic authentication support in IL2CPP universal patch)
Moved instance ID to header User instances for profile management .. other stuff
This commit is contained in:
101
src/main.ts
101
src/main.ts
@@ -1,11 +1,13 @@
|
||||
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";
|
||||
import { generateRandomString } from "./apiutils.ts";
|
||||
// @ts-types = "npm:@types/express"
|
||||
import express from "express";
|
||||
|
||||
const instanceId = generateRandomString(64);
|
||||
|
||||
const log = new Log.default("Main");
|
||||
|
||||
@@ -17,54 +19,18 @@ if (typeof config == 'undefined') {
|
||||
log.e('Cannot start: Configuration is undefined');
|
||||
Deno.exit(1);
|
||||
}
|
||||
if (config.secrets.authSecret == Config.defaultConfig.secrets.authSecret) {
|
||||
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;
|
||||
|
||||
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) {
|
||||
@@ -72,9 +38,53 @@ try {
|
||||
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');
|
||||
|
||||
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((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}`);
|
||||
log.n(`Listening on http://${config.web.host}:${config.web.port}`);
|
||||
|
||||
let shuttingDown = false;
|
||||
Deno.addSignalListener('SIGINT', () => {
|
||||
@@ -83,7 +93,6 @@ try {
|
||||
log.i(`Shutting down`);
|
||||
|
||||
http.close();
|
||||
http.removeAllListeners();
|
||||
});
|
||||
});
|
||||
} catch (err) {
|
||||
|
||||
Reference in New Issue
Block a user