add log incoming middleware

This commit is contained in:
Milo Schwartz 2024-10-26 17:19:10 -04:00
parent be144fe15d
commit 6cee5703b5
No known key found for this signature in database
5 changed files with 124 additions and 109 deletions

View file

@ -1,5 +1,4 @@
app: app:
name: Pangolin
base_url: http://localhost:3000 base_url: http://localhost:3000
log_level: warning log_level: warning
save_logs: false save_logs: false

View file

@ -14,7 +14,6 @@
"email": "email dev --dir server/emails/templates --port 3002" "email": "email dev --dir server/emails/templates --port 3002"
}, },
"dependencies": { "dependencies": {
"@esbuild-plugins/tsconfig-paths": "0.1.2",
"@hookform/resolvers": "3.9.0", "@hookform/resolvers": "3.9.0",
"@node-rs/argon2": "1.8.3", "@node-rs/argon2": "1.8.3",
"@oslojs/crypto": "1.0.1", "@oslojs/crypto": "1.0.1",
@ -43,8 +42,6 @@
"cookie-parser": "1.4.6", "cookie-parser": "1.4.6",
"cors": "2.8.5", "cors": "2.8.5",
"drizzle-orm": "0.33.0", "drizzle-orm": "0.33.0",
"esbuild": "0.20.1",
"esbuild-node-externals": "1.13.0",
"express": "4.21.0", "express": "4.21.0",
"express-rate-limit": "7.4.0", "express-rate-limit": "7.4.0",
"glob": "11.0.0", "glob": "11.0.0",
@ -67,11 +64,14 @@
"tailwindcss-animate": "1.0.7", "tailwindcss-animate": "1.0.7",
"winston": "3.14.2", "winston": "3.14.2",
"winston-daily-rotate-file": "5.0.0", "winston-daily-rotate-file": "5.0.0",
"yargs": "17.7.2",
"zod": "3.23.8", "zod": "3.23.8",
"zod-validation-error": "3.4.0" "zod-validation-error": "3.4.0"
}, },
"devDependencies": { "devDependencies": {
"esbuild": "0.20.1",
"esbuild-node-externals": "1.13.0",
"yargs": "17.7.2",
"@esbuild-plugins/tsconfig-paths": "0.1.2",
"@dotenvx/dotenvx": "1.14.2", "@dotenvx/dotenvx": "1.14.2",
"@types/better-sqlite3": "7.6.11", "@types/better-sqlite3": "7.6.11",
"@types/cookie-parser": "1.4.7", "@types/cookie-parser": "1.4.7",

View file

@ -15,6 +15,7 @@ import { authenticated, unauthenticated } from "@server/routers/external";
import cookieParser from "cookie-parser"; import cookieParser from "cookie-parser";
import { User } from "@server/db/schema"; import { User } from "@server/db/schema";
import { ensureActions } from "./db/ensureActions"; import { ensureActions } from "./db/ensureActions";
import { logIncomingMiddleware } from "./middlewares/logIncoming";
const dev = process.env.ENVIRONMENT !== "prod"; const dev = process.env.ENVIRONMENT !== "prod";
@ -25,7 +26,6 @@ const externalPort = config.server.external_port;
const internalPort = config.server.internal_port; const internalPort = config.server.internal_port;
app.prepare().then(() => { app.prepare().then(() => {
ensureActions(); // This loads the actions into the database ensureActions(); // This loads the actions into the database
// External server // External server
@ -42,15 +42,18 @@ app.prepare().then(() => {
windowMin: config.rate_limit.window_minutes, windowMin: config.rate_limit.window_minutes,
max: config.rate_limit.max_requests, max: config.rate_limit.max_requests,
type: "IP_ONLY", type: "IP_ONLY",
}), })
); );
} }
const prefix = `/api/v1`; const prefix = `/api/v1`;
if (dev) {
externalServer.use(logIncomingMiddleware);
}
externalServer.use(prefix, unauthenticated); externalServer.use(prefix, unauthenticated);
externalServer.use(prefix, authenticated); externalServer.use(prefix, authenticated);
externalServer.use(notFoundMiddleware) externalServer.use(notFoundMiddleware);
// We are using NEXT from here on // We are using NEXT from here on
externalServer.all("*", (req: Request, res: Response) => { externalServer.all("*", (req: Request, res: Response) => {
@ -61,7 +64,7 @@ app.prepare().then(() => {
externalServer.listen(externalPort, (err?: any) => { externalServer.listen(externalPort, (err?: any) => {
if (err) throw err; if (err) throw err;
logger.info( logger.info(
`Main server is running on http://localhost:${externalPort}`, `Main server is running on http://localhost:${externalPort}`
); );
}); });
@ -80,15 +83,16 @@ app.prepare().then(() => {
internalServer.listen(internalPort, (err?: any) => { internalServer.listen(internalPort, (err?: any) => {
if (err) throw err; if (err) throw err;
logger.info( logger.info(
`Internal server is running on http://localhost:${internalPort}`, `Internal server is running on http://localhost:${internalPort}`
); );
}); });
internalServer.use(notFoundMiddleware) internalServer.use(notFoundMiddleware);
internalServer.use(errorHandlerMiddleware); internalServer.use(errorHandlerMiddleware);
}); });
declare global { // TODO: eventually make seperate types that extend express.Request declare global {
// TODO: eventually make seperate types that extend express.Request
namespace Express { namespace Express {
interface Request { interface Request {
user?: User; user?: User;

View file

@ -0,0 +1,14 @@
import logger from "@server/logger";
import { NextFunction, Request, Response } from "express";
export function logIncomingMiddleware(
req: Request,
res: Response,
next: NextFunction
) {
const { method, url, headers, body } = req;
if (url.includes("/api/v1")) {
logger.debug(`${method} ${url}`);
}
next();
}

View file

@ -91,8 +91,6 @@ export async function listResources(
next: NextFunction, next: NextFunction,
): Promise<any> { ): Promise<any> {
try { try {
logger.info(JSON.stringify(req.query, null, 2));
logger.info(JSON.stringify(req.params, null, 2));
const parsedQuery = listResourcesSchema.safeParse(req.query); const parsedQuery = listResourcesSchema.safeParse(req.query);
if (!parsedQuery.success) { if (!parsedQuery.success) {
return next( return next(