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

40 lines
1.5 KiB
TypeScript
Raw 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 { 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 { 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);