hostr/app.js

104 lines
2.4 KiB
JavaScript
Raw Permalink Normal View History

2025-05-30 09:03:26 +01:00
import path from "path";
import Koa from "koa";
import logger from "koa-logger";
import serve from "koa-static";
import favicon from "koa-favicon";
import compress from "koa-compress";
import bodyparser from "koa-bodyparser";
import websockify from "koa-websocket";
import helmet from "koa-helmet";
import session from "koa-session";
import * as Sentry from "@sentry/node";
import debugname from "debug";
import * as redis from "./lib/redis";
import api, { ws } from "./api/app";
import web from "./web/app";
import { constants } from "zlib";
2015-07-09 23:01:43 +01:00
2025-05-30 09:03:26 +01:00
const debug = debugname("hostr");
2015-07-09 23:01:43 +01:00
2018-06-02 15:50:39 +00:00
const app = websockify(new Koa());
2015-08-30 18:35:05 +02:00
app.keys = [process.env.COOKIE_KEY];
2015-08-23 01:05:20 +01:00
2019-01-14 22:43:10 +00:00
app.use(bodyparser());
2015-08-22 16:16:15 +01:00
if (process.env.SENTRY_DSN) {
2019-01-14 21:53:36 +00:00
Sentry.init({
dsn: process.env.SENTRY_DSN,
2019-01-14 22:06:27 +00:00
release: process.env.GIT_REV,
2019-01-14 21:53:36 +00:00
});
2019-01-14 22:43:10 +00:00
app.context.Sentry = Sentry;
2015-08-22 16:16:15 +01:00
}
2015-07-09 23:01:43 +01:00
2015-08-22 18:24:39 +01:00
app.use(helmet());
2018-06-02 15:50:39 +00:00
app.use(async (ctx, next) => {
2025-05-30 09:03:26 +01:00
ctx.set("Server", "Nintendo 64");
if (ctx.req.headers["x-forwarded-proto"] === "http") {
2018-06-02 19:42:45 +00:00
ctx.redirect(`https://${ctx.req.headers.host}${ctx.req.url}`);
2016-06-06 15:37:00 +01:00
return;
2015-08-22 16:16:15 +01:00
}
2015-08-23 01:05:20 +01:00
try {
2018-06-02 15:50:39 +00:00
await next();
2015-08-23 01:05:20 +01:00
} catch (err) {
2018-06-02 19:01:31 +00:00
if (!err.statusCode && process.env.SENTRY_DSN) {
2019-01-14 21:37:03 +00:00
Sentry.captureException(err, (_err, eventId) => {
2025-05-30 09:03:26 +01:00
debug("Reported error", eventId);
2019-01-14 21:37:03 +00:00
});
2015-08-23 01:05:20 +01:00
}
throw err;
}
2015-08-22 16:16:15 +01:00
});
2018-06-02 15:50:39 +00:00
app.use(session(app));
2015-08-22 18:24:39 +01:00
app.use(redis.middleware());
2025-05-30 09:03:26 +01:00
if (app.env === "development") {
2019-01-13 17:48:27 +00:00
app.use(logger());
}
2025-05-30 09:03:26 +01:00
app.use(
compress({
filter(content_type) {
return /text/i.test(content_type);
},
threshold: 2048,
gzip: {
flush: constants.Z_SYNC_FLUSH,
},
deflate: {
flush: constants.Z_SYNC_FLUSH,
},
br: false,
}),
);
2015-08-22 16:16:15 +01:00
2025-05-30 09:03:26 +01:00
app.use(favicon(path.join(__dirname, "web/public/images/favicon.png")));
app.use(serve(path.join(__dirname, "web/public/"), { maxage: 31536000000 }));
2015-08-22 16:16:15 +01:00
2025-05-30 09:03:26 +01:00
app.use(api.prefix("/api").routes());
app.use(web.prefix("").routes());
2015-07-09 23:01:43 +01:00
2015-08-23 01:05:20 +01:00
app.ws.use(redis.middleware());
2025-05-30 09:03:26 +01:00
app.ws.use(ws.prefix("/api").routes());
2015-08-23 01:05:20 +01:00
2025-05-30 09:03:26 +01:00
app.on("error", (err, ctx) => {
2019-01-14 22:53:00 +00:00
if (err.statusCode === 404) return;
if (process.env.SENTRY_DSN) {
2025-05-30 09:03:26 +01:00
Sentry.withScope(function (scope) {
scope.addEventProcessor(function (event) {
return Sentry.Handlers.parseRequest(event, ctx.request);
});
Sentry.captureException(err);
2019-01-14 22:53:00 +00:00
});
}
2020-06-14 22:29:04 +01:00
debug(err);
2019-01-14 22:53:00 +00:00
});
2015-07-09 23:01:43 +01:00
if (!module.parent) {
2016-06-06 15:37:00 +01:00
app.listen(process.env.PORT || 4040, () => {
2025-05-30 09:03:26 +01:00
debug("Koa HTTP server listening on port ", process.env.PORT || 4040);
2015-08-08 20:37:49 +01:00
});
2015-07-09 23:01:43 +01:00
}
2015-08-11 21:54:55 +01:00
export default app;