1
1
mirror of https://github.com/ZeroCatDev/ClassworksKV.git synced 2025-12-07 13:03:09 +00:00
ClassworksKV/app.js
SunWuyuan 2ab90ffebc
feat: Implement Refresh Token system with enhanced security and user experience
- Added refresh token support in the account model with new fields: refreshToken, refreshTokenExpiry, and tokenVersion.
- Created a new token management utility (utils/tokenManager.js) for generating and verifying access and refresh tokens.
- Updated JWT utility (utils/jwt.js) to maintain backward compatibility while introducing new token generation methods.
- Enhanced middleware for JWT authentication to support new token types and automatic token refreshing.
- Expanded API endpoints in routes/accounts.js to include refresh token functionality, logout options, and token info retrieval.
- Introduced automatic token refresh mechanism in the front-end integration examples.
- Comprehensive migration checklist and documentation for the new refresh token system.
- Added database migration script to accommodate new fields in the Account table.
2025-11-02 09:48:03 +08:00

124 lines
3.4 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import "./utils/instrumentation.js";
// import createError from "http-errors";
import express from "express";
import { join, dirname } from "path";
import { fileURLToPath } from "url";
// import cookieParser from "cookie-parser";
import logger from "morgan";
import bodyParser from "body-parser";
import errorHandler from "./middleware/errorHandler.js";
import errors from "./utils/errors.js";
import kvRouter from "./routes/kv-token.js";
import appsRouter from "./routes/apps.js";
import deviceRouter from "./routes/device.js";
import deviceAuthRouter from "./routes/device-auth.js";
import accountsRouter from "./routes/accounts.js";
import autoAuthRouter from "./routes/auto-auth.js";
var app = express();
import cors from "cors";
app.options("/{*path}", cors());
app.use(
cors({
exposedHeaders: ["ratelimit-policy", "retry-after", "ratelimit"], // 告诉浏览器这些响应头可以暴露
maxAge: 86400, // 设置OPTIONS请求的结果缓存24小时(86400秒),减少预检请求
})
);
app.disable("x-powered-by");
// 获取当前文件的目录路径
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
// view engine setup
app.set("views", join(__dirname, "views"));
app.set("view engine", "ejs");
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
app.use(logger("dev"));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
// app.use(cookieParser());
app.use(express.static(join(__dirname, "public")));
// 添加请求超时处理中间件
app.use((req, res, next) => {
// 设置默认请求超时时间为30秒
const timeout = 30000;
// 设置超时回调
const timeoutCallback = () => {
const timeoutError = errors.createError(408, "请求处理超时");
next(timeoutError);
};
// 设置超时
req.setTimeout(timeout, timeoutCallback);
// 监听响应完成事件
res.on("finish", () => {
// 如果响应已经完成,清除超时处理
req.setTimeout(0, timeoutCallback);
});
next();
});
app.get("/", (req, res) => {
res.render("index.ejs");
});
app.get("/check", (req, res) => {
res.json({
status: "success",
message: "Classworks KV is running",
time: new Date().getTime(),
});
});
// Mount the Apps router with API rate limiting
app.use("/apps", appsRouter);
// Mount the Auto Auth router with API rate limiting
app.use("/auto-auth", autoAuthRouter);
// Mount the Device router with API rate limiting
app.use("/devices", deviceRouter);
// Mount the KV store router
app.use("/kv", kvRouter);
// Mount the Device Authorization router with API rate limiting
app.use("/auth", deviceAuthRouter);
// Mount the Accounts router with API rate limiting
app.use("/accounts", accountsRouter);
// 兜底404路由 - 处理所有未匹配的路由
app.use((req, res, next) => {
const notFoundError = errors.createError(404, `找不到路径: ${req.path}`);
next(notFoundError);
});
// 全局错误处理中间件
app.use(errorHandler);
// 全局未捕获的异常处理
process.on("uncaughtException", (error) => {
console.error("未捕获的异常:", error);
// 记录错误但不退出进程
});
// 全局未处理的Promise拒绝处理
process.on("unhandledRejection", (reason, promise) => {
console.error("未处理的Promise拒绝", reason);
// 记录错误但不退出进程
});
// 处理 SIGTERM 信号
process.on("SIGTERM", () => {
console.log("收到 SIGTERM 信号,准备关闭服务...");
});
export default app;