feat: main 병합 + 인스타 올리기 API(/v1/card/{id}/instagram), 관리자 올릴 곳 선택(로컬·킹서버), 만든 카드 화면

This commit is contained in:
minaChoi committed 2026-10-23 15:48:09 +09:00
commit 572b2a05c8
188 files changed
+16605 -741

No files matched your search

+29 -13
View File
@@ -91,30 +91,45 @@ function imageApi(): Plugin {
}
}
/** 날씨각 백엔드 카드뉴스 API 를 관리자 비밀값(ADMIN_API_KEY)을 붙여 대신 부른다. 비밀값은 이 서버에서만 쓰고 브라우저에 나가지 않는다
* POST /api/post {…카드 묶음} → POST /v1/card (저장·게시) · GET /api/cards → GET /v1/card (만든 카드 목록) · POST /api/cards/<id>/publish|reject */
/** 올릴 곳. 로컬(이 Mac 백엔드)과 킹서버(운영). 비밀값은 이 서버에서만 쓰고 브라우저엔 안 나간다 */
type Target = { id: 'local' | 'king'; label: string; api: string; key?: string; site: string }
const TARGETS: Target[] = [
{ id: 'local', label: '로컬', api: process.env.VITE_API_BASE_URL ?? 'http://localhost:9810', key: process.env.ADMIN_API_KEY, site: process.env.LOCAL_SITE_URL ?? 'http://localhost:5174' },
{ id: 'king', label: '킹서버', api: process.env.KING_API_BASE_URL ?? 'https://weather.o2o.kr', key: process.env.KING_ADMIN_API_KEY, site: process.env.KING_SITE_URL ?? 'https://weather.o2o.kr' },
].filter((t) => !!t.key) as Target[]
function targetOf(req: IncomingMessage) {
const id = new URL(req.url ?? '/', 'http://x').searchParams.get('target') ?? TARGETS[0]?.id
const t = TARGETS.find((x) => x.id === id)
if (!t) throw new Error(id ? `'${id}' 에 올릴 비밀값이 없어요 (ADMIN_API_KEY / KING_ADMIN_API_KEY)` : 'ADMIN_API_KEY 가 없어요. 백엔드와 같은 값을 환경변수로 주고 관리자 서버를 다시 켜 주세요')
return t
}
/** 날씨각 백엔드 카드뉴스 API 를 대신 부른다 (?target=local|king)
* GET /api/targets · POST /api/post {…카드 묶음} → POST /v1/card · GET /api/cards → GET /v1/card · POST /api/cards/<id>/publish|reject|instagram */
function postApi(): Plugin {
return {
name: 'post-api',
configureServer(server) {
const api = process.env.VITE_API_BASE_URL ?? 'http://localhost:9810'
const headers = () => {
const key = process.env.ADMIN_API_KEY
if (!key) throw new Error('ADMIN_API_KEY 가 없어요. 백엔드와 같은 값을 환경변수로 주고 관리자 서버를 다시 켜 주세요')
return { 'Content-Type': 'application/json', 'X-Admin-Key': key }
}
const relay = async (res: ServerResponse, r: Response) => {
const text = await r.text()
res.statusCode = r.ok ? 200 : r.status
res.end(r.ok ? text : JSON.stringify({ error: `날씨각 API ${r.status}: ${text.slice(0, 200)}` }))
}
const headers = (t: Target) => ({ 'Content-Type': 'application/json', 'X-Admin-Key': t.key! })
server.middlewares.use('/api/targets', (_req, res) => {
res.setHeader('Content-Type', 'application/json; charset=utf-8')
res.end(JSON.stringify(TARGETS.map(({ id, label, site, api }) => ({ id, label, site, api }))))
})
server.middlewares.use('/api/cards', async (req, res) => {
res.setHeader('Content-Type', 'application/json; charset=utf-8')
try {
const m = (req.url ?? '').match(/^\/(\d+)\/(publish|reject)$/)
if (req.method === 'GET') return await relay(res, await fetch(`${api}/v1/card?limit=100`, { headers: headers() }))
if (req.method === 'POST' && m) return await relay(res, await fetch(`${api}/v1/card/${m[1]}/${m[2]}`, { method: 'POST', headers: headers() }))
throw new Error('GET /api/cards 또는 POST /api/cards/<id>/publish|reject')
const t = targetOf(req)
const path = (req.url ?? '').split('?')[0]
const m = path.match(/^\/(\d+)\/(publish|reject|instagram)$/)
if (req.method === 'GET') return await relay(res, await fetch(`${t.api}/v1/card?limit=100`, { headers: headers(t) }))
if (req.method === 'POST' && m) return await relay(res, await fetch(`${t.api}/v1/card/${m[1]}/${m[2]}`, { method: 'POST', headers: headers(t) }))
throw new Error('GET /api/cards 또는 POST /api/cards/<id>/publish|reject|instagram')
} catch (e) {
server.config.logger.error(`[cards-api] ${e instanceof Error ? e.message : String(e)}`)
res.statusCode = 500
@@ -125,9 +140,10 @@ function postApi(): Plugin {
res.setHeader('Content-Type', 'application/json; charset=utf-8')
try {
if (req.method !== 'POST') throw new Error('POST 로 보내 주세요')
const t = targetOf(req)
const body = await readJson(req)
if (!body.title) Object.assign(body, await (await server.ssrLoadModule('/src/cards/context.ts')).postText(body.template, body.texts))
await relay(res, await fetch(`${api}/v1/card`, { method: 'POST', headers: headers(), body: JSON.stringify(body) }))
await relay(res, await fetch(`${t.api}/v1/card`, { method: 'POST', headers: headers(t), body: JSON.stringify(body) }))
} catch (e) {
server.config.logger.error(`[post-api] ${e instanceof Error ? e.message : String(e)}`)
res.statusCode = 500