Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 27 additions & 14 deletions server/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -230,20 +230,17 @@ appRouter.use((req, res, next) => {
req.hostname.includes('localhost') ||
req.hostname.includes('127.0.0.1')
) {
console.log('SETTING LOCALHOST');
req.headers.localhost = req.headers.host;
if (process.env.PUBPUB_LOCAL_COMMUNITY) {
const subdomain = process.env.PUBPUB_LOCAL_COMMUNITY;
req.headers.host = `${subdomain}.duqduq.org`;
} else {
req.headers.host = 'demo.pubpub.org';
}
console.log('SETTING HOST', req.headers.host);
}
if (req.hostname.indexOf('duqduq.org') > -1) {
req.headers.host = req.hostname.replace('duqduq.org', 'pubpub.org');
}
console.log('HOST', req.headers.host);
next();
});

Expand All @@ -256,6 +253,32 @@ appRouter.use(readOnlyMiddleware());

const { customScript: _, ...contractWithoutCustomScript } = contract;

appRouter.use((req, res, next) => {
const now = process.hrtime.bigint();

let isLogged = false;
function logger() {
if (isLogged) {
return;
}
const durationMs = Number((process.hrtime.bigint() - now) / BigInt(1_000_000));
const host = req.headers.host ?? 'unknown';
const userAgent = req.headers['user-agent'] ?? 'unknown';
const contentLength = res.get('content-length') ?? '-';
const userId = req.user?.id ?? 'anon';
const ip = req.headers['x-forwarded-for'] ?? req.socket.remoteAddress ?? 'unknown';

console.log(
`${req.method} ${res.statusCode} ${req.path} ${durationMs}ms | host=${host} | user=${userId} | ip=${ip} | size=${contentLength} | ua=${userAgent} | origin=${req.headers.origin}`,
);
isLogged = true;
}

res.on('finish', logger);
res.on('close', logger);
res.on('error', logger);
next();
});
/* ------------------------- */
/* Create ts-rest api routes */
/* ------------------------- */
Expand Down Expand Up @@ -291,19 +314,9 @@ createExpressEndpoints(contractWithoutCustomScript, server, appRouter, {
import { apiRouter } from './apiRoutes';
import { rootRouter } from './routes';

appRouter.use((req, res, next) => {
console.log('==================');
console.log('==================');
const now = process.hrtime.bigint();
res.on('finish', () =>
console.log(
`${req.method} ${req.path} – ${Number((process.hrtime.bigint() - now) / BigInt(1_000_000))}ms`,
),
);
next();
});
appRouter.use(apiRouter);
appRouter.use(rootRouter);

/* ------------- */
/* Error Handlers */
/* ------------- */
Expand Down
9 changes: 9 additions & 0 deletions server/utils/queryHelpers/communityGet.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Collection, Community, Member, Page, ScopeSummary, SpamTag } from 'server/models';
import { isProd } from 'utils/environment';

export async function logg<T>(
out: Record<string, number>,
Expand All @@ -19,6 +20,10 @@ export function createLogger(initialId: string) {
const logger = logg.bind(this as any, acc);

const log = <T>(id: string, q: Promise<T>) => {
if (!process.env.DEBUG_LOG) {
return q;
}

return logger(`${initialId}:${id}`, q) as unknown as Promise<T>;
};

Expand All @@ -27,6 +32,10 @@ export function createLogger(initialId: string) {
return {
log,
end: () => {
if (!process.env.DEBUG_LOG) {
return;
}

const end = process.hrtime.bigint();

const total = Number(end - now) / 1_000_000;
Expand Down