You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 

44 lines
1.2 KiB

// 日志、全局插件、定时任务等基础设施
import "./logger.js"
import "./jobs/index.js"
// 第三方依赖
import Koa from "koa"
import os from "os"
import log4js from "log4js"
// 应用插件与自动路由
import LoadMiddlewares from "./middlewares/install.js"
import { autoRegisterControllers } from "utils/autoRegister.js"
import bodyParser from "koa-bodyparser"
const logger = log4js.getLogger()
const app = new Koa()
app.use(bodyParser());
// 注册插件
LoadMiddlewares(app)
// 自动注册所有 controller
autoRegisterControllers(app)
const PORT = process.env.PORT || 3000
const server = app.listen(PORT, () => {
const port = server.address().port
// 获取本地 IP
const getLocalIP = () => {
const interfaces = os.networkInterfaces()
for (const name of Object.keys(interfaces)) {
for (const iface of interfaces[name]) {
if (iface.family === "IPv4" && !iface.internal) {
return iface.address
}
}
}
return "localhost"
}
const localIP = getLocalIP()
logger.trace(`服务器运行在: http://${localIP}:${port}`)
})
export default app