import express from 'express' import cors from 'cors' import path from 'path' import { fileURLToPath } from 'url' import fs from 'fs' import sitesRouter from './routes/sites.js' import authRouter from './routes/auth.js' import proxyRouter from './routes/proxy.js' import billingRouter from './routes/billing.js' import { ensureChineseFont } from './utils/font.js' const app = express() const PORT = Number(process.env.PORT) || 3001 const __dirname = path.dirname(fileURLToPath(import.meta.url)) app.use(cors()) app.use(express.json()) // API routes app.get('/api/health', (_req, res) => { res.json({ success: true, message: 'NewAPI Dashboard BFF running' }) }) app.use('/api/sites', sitesRouter) app.use('/api/auth', authRouter) app.use('/proxy', proxyRouter) app.use('/api/billing', billingRouter) // Production: serve frontend static files from dist/ const distPath = path.join(__dirname, '..', 'dist') if (fs.existsSync(path.join(distPath, 'index.html'))) { app.use(express.static(distPath)) // SPA fallback: any non-API GET request returns index.html app.get('*', (_req, res) => { res.sendFile(path.join(distPath, 'index.html')) }) } // Download Chinese font then start server ensureChineseFont() .then(() => { app.listen(PORT, '0.0.0.0', () => { console.log(`BFF server running on http://0.0.0.0:${PORT}`) }) }) .catch((err) => { console.error('Font initialization failed:', err.message) app.listen(PORT, '0.0.0.0', () => { console.log(`BFF server running on http://0.0.0.0:${PORT} (font unavailable)`) }) })