wealth/apps/bear/src/bootstrap/bootstrap.server.ts

46 lines
2.0 KiB
TypeScript
Raw Permalink Normal View History

import 'server-only';
import { configureErrorHandler } from '@lumina/libs/error-handler';
import { http } from '@lumina/libs/http';
import { configureServerHttp } from '@lumina/libs/server';
import { mapApiErrorBody } from '@lumina/market/app/http';
import { setEnv as setMarketEnv } from '@lumina/market/env';
import { SERVER_RESOURCES } from '@lumina/market/server';
import { cookies } from 'next/headers';
2026-10-17 09:58:30 +00:00
import { API_VERSION_PATH, FORWARDED_COOKIES } from '../constants';
import { clientEnv } from '../env';
import { serverEnv } from '../env/server';
import { i18n } from '../i18n';
import { ERROR_HANDLER_CONFIG } from './error-reporting';
// Fills the server module graph once, before rendering. Idempotent: safe to run again.
setMarketEnv({
2026-10-17 09:58:30 +00:00
apiBaseUrl: clientEnv.NEXT_PUBLIC_CLIENT_END_POINT,
baleHandle: clientEnv.NEXT_PUBLIC_BALE_HANDLE,
telegramHandle: clientEnv.NEXT_PUBLIC_TELEGRAM_HANDLE,
supportHours: clientEnv.NEXT_PUBLIC_SUPPORT_HOURS,
});
2026-10-17 09:58:30 +00:00
// Server requests go straight to the gateway (`INTERNAL_END_POINT`, 0066) and forward only the
// allowlisted cookies, never the session: the server render is the same for every visitor, and the
// browser loads whatever depends on the viewer. Read on each request; nothing per visitor is kept
// in module state.
configureServerHttp({
2026-10-17 09:58:30 +00:00
baseUrl: `${serverEnv.INTERNAL_END_POINT}${API_VERSION_PATH}`,
getCookieHeader: async () =>
(await cookies())
.getAll()
.filter(({ name }) => FORWARDED_COOKIES.has(name))
.map(({ name, value }) => `${name}=${encodeURIComponent(value)}`)
.join('; '),
});
http.configure({ mapErrorBody: mapApiErrorBody });
configureErrorHandler(ERROR_HANDLER_CONFIG);
// Namespaces only server components read (the landing) join the server's i18n instance here, so
// no client bundle carries them. Existing keys are kept, so running this again changes nothing.
for (const [language, bundles] of Object.entries(SERVER_RESOURCES)) {
for (const [namespace, bundle] of Object.entries(bundles)) {
i18n.addResourceBundle(language, namespace, bundle);
}
}