From 0d3562de76db5cc003fc55a8f92705ba5bd54540 Mon Sep 17 00:00:00 2001 From: The Arcane Brony Date: Sat, 18 Sep 2021 16:13:15 +0000 Subject: Add request logging (with env var: log-requests) Request logging is enabled when the log-request environment variable is set. --- api/src/Server.ts | 9 +++++++++ 1 file changed, 9 insertions(+) (limited to 'api/src') diff --git a/api/src/Server.ts b/api/src/Server.ts index b9ca3fba..318d7094 100644 --- a/api/src/Server.ts +++ b/api/src/Server.ts @@ -11,6 +11,7 @@ import path from "path"; import { initRateLimits } from "./middlewares/RateLimit"; import TestClient from "./middlewares/TestClient"; import { initTranslation } from "./middlewares/Translation"; +import morgan from "morgan"; export interface FosscordServerOptions extends ServerOptions {} @@ -36,6 +37,11 @@ export class FosscordServer extends Server { await Config.init(); await initEvent(); + let logRequests = process.env["log-requests"] != undefined; + if(logRequests) { + this.app.use(morgan("combined")); + } + this.app.use(CORS); this.app.use(BodyParser({ inflate: true, limit: "10mb" })); @@ -65,6 +71,9 @@ export class FosscordServer extends Server { this.app.use(ErrorHandler); TestClient(this.app); + if(logRequests){ + console.log("Warning: Request logging is enabled! This will spam your console!\nTo disable this, unset the 'log-requests' environment variable!"); + } return super.start(); } } -- cgit 1.5.1 From 0d41e18065131958de98c6dc86a564893115ebff Mon Sep 17 00:00:00 2001 From: The Arcane Brony Date: Sat, 18 Sep 2021 18:43:00 +0000 Subject: Add response code white/blacklisting --- api/src/Server.ts | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) (limited to 'api/src') diff --git a/api/src/Server.ts b/api/src/Server.ts index 318d7094..bda19db5 100644 --- a/api/src/Server.ts +++ b/api/src/Server.ts @@ -1,3 +1,4 @@ +import { OptionsJson } from 'body-parser'; import "missing-native-js-functions"; import { Connection } from "mongoose"; import { Server, ServerOptions } from "lambert-server"; @@ -37,9 +38,27 @@ export class FosscordServer extends Server { await Config.init(); await initEvent(); + + /* + DOCUMENTATION: uses log-requests environment variable + + # only log 200 and 204 + log-requests=200 204 + # log everything except 200 and 204 + log-requests=-200 204 + # log all requests + log-requests=- + */ + let logRequests = process.env["log-requests"] != undefined; if(logRequests) { - this.app.use(morgan("combined")); + this.app.use(morgan("combined", { + skip: (req, res) => { + var skip = !(process.env["log-requests"]?.includes(res.statusCode.toString()) ?? false); + if(process.env["log-requests"]?.charAt(0) == '-') skip = !skip; + return skip; + } + })); } this.app.use(CORS); -- cgit 1.5.1