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

49 lines
2.0 KiB
TypeScript

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';
import {
API_PROXY_PATH,
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({
apiBaseUrl: API_PROXY_PATH,
baleHandle: clientEnv.NEXT_PUBLIC_BALE_HANDLE,
telegramHandle: clientEnv.NEXT_PUBLIC_TELEGRAM_HANDLE,
supportHours: clientEnv.NEXT_PUBLIC_SUPPORT_HOURS,
});
// Server requests 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({
baseUrl: `${serverEnv.API_INTERNAL_ORIGIN}${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);
}
}