2025-02-24 17:46:27 +08:00
|
|
|
|
require("dotenv").config();
|
|
|
|
|
|
const Koa = require("koa");
|
|
|
|
|
|
const Router = require("koa-router");
|
|
|
|
|
|
const jwt = require("jsonwebtoken");
|
|
|
|
|
|
const koaJwt = require("koa-jwt");
|
|
|
|
|
|
const fs = require("fs");
|
|
|
|
|
|
const path = require("path");
|
|
|
|
|
|
const bodyParser = require("koa-bodyparser");
|
2025-02-24 17:41:40 +08:00
|
|
|
|
|
|
|
|
|
|
const app = new Koa();
|
2025-02-24 17:46:27 +08:00
|
|
|
|
app.use(bodyParser()); // 添加在路由中间件之前
|
2025-02-24 17:41:40 +08:00
|
|
|
|
const router = new Router();
|
|
|
|
|
|
|
2025-02-24 17:46:27 +08:00
|
|
|
|
// 自动加载API路由函数
|
|
|
|
|
|
const loadAPIRoutes = () => {
|
|
|
|
|
|
const apiDir = path.join(__dirname, "api");
|
|
|
|
|
|
const files = fs.readdirSync(apiDir);
|
|
|
|
|
|
|
|
|
|
|
|
files.forEach((file) => {
|
|
|
|
|
|
if (file.endsWith(".js") && file !== "index.js") {
|
|
|
|
|
|
const routePath = `/${file.replace(".js", "")}`;
|
|
|
|
|
|
const handler = require(path.join(apiDir, file));
|
|
|
|
|
|
|
|
|
|
|
|
router.post(routePath, async (ctx) => {
|
|
|
|
|
|
await handler(ctx);
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2025-02-24 17:41:40 +08:00
|
|
|
|
// 公开路由
|
2025-02-24 17:46:27 +08:00
|
|
|
|
router.get("/public", (ctx) => {
|
|
|
|
|
|
ctx.body = "Public content";
|
2025-02-24 17:41:40 +08:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
// 登录路由
|
2025-02-24 17:46:27 +08:00
|
|
|
|
router.post("/login", (ctx) => {
|
|
|
|
|
|
const user = { id: 1, username: "admin" };
|
|
|
|
|
|
const token = jwt.sign(user, process.env.JWT_SECRET, { expiresIn: "1h" });
|
2025-02-24 17:41:40 +08:00
|
|
|
|
ctx.body = { token };
|
|
|
|
|
|
});
|
|
|
|
|
|
|
2025-02-24 17:46:27 +08:00
|
|
|
|
// JWT中间件(保护下方所有路由)
|
|
|
|
|
|
app.use(
|
|
|
|
|
|
koaJwt({
|
|
|
|
|
|
secret: process.env.JWT_SECRET,
|
|
|
|
|
|
}).unless({
|
|
|
|
|
|
path: [/^\/public/, /^\/login/],
|
|
|
|
|
|
})
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
// 加载自动生成的路由
|
|
|
|
|
|
loadAPIRoutes();
|
2025-02-24 17:41:40 +08:00
|
|
|
|
|
|
|
|
|
|
// 受保护路由
|
2025-02-24 17:46:27 +08:00
|
|
|
|
router.get("/protected", (ctx) => {
|
2025-02-24 17:41:40 +08:00
|
|
|
|
ctx.body = `Protected content for ${ctx.state.user.username}`;
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
app.use(router.routes());
|
|
|
|
|
|
app.use(router.allowedMethods());
|
|
|
|
|
|
|
|
|
|
|
|
app.listen(process.env.PORT || 3000, () => {
|
|
|
|
|
|
console.log(`Server running on http://localhost:${process.env.PORT || 3000}`);
|
2025-02-24 17:46:27 +08:00
|
|
|
|
});
|