feat: 完善项目架构和功能
- 修复路由配置,实现根路径自动重定向到/project - 新增Gitea OAuth认证系统和相关组件 - 完善日志系统实现,包含pino日志工具和中间件 - 重构页面结构,分离项目管理和环境管理页面 - 新增CORS、Session等关键中间件 - 优化前端请求封装和类型定义 - 修复TypeScript类型错误和参数传递问题
This commit is contained in:
@@ -1,10 +1,13 @@
|
||||
import Koa from 'koa';
|
||||
import { initMiddlewares } from './middlewares/index.ts';
|
||||
import { log } from './libs/logger.ts';
|
||||
|
||||
const app = new Koa();
|
||||
|
||||
initMiddlewares(app);
|
||||
|
||||
app.listen(3000, () => {
|
||||
console.log('server started at http://localhost:3000');
|
||||
const PORT = process.env.PORT || 3001;
|
||||
|
||||
app.listen(PORT, () => {
|
||||
log.info('APP', 'Server started at port %d', PORT);
|
||||
});
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import type { Context } from 'koa';
|
||||
import prisma from '../libs/db.ts';
|
||||
import { log } from '../libs/logger.ts';
|
||||
import { BusinessError } from '../middlewares/exception.ts';
|
||||
import { Controller, Get } from '../decorators/route.ts';
|
||||
|
||||
@@ -7,6 +8,7 @@ import { Controller, Get } from '../decorators/route.ts';
|
||||
export class ApplicationController {
|
||||
@Get('/list')
|
||||
async list(ctx: Context) {
|
||||
log.debug('app', 'session %o', ctx.session);
|
||||
try {
|
||||
const list = await prisma.application.findMany({
|
||||
where: {
|
||||
@@ -24,27 +26,15 @@ export class ApplicationController {
|
||||
|
||||
@Get('/detail/:id')
|
||||
async detail(ctx: Context) {
|
||||
try {
|
||||
const { id } = ctx.params;
|
||||
const app = await prisma.application.findUnique({
|
||||
where: { id: Number(id) },
|
||||
});
|
||||
const { id } = ctx.params;
|
||||
const app = await prisma.application.findUnique({
|
||||
where: { id: Number(id) },
|
||||
});
|
||||
|
||||
if (!app) {
|
||||
throw new BusinessError('应用不存在', 1002, 404);
|
||||
}
|
||||
|
||||
return app;
|
||||
} catch (error) {
|
||||
if (error instanceof BusinessError) {
|
||||
throw error;
|
||||
}
|
||||
throw new BusinessError('获取应用详情失败', 1003, 500);
|
||||
if (!app) {
|
||||
throw new BusinessError('应用不存在', 1002, 404);
|
||||
}
|
||||
|
||||
return app;
|
||||
}
|
||||
}
|
||||
|
||||
// 保持向后兼容的导出方式
|
||||
const applicationController = new ApplicationController();
|
||||
export const list = applicationController.list.bind(applicationController);
|
||||
export const detail = applicationController.detail.bind(applicationController);
|
||||
|
||||
66
apps/server/controllers/auth.ts
Normal file
66
apps/server/controllers/auth.ts
Normal file
@@ -0,0 +1,66 @@
|
||||
import type { Context } from 'koa';
|
||||
import { Controller, Get, Post } from '../decorators/route.ts';
|
||||
import prisma from '../libs/db.ts';
|
||||
import { log } from '../libs/logger.ts';
|
||||
import { gitea } from '../libs/gitea.ts';
|
||||
|
||||
@Controller('/auth')
|
||||
export class AuthController {
|
||||
private readonly TAG = 'Auth';
|
||||
|
||||
@Get('/url')
|
||||
async url() {
|
||||
return {
|
||||
url: `${process.env.GITEA_URL}/login/oauth/authorize?client_id=${process.env.GITEA_CLIENT_ID}&redirect_uri=${process.env.GITEA_REDIRECT_URI}&response_type=code&state=STATE`,
|
||||
};
|
||||
}
|
||||
|
||||
@Post('/login')
|
||||
async login(ctx: Context) {
|
||||
if (!ctx.session.isNew) {
|
||||
return ctx.session.user;
|
||||
}
|
||||
const { code } = (ctx.request as any).body;
|
||||
const { access_token } = await gitea.getToken(code);
|
||||
const giteaUser = await gitea.getUserInfo(access_token);
|
||||
log.debug(this.TAG, 'gitea user: %o', giteaUser);
|
||||
const exist = await prisma.user.findFirst({
|
||||
where: {
|
||||
login: giteaUser.login,
|
||||
email: giteaUser.email,
|
||||
},
|
||||
});
|
||||
if (exist == null) {
|
||||
const createdUser = await prisma.user.create({
|
||||
data: {
|
||||
id: giteaUser.id,
|
||||
login: giteaUser.login,
|
||||
email: giteaUser.email,
|
||||
username: giteaUser.username,
|
||||
avatar_url: giteaUser.avatar_url,
|
||||
active: giteaUser.active,
|
||||
createdAt: giteaUser.created,
|
||||
},
|
||||
});
|
||||
log.debug(this.TAG, '新建用户成功 %o', createdUser);
|
||||
ctx.session.user = createdUser;
|
||||
} else {
|
||||
const updatedUser = await prisma.user.update({
|
||||
where: {
|
||||
id: exist.id,
|
||||
},
|
||||
data: {
|
||||
login: giteaUser.login,
|
||||
email: giteaUser.email,
|
||||
username: giteaUser.username,
|
||||
avatar_url: giteaUser.avatar_url,
|
||||
active: giteaUser.active,
|
||||
createdAt: giteaUser.created,
|
||||
},
|
||||
});
|
||||
log.debug(this.TAG, '更新用户信息成功 %o', updatedUser);
|
||||
ctx.session.user = updatedUser;
|
||||
}
|
||||
return ctx.session.user;
|
||||
}
|
||||
}
|
||||
94
apps/server/libs/gitea.ts
Normal file
94
apps/server/libs/gitea.ts
Normal file
@@ -0,0 +1,94 @@
|
||||
interface TokenResponse {
|
||||
access_token: string;
|
||||
token_type: string;
|
||||
expires_in: number;
|
||||
refresh_token: string;
|
||||
}
|
||||
|
||||
interface GiteaUser {
|
||||
id: number;
|
||||
login: string;
|
||||
login_name: string;
|
||||
source_id: number;
|
||||
full_name: string;
|
||||
email: string;
|
||||
avatar_url: string;
|
||||
html_url: string;
|
||||
language: string;
|
||||
is_admin: boolean;
|
||||
last_login: string;
|
||||
created: string;
|
||||
restricted: boolean;
|
||||
active: boolean;
|
||||
prohibit_login: boolean;
|
||||
location: string;
|
||||
website: string;
|
||||
description: string;
|
||||
visibility: string;
|
||||
followers_count: number;
|
||||
following_count: number;
|
||||
starred_repos_count: number;
|
||||
username: string;
|
||||
}
|
||||
|
||||
class Gitea {
|
||||
private get config() {
|
||||
return {
|
||||
giteaUrl: process.env.GITEA_URL!,
|
||||
clientId: process.env.GITEA_CLIENT_ID!,
|
||||
clientSecret: process.env.GITEA_CLIENT_SECRET!,
|
||||
redirectUri: process.env.GITEA_REDIRECT_URI!,
|
||||
}
|
||||
}
|
||||
|
||||
async getToken(code: string) {
|
||||
const { giteaUrl, clientId, clientSecret, redirectUri } = this.config;
|
||||
console.log('this.config', this.config);
|
||||
const response = await fetch(
|
||||
`${giteaUrl}/login/oauth/access_token`,
|
||||
{
|
||||
method: 'POST',
|
||||
headers: this.getHeaders(),
|
||||
body: JSON.stringify({
|
||||
client_id: clientId,
|
||||
client_secret: clientSecret,
|
||||
code,
|
||||
grant_type: 'authorization_code',
|
||||
redirect_uri: redirectUri,
|
||||
}),
|
||||
},
|
||||
);
|
||||
if (!response.ok) {
|
||||
console.log(await response.json());
|
||||
throw new Error(`Fetch failed: ${response.status}`);
|
||||
}
|
||||
return (await response.json()) as TokenResponse;
|
||||
}
|
||||
/**
|
||||
* 获取用户信息
|
||||
* @param accessToken 访问令牌
|
||||
*/
|
||||
async getUserInfo(accessToken: string) {
|
||||
const response = await fetch(`${this.config.giteaUrl}/api/v1/user`, {
|
||||
method: 'GET',
|
||||
headers: this.getHeaders(accessToken),
|
||||
});
|
||||
if (!response.ok) {
|
||||
throw new Error(`Fetch failed: ${response.status}`);
|
||||
}
|
||||
const result = (await response.json()) as GiteaUser;
|
||||
return result;
|
||||
}
|
||||
|
||||
private getHeaders(accessToken?: string) {
|
||||
const headers: Record<string, string> = {
|
||||
'Content-Type': 'application/json',
|
||||
};
|
||||
if (accessToken) {
|
||||
headers['Authorization'] = `token ${accessToken}`;
|
||||
}
|
||||
return headers;
|
||||
}
|
||||
}
|
||||
|
||||
export const gitea = new Gitea();
|
||||
38
apps/server/libs/logger.ts
Normal file
38
apps/server/libs/logger.ts
Normal file
@@ -0,0 +1,38 @@
|
||||
import pino from 'pino';
|
||||
|
||||
class Logger {
|
||||
private readonly logger: pino.Logger;
|
||||
|
||||
constructor() {
|
||||
this.logger = pino({
|
||||
transport: {
|
||||
target: 'pino-pretty',
|
||||
options: {
|
||||
singleLine: true,
|
||||
colorize: true,
|
||||
translateTime: 'SYS:standard',
|
||||
ignore: 'pid,hostname',
|
||||
},
|
||||
},
|
||||
level: 'debug',
|
||||
});
|
||||
}
|
||||
|
||||
debug(tag: string, message: string, ...args: unknown[]) {
|
||||
if (args.length > 0) {
|
||||
this.logger.debug({ TAG: tag }, message, ...(args as []));
|
||||
} else {
|
||||
this.logger.debug({ TAG: tag }, message);
|
||||
}
|
||||
}
|
||||
|
||||
info(tag: string, message: string, ...args: unknown[]) {
|
||||
this.logger.info({ TAG: tag }, message, ...(args as []));
|
||||
}
|
||||
|
||||
error(tag: string, message: string, ...args: unknown[]) {
|
||||
this.logger.error({ TAG: tag }, message, ...(args as []));
|
||||
}
|
||||
}
|
||||
|
||||
export const log = new Logger();
|
||||
@@ -74,8 +74,6 @@ export class RouteScanner {
|
||||
default:
|
||||
console.warn(`未支持的HTTP方法: ${route.method}`);
|
||||
}
|
||||
|
||||
console.log(`注册路由: ${route.method} ${fullPath} -> ${ControllerClass.name}.${route.propertyKey}`);
|
||||
});
|
||||
}
|
||||
|
||||
@@ -115,10 +113,7 @@ export class RouteScanner {
|
||||
const result = await method.call(instance, ctx, next);
|
||||
|
||||
// 如果控制器返回了数据,则包装成统一响应格式
|
||||
if (result !== undefined) {
|
||||
ctx.body = createAutoSuccessResponse(result);
|
||||
}
|
||||
// 如果控制器没有返回数据,说明已经自己处理了响应
|
||||
ctx.body = createAutoSuccessResponse(result);
|
||||
} catch (error) {
|
||||
// 错误由全局异常处理中间件处理
|
||||
throw error;
|
||||
|
||||
17
apps/server/middlewares/cors.ts
Normal file
17
apps/server/middlewares/cors.ts
Normal 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') || '*';
|
||||
},
|
||||
}),
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -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(),
|
||||
};
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
14
apps/server/middlewares/logger.ts
Normal file
14
apps/server/middlewares/logger.ts
Normal 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`)
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -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`);
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -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}`,
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
24
apps/server/middlewares/session.ts
Normal file
24
apps/server/middlewares/session.ts
Normal 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,
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -10,14 +10,19 @@
|
||||
"author": "",
|
||||
"license": "ISC",
|
||||
"dependencies": {
|
||||
"@koa/cors": "^5.0.0",
|
||||
"@koa/router": "^14.0.0",
|
||||
"@prisma/client": "^6.15.0",
|
||||
"koa": "^3.0.1"
|
||||
"koa": "^3.0.1",
|
||||
"koa-session": "^7.0.2",
|
||||
"pino": "^9.9.1",
|
||||
"pino-pretty": "^13.1.1"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@tsconfig/node-ts": "^23.6.1",
|
||||
"@tsconfig/node22": "^22.0.2",
|
||||
"@types/koa": "^3.0.0",
|
||||
"@types/koa__cors": "^5.0.0",
|
||||
"@types/koa__router": "^12.0.4",
|
||||
"@types/node": "^24.3.0",
|
||||
"prisma": "^6.15.0",
|
||||
|
||||
Binary file not shown.
@@ -33,3 +33,17 @@ model Environment {
|
||||
createdBy String
|
||||
updatedBy String
|
||||
}
|
||||
|
||||
model User {
|
||||
id Int @id @default(autoincrement())
|
||||
username String
|
||||
login String
|
||||
email String
|
||||
avatar_url String?
|
||||
active Boolean @default(true)
|
||||
valid Int @default(1)
|
||||
createdAt DateTime @default(now())
|
||||
updatedAt DateTime @updatedAt
|
||||
createdBy String @default("system")
|
||||
updatedBy String @default("system")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user