feat: 完善项目架构和功能
- 修复路由配置,实现根路径自动重定向到/project - 新增Gitea OAuth认证系统和相关组件 - 完善日志系统实现,包含pino日志工具和中间件 - 重构页面结构,分离项目管理和环境管理页面 - 新增CORS、Session等关键中间件 - 优化前端请求封装和类型定义 - 修复TypeScript类型错误和参数传递问题
This commit is contained in:
@@ -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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user