Merge branch 'feature/20250930_cl_GR_GALLERY' into feature/20250910-all
This commit is contained in:
82
layers/server/middleware/gameData.ts
Normal file
82
layers/server/middleware/gameData.ts
Normal file
@@ -0,0 +1,82 @@
|
||||
import {
|
||||
getHeader,
|
||||
getRequestHost,
|
||||
defineEventHandler,
|
||||
getRequestURL,
|
||||
} from 'h3'
|
||||
|
||||
import { ssrGetFinalLocale } from '../../utils/localeUtil'
|
||||
|
||||
export default defineEventHandler(async event => {
|
||||
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 stoveApiBaseUrl = config.public.stoveApiUrl
|
||||
const apiUrl = `${stoveApiBaseUrl}/pub-comm/v1.0/template/game`
|
||||
|
||||
const langCode = ssrGetFinalLocale(
|
||||
event?.node.req.url,
|
||||
event.node.req.headers
|
||||
)
|
||||
|
||||
// URL의 첫 번째 path를 lang_code로 사용 (파비콘, API 경로 제외)
|
||||
// const pathSegments = url.pathname
|
||||
// .split('/')
|
||||
// .filter(
|
||||
// segment =>
|
||||
// segment &&
|
||||
// !segment.includes('favicon') &&
|
||||
// !segment.includes('api') &&
|
||||
// !segment.startsWith('_')
|
||||
// )
|
||||
// const langCode = pathSegments[0] || 'ko'
|
||||
|
||||
const queryParams: Record<string, string> = {
|
||||
game_domain: event.context.gameDomain || '',
|
||||
lang_code: langCode,
|
||||
}
|
||||
|
||||
const response = await $fetch(apiUrl, {
|
||||
query: queryParams,
|
||||
})
|
||||
|
||||
const gaId = (response as any).value?.ga_code
|
||||
|
||||
if (gaId) {
|
||||
// 환경변수에 동적 설정
|
||||
event.context.googleAnalyticsId = gaId
|
||||
}
|
||||
|
||||
// 타입 단언을 사용하여 response의 타입 오류를 해결
|
||||
const res = response as { code?: number; value?: unknown }
|
||||
|
||||
if (res?.code === 0 && res && typeof res === 'object' && 'value' in res) {
|
||||
event.context.gameData = res.value
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('gameData load error:', error)
|
||||
}
|
||||
})
|
||||
Reference in New Issue
Block a user