import { httpInstrumentationMiddleware } from "@hono/otel";
import { OpenAPIHono } from "@hono/zod-openapi";
import { contextStorage } from "hono/context-storage";
import { cors } from "hono/cors";
import { csrf } from "hono/csrf";
import { languageDetector } from "hono/language";
import { logger as loggerMiddleware } from "hono/logger";
import { prettyJSON } from "hono/pretty-json";
import { requestId } from "hono/request-id";
import { secureHeaders } from "hono/secure-headers";
import { timeout } from "hono/timeout";
import { timing } from "hono/timing";
const TIMEOUT = 15_000; // 15 seconds
const app = new OpenAPIHono<{ Variables: Variables }>();
app.use(
"*",
// OpenTelemetry instrumentation
httpInstrumentationMiddleware({
serviceName: SERVICE_NAME,
serviceVersion: SERVICE_VERSION,
}),
// Context storage using AsyncLocalStorage
contextStorage(),
// HTTP logger
loggerMiddleware(),
// CORS
cors({
origin: [ENV.APP_URL],
allowMethods: ["GET", "POST", "OPTIONS"],
allowHeaders: ["Content-Type", "Authorization"],
exposeHeaders: ["Content-Length"],
credentials: true,
}),
// Request ID
requestId(),
// Auth context
authContextMiddleware(),
// Server timing
timing(),
// Timeout protection
timeout(TIMEOUT),
// Language detection
languageDetector({
supportedLanguages: ["en", "id"],
fallbackLanguage: "en",
}),
// CSRF protection
csrf({
origin: [ENV.APP_URL],
}),
// Security headers
secureHeaders(),
// Pretty JSON responses
prettyJSON()
);