84 lines
2.6 KiB
JavaScript
84 lines
2.6 KiB
JavaScript
import Koa from 'koa'
|
||
import Router from 'koa-router'
|
||
import bodyParser from 'koa-bodyparser'
|
||
import koaStatic from 'koa-static'
|
||
import config from '../config/index.js'
|
||
import * as log4js from '../log4js.js'
|
||
import auth from './auth.js'
|
||
import login from './login.js'
|
||
import registry from './registry.js'
|
||
import linuxdo from './linuxdo.js'
|
||
import errorHandler from './middleware/errorHandler.js'
|
||
import ipFilter from './middleware/ipFilter.js'
|
||
import rateLimiter from './middleware/rateLimiter.js'
|
||
|
||
const app = new Koa()
|
||
const router = new Router()
|
||
|
||
// ─── 基础路由 ────────────────────────────────────────────────────────────────
|
||
router.get('/', (ctx) => {
|
||
ctx.body = { message: 'Chuanqi Server Running!' }
|
||
})
|
||
|
||
router.get('/api/config', (ctx) => {
|
||
ctx.body = {
|
||
data: {
|
||
gameName: config.game.name,
|
||
gameDescription: config.game.description,
|
||
codeOpen: config.code.open,
|
||
regCodeOpen: config.code.regCodeOpen,
|
||
regOpen: config.account.regOpen,
|
||
loginOpen: config.account.loginOpen,
|
||
linuxdoAuthorizeUrl: `/api/linuxdo/authorize`,
|
||
// 提现相关
|
||
withdrawRatio: config.withdraw.ratio,
|
||
withdrawMinOnce: config.withdraw.minOnce,
|
||
currencyName: config.currency.list[config.withdraw.type] || '货币',
|
||
}
|
||
}
|
||
})
|
||
|
||
// ─── 中间件 ──────────────────────────────────────────────────────────────────
|
||
app.proxy = true
|
||
|
||
// 1. 统一错误处理(最外层,捕获所有异常)
|
||
app.use(errorHandler)
|
||
|
||
// 2. 请求日志
|
||
app.use(async (ctx, next) => {
|
||
log4js.koa.debug(`${ctx.method} ${ctx.path}`)
|
||
await next()
|
||
})
|
||
|
||
// 3. IP 黑名单过滤
|
||
app.use(ipFilter)
|
||
|
||
// 4. 请求限流(防暴力破解)
|
||
app.use(rateLimiter)
|
||
|
||
// 5. body 解析
|
||
app.use(bodyParser({
|
||
enableTypes: ['json', 'form'],
|
||
formLimit: '10mb',
|
||
jsonLimit: '10mb',
|
||
}))
|
||
|
||
// 6. JWT 鉴权
|
||
app.use(auth)
|
||
|
||
// 路由挂载
|
||
app.use(router.routes())
|
||
app.use(router.allowedMethods())
|
||
app.use(login)
|
||
app.use(registry)
|
||
app.use(linuxdo)
|
||
|
||
// 静态文件(部署时前端 dist 挂到 /www)
|
||
app.use(koaStatic('/www'))
|
||
|
||
// ─── 启动 ────────────────────────────────────────────────────────────────────
|
||
const PORT = process.env.PORT || 3001
|
||
app.listen(PORT, () => {
|
||
log4js.koa.info(`🚀 Koa server running on port ${PORT}`)
|
||
})
|