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
6 changes: 4 additions & 2 deletions src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,12 @@ const JSON_BODY_LIMIT = 1 * 1024 * 1024; // 1 MB
export function createApp(ctx: StackContext, config: Config, logger: Logger): Hono<AppEnv> {
const app = new Hono<AppEnv>();

// Assign a unique request ID to every request for log correlation.
// Assign a unique request ID to every request and expose it on the response.
app.use(async (c, next) => {
c.set('requestId', crypto.randomUUID());
const id = crypto.randomUUID();
c.set('requestId', id);
await next();
c.header('X-Request-Id', id);
});

app.use(
Expand Down
19 changes: 17 additions & 2 deletions src/config.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { existsSync } from 'node:fs';

const DEFAULT_MAX_ATTACHMENT_BYTES = 50 * 1024 * 1024; // 50 MB

function required(name: string): string {
const val = process.env[name];
if (!val) throw new Error(`Missing required environment variable: ${name}`);
Expand Down Expand Up @@ -35,15 +37,28 @@ export function loadConfig(): Config {
);
}

const port = parseInt(optional('PORT', '3000'), 10);
if (isNaN(port) || port < 1 || port > 65535) {
throw new Error(`Invalid PORT: ${process.env['PORT']}`);
}

const maxAttachmentBytes = parseInt(
optional('MAX_ATTACHMENT_BYTES', String(DEFAULT_MAX_ATTACHMENT_BYTES)),
10,
);
if (isNaN(maxAttachmentBytes) || maxAttachmentBytes < 1) {
throw new Error(`Invalid MAX_ATTACHMENT_BYTES: ${process.env['MAX_ATTACHMENT_BYTES']}`);
}

return {
port: parseInt(optional('PORT', '3000'), 10),
port,
dbPath,
entityId,
timezone,
ownerToken: required('OWNER_TOKEN'),
corsOrigins: optional('CORS_ORIGINS', ''),
baseUrl: process.env['BASE_URL'] ?? null,
isNewDb,
maxAttachmentBytes: parseInt(optional('MAX_ATTACHMENT_BYTES', String(50 * 1024 * 1024)), 10),
maxAttachmentBytes,
};
}
Loading