feat: 完善项目架构和功能

- 修复路由配置,实现根路径自动重定向到/project
- 新增Gitea OAuth认证系统和相关组件
- 完善日志系统实现,包含pino日志工具和中间件
- 重构页面结构,分离项目管理和环境管理页面
- 新增CORS、Session等关键中间件
- 优化前端请求封装和类型定义
- 修复TypeScript类型错误和参数传递问题
This commit is contained in:
2025-09-04 23:19:52 +08:00
parent d178df54da
commit ef473d6084
34 changed files with 1022 additions and 196 deletions

View File

@@ -0,0 +1,17 @@
import cors from '@koa/cors';
import type Koa from 'koa';
import type { Middleware } from './types.ts';
export class CORS implements Middleware {
apply(app: Koa) {
app.use(
cors({
credentials: true,
allowHeaders: ['Content-Type'],
origin(ctx) {
return ctx.get('Origin') || '*';
},
}),
);
}
}

View File

@@ -1,14 +1,15 @@
import type Koa from 'koa';
import type { Middleware } from './types.ts';
import { log } from '../libs/logger.ts';
/**
* 统一响应体结构
*/
export interface ApiResponse<T = any> {
code: number; // 状态码0表示成功其他表示失败
message: string; // 响应消息
data?: T; // 响应数据
timestamp: number; // 时间戳
code: number; // 状态码0表示成功其他表示失败
message: string; // 响应消息
data?: T; // 响应数据
timestamp: number; // 时间戳
}
/**
@@ -36,11 +37,11 @@ export class Exception implements Middleware {
await next();
// 如果没有设置响应体则返回404
if (ctx.status === 404 && !ctx.body) {
this.sendResponse(ctx, 404, '接口不存在', null, 404);
if (ctx.status === 404) {
this.sendResponse(ctx, 404, 'Not Found', null, 404);
}
} catch (error) {
console.error('全局异常捕获:', error);
log.error('Exception', 'catch error: %o', error);
this.handleError(ctx, error);
}
});
@@ -55,11 +56,16 @@ export class Exception implements Middleware {
this.sendResponse(ctx, error.code, error.message, null, error.httpStatus);
} else if (error.status) {
// Koa HTTP 错误
const message = error.status === 401 ? '未授权访问' :
error.status === 403 ? '禁止访问' :
error.status === 404 ? '资源不存在' :
error.status === 422 ? '请求参数错误' :
error.message || '请求失败';
const message =
error.status === 401
? '未授权访问'
: error.status === 403
? '禁止访问'
: error.status === 404
? '资源不存在'
: error.status === 422
? '请求参数错误'
: error.message || '请求失败';
this.sendResponse(ctx, error.status, message, null, error.status);
} else {
@@ -80,13 +86,13 @@ export class Exception implements Middleware {
code: number,
message: string,
data: any = null,
httpStatus = 200
httpStatus = 200,
): void {
const response: ApiResponse = {
code,
message,
data,
timestamp: Date.now()
timestamp: Date.now(),
};
ctx.status = httpStatus;
@@ -98,12 +104,15 @@ export class Exception implements Middleware {
/**
* 创建成功响应的辅助函数
*/
export function createSuccessResponse<T>(data: T, message = '操作成功'): ApiResponse<T> {
export function createSuccessResponse<T>(
data: T,
message = '操作成功',
): ApiResponse<T> {
return {
code: 0,
message,
data,
timestamp: Date.now()
timestamp: Date.now(),
};
}
@@ -128,11 +137,15 @@ export function createAutoSuccessResponse<T>(data: T): ApiResponse<T> {
/**
* 创建失败响应的辅助函数
*/
export function createErrorResponse(code: number, message: string, data?: any): ApiResponse {
export function createErrorResponse(
code: number,
message: string,
data?: any,
): ApiResponse {
return {
code,
message,
data,
timestamp: Date.now()
timestamp: Date.now(),
};
}

View File

@@ -1,7 +1,9 @@
import { Router } from './router.ts';
import { ResponseTime } from './responseTime.ts';
import { Exception } from './exception.ts';
import { BodyParser } from './body-parser.ts';
import { Session } from './session.ts';
import { CORS } from './cors.ts';
import { HttpLogger } from './logger.ts';
import type Koa from 'koa';
/**
@@ -9,17 +11,19 @@ import type Koa from 'koa';
* @param app Koa
*/
export function initMiddlewares(app: Koa) {
// 日志中间件需要最早注册,记录所有请求
new HttpLogger().apply(app);
// 全局异常处理中间件必须最先注册
const exception = new Exception();
exception.apply(app);
new Exception().apply(app);
// Session 中间件需要在请求体解析之前注册
new Session().apply(app);
// 请求体解析中间件
const bodyParser = new BodyParser();
bodyParser.apply(app);
new BodyParser().apply(app);
const responseTime = new ResponseTime();
responseTime.apply(app);
new CORS().apply(app);
const router = new Router();
router.apply(app);
new Router().apply(app);
}

View File

@@ -0,0 +1,14 @@
import Koa, { type Context } from 'koa';
import { log } from '../libs/logger.ts';
import type { Middleware } from './types.ts';
export class HttpLogger implements Middleware {
apply(app: Koa): void {
app.use(async (ctx: Context, next: Koa.Next) => {
const start = Date.now();
await next();
const ms = Date.now() - start;
log.info('HTTP', `${ctx.method} ${ctx.url} - ${ms}ms`)
});
}
}

View File

@@ -1,13 +0,0 @@
import type { Middleware } from './types.ts';
import type Koa from 'koa';
export class ResponseTime implements Middleware {
apply(app: Koa): void {
app.use(async (ctx, next) => {
const start = Date.now();
await next();
const ms = Date.now() - start;
ctx.set('X-Response-Time', `${ms}ms`);
});
}
}

View File

@@ -1,36 +1,16 @@
import KoaRouter from '@koa/router';
import type Koa from 'koa';
import type { Middleware } from './types.ts';
import { createAutoSuccessResponse } from './exception.ts';
import { RouteScanner } from '../libs/route-scanner.ts';
import { ApplicationController } from '../controllers/application.ts';
import { UserController } from '../controllers/user.ts';
import * as application from '../controllers/application.ts';
/**
* 包装控制器函数,统一处理响应格式
*/
function wrapController(controllerFn: Function) {
return async (ctx: Koa.Context, next: Koa.Next) => {
try {
// 调用控制器函数获取返回数据
const result = await controllerFn(ctx, next);
// 如果控制器返回了数据,则包装成统一响应格式
if (result !== undefined) {
ctx.body = createAutoSuccessResponse(result);
}
// 如果控制器没有返回数据,说明已经自己处理了响应
} catch (error) {
// 错误由全局异常处理中间件处理
throw error;
}
};
}
import { AuthController } from '../controllers/auth.ts';
import { log } from '../libs/logger.ts';
export class Router implements Middleware {
private router: KoaRouter;
private routeScanner: RouteScanner;
private readonly TAG = 'Router';
constructor() {
this.router = new KoaRouter({
@@ -54,14 +34,18 @@ export class Router implements Middleware {
// 注册所有使用装饰器的控制器
this.routeScanner.registerControllers([
ApplicationController,
UserController
UserController,
AuthController,
]);
// 输出注册的路由信息
const routes = this.routeScanner.getRegisteredRoutes();
console.log('装饰器路由注册完成:');
routes.forEach(route => {
console.log(` ${route.method} ${route.path} -> ${route.controller}.${route.action}`);
log.debug(this.TAG, '装饰器路由注册完成:');
routes.forEach((route) => {
log.debug(
this.TAG,
` ${route.method} ${route.path} -> ${route.controller}.${route.action}`,
);
});
}

View File

@@ -0,0 +1,24 @@
import session from 'koa-session';
import type Koa from 'koa';
import type { Middleware } from './types.ts';
export class Session implements Middleware {
apply(app: Koa): void {
app.keys = ['foka-ci'];
app.use(
session(
{
key: 'foka.sid',
maxAge: 86400000,
autoCommit: true /** (boolean) automatically commit headers (default true) */,
overwrite: true /** (boolean) can overwrite or not (default true) */,
httpOnly: true /** (boolean) httpOnly or not (default true) */,
signed: true /** (boolean) signed or not (default true) */,
rolling: false /** (boolean) Force a session identifier cookie to be set on every response. The expiration is reset to the original maxAge, resetting the expiration countdown. (default is false) */,
renew: false /** (boolean) renew session when session is nearly expired, so we can always keep user logged in. (default is false)*/,
},
app,
),
);
}
}