import { getHeader, getRequestHost, defineEventHandler, getRequestURL, } from 'h3' import { ssrGetFinalLocale } from '../../utils/localeUtil' import type { GameDataResponse } from '../../types/api/gameData' import type { ResGetInspectionData } from '../../types/InspectionType' export default defineEventHandler(async event => { const config = useRuntimeConfig() const iBaseApiUrl = `${config.public.stoveApiUrlServer}` const url = getRequestURL(event) // 정적 자산, API, 파비콘 등은 제외하고 페이지 요청만 처리 if ( url.pathname.startsWith('/api/') || url.pathname.startsWith('/_nuxt/') || url.pathname.startsWith('/favicon') || url.pathname.includes('.') || url.pathname.startsWith('/_') ) { return } const host = (getHeader(event, 'host') || getRequestHost(event)).toString() || '' const baseDomain = process.env.BASE_DOMAIN || '.onstove.com' const isGameDomainExtractable = host.includes(baseDomain) if (isGameDomainExtractable) { const cleanHost = host.split(':')[0] event.context.gameDomain = cleanHost } // gameData를 직접 가져와서 context에 저장 (API 호출 없이) try { const config = useRuntimeConfig() const stoveApiUrlServer = config.public.stoveApiUrlServer const apiUrl = `${stoveApiUrlServer}/pub-comm/v1.0/template/game` let inspectionData const langCode = ssrGetFinalLocale( event?.node.req.url, event.node.req.headers ) const queryParams: Record = { game_domain: event.context.gameDomain || '', lang_code: langCode, } const response = (await $fetch(apiUrl, { query: queryParams, })) as GameDataResponse | null if (response?.code === 0 && 'value' in response) { event.context.gameData = response.value event.context.googleAnalyticsId = response.value?.ga_code console.log('🚀 ~ gameData:', response.value) // 점검 데이터 조회 if (response.value.game_id) { const inspectionApiUrl = `${iBaseApiUrl}/pub-comm/v3.0/inspection/${response.value.game_id}` // 직접 $fetch 사용 (composable 사용하지 않음) const inspectionResponse = await $fetch(inspectionApiUrl, { method: 'GET', headers: { 'Content-Type': 'application/json' } }) inspectionData = inspectionResponse?.value?.inspection // console.log("🚀 ~ inspectionData:", inspectionData) if (inspectionData?.inspection_status === 0) { /** * 점검 중인 경우 * - 점검 상태가 1이고 현재 시간이 점검 시작과 종료 사이에 있는지 확인ㄹ * - 점검 URL 경로가 아닐 경우 no-cache 설정 * - 화이트 리스트 체크 */ // 현재 경로가 점검 페이지가 아닐 경우 리다이렉트 const inspectionPath = `/${langCode}/inspection` if (!url.pathname.includes('/inspection')) { event.node.res.statusCode = 302 event.node.res.setHeader('Location', inspectionPath) event.node.res.end() return } } } } } catch (error) { console.error('gameData load error:', error) } })