feat: Docker部署与CI/CD集成, 搜索栏修复, 上传目录改为data
This commit is contained in:
61
pages/api/v1/admin/config.ts
Normal file
61
pages/api/v1/admin/config.ts
Normal file
@@ -0,0 +1,61 @@
|
||||
import { NextApiResponse } from 'next';
|
||||
import { AuthenticatedRequest } from '../../../../lib/middleware/authMiddleware';
|
||||
import { requireAdmin } from '../../../../lib/middleware/adminMiddleware';
|
||||
import { getServerConfig, setServerConfig } from '../../../../lib/serverConfig';
|
||||
import { resetPrisma } from '../../../../lib/prisma';
|
||||
|
||||
const SYSTEM_CONFIG = {
|
||||
maintenanceMode: false,
|
||||
apiVersion: '1.0.0',
|
||||
maxUploadMB: getServerConfig().uploadMaxMB,
|
||||
dbHost: getServerConfig().dbHost,
|
||||
dbPort: getServerConfig().dbPort,
|
||||
dbUser: getServerConfig().dbUser,
|
||||
dbPass: getServerConfig().dbPass,
|
||||
dbName: getServerConfig().dbName,
|
||||
uploadDir: getServerConfig().uploadDir,
|
||||
};
|
||||
|
||||
export default async function handler(req: AuthenticatedRequest, res: NextApiResponse) {
|
||||
// Require admin authentication
|
||||
const isAdmin = await requireAdmin(req, res);
|
||||
if (!isAdmin) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (req.method === 'GET') {
|
||||
// Return non-sensitive system configuration
|
||||
return res.status(200).json({
|
||||
success: true,
|
||||
data: SYSTEM_CONFIG
|
||||
});
|
||||
}
|
||||
|
||||
if (req.method === 'PUT') {
|
||||
const body = req.body || {};
|
||||
const before = getServerConfig();
|
||||
const nextCfg = setServerConfig({
|
||||
uploadMaxMB: body.maxUploadMB,
|
||||
uploadDir: body.uploadDir,
|
||||
dbHost: body.dbHost,
|
||||
dbPort: body.dbPort,
|
||||
dbUser: body.dbUser,
|
||||
dbPass: body.dbPass,
|
||||
dbName: body.dbName,
|
||||
});
|
||||
SYSTEM_CONFIG.maxUploadMB = nextCfg.uploadMaxMB;
|
||||
SYSTEM_CONFIG.uploadDir = nextCfg.uploadDir;
|
||||
SYSTEM_CONFIG.dbHost = nextCfg.dbHost;
|
||||
SYSTEM_CONFIG.dbPort = nextCfg.dbPort;
|
||||
SYSTEM_CONFIG.dbUser = nextCfg.dbUser;
|
||||
SYSTEM_CONFIG.dbPass = nextCfg.dbPass;
|
||||
SYSTEM_CONFIG.dbName = nextCfg.dbName;
|
||||
const dbChanged = before.dbHost !== nextCfg.dbHost || before.dbPort !== nextCfg.dbPort || before.dbUser !== nextCfg.dbUser || before.dbPass !== nextCfg.dbPass || before.dbName !== nextCfg.dbName;
|
||||
if (dbChanged) {
|
||||
resetPrisma();
|
||||
}
|
||||
return res.status(200).json({ success: true, message: 'Configuration updated successfully', data: SYSTEM_CONFIG });
|
||||
}
|
||||
|
||||
return res.status(405).json({ success: false, error: 'Method not allowed' });
|
||||
}
|
||||
24
pages/api/v1/admin/users.ts
Normal file
24
pages/api/v1/admin/users.ts
Normal file
@@ -0,0 +1,24 @@
|
||||
import { NextApiResponse } from 'next';
|
||||
import { AuthenticatedRequest } from '../../../../lib/middleware/authMiddleware';
|
||||
import { requireAdmin } from '../../../../lib/middleware/adminMiddleware';
|
||||
import { UserService } from '../../../../backend/services/userService';
|
||||
|
||||
export default async function handler(req: AuthenticatedRequest, res: NextApiResponse) {
|
||||
if (req.method !== 'GET') {
|
||||
return res.status(405).json({ success: false, error: 'Method not allowed' });
|
||||
}
|
||||
|
||||
// Require admin authentication
|
||||
const isAdmin = await requireAdmin(req, res);
|
||||
if (!isAdmin) {
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
const users = await UserService.getAllUsers();
|
||||
return res.status(200).json({ success: true, data: users });
|
||||
} catch (error) {
|
||||
console.error('Error fetching users:', error);
|
||||
return res.status(500).json({ success: false, error: 'Failed to fetch users' });
|
||||
}
|
||||
}
|
||||
28
pages/api/v1/admin/users/[id]/role.ts
Normal file
28
pages/api/v1/admin/users/[id]/role.ts
Normal file
@@ -0,0 +1,28 @@
|
||||
import { NextApiResponse } from 'next';
|
||||
import { AuthenticatedRequest } from '@/lib/middleware/authMiddleware';
|
||||
import { requireAdmin } from '@/lib/middleware/adminMiddleware';
|
||||
import { UserService } from '@/backend/services/userService';
|
||||
|
||||
export default async function handler(req: AuthenticatedRequest, res: NextApiResponse) {
|
||||
if (req.method !== 'POST') {
|
||||
return res.status(405).json({ success: false, error: 'Method not allowed' });
|
||||
}
|
||||
|
||||
const isAdmin = await requireAdmin(req, res);
|
||||
if (!isAdmin) return;
|
||||
|
||||
const { id } = req.query;
|
||||
const { role } = req.body || {};
|
||||
|
||||
if (typeof id !== 'string' || typeof role !== 'string') {
|
||||
return res.status(400).json({ success: false, error: 'Invalid input' });
|
||||
}
|
||||
|
||||
try {
|
||||
const updated = await UserService.updateUserRole(id, role as any);
|
||||
return res.status(200).json({ success: true, data: updated });
|
||||
} catch (error) {
|
||||
console.error('Error updating role:', error);
|
||||
return res.status(500).json({ success: false, error: 'Failed to update role' });
|
||||
}
|
||||
}
|
||||
30
pages/api/v1/admin/users/[id]/toggle-status.ts
Normal file
30
pages/api/v1/admin/users/[id]/toggle-status.ts
Normal file
@@ -0,0 +1,30 @@
|
||||
import { NextApiResponse } from 'next';
|
||||
import { AuthenticatedRequest } from '../../../../../../lib/middleware/authMiddleware';
|
||||
import { requireAdmin } from '../../../../../../lib/middleware/adminMiddleware';
|
||||
import { UserService } from '../../../../../../backend/services/userService';
|
||||
|
||||
export default async function handler(req: AuthenticatedRequest, res: NextApiResponse) {
|
||||
if (req.method !== 'POST') {
|
||||
return res.status(405).json({ success: false, error: 'Method not allowed' });
|
||||
}
|
||||
|
||||
// Require admin authentication
|
||||
const isAdmin = await requireAdmin(req, res);
|
||||
if (!isAdmin) {
|
||||
return;
|
||||
}
|
||||
|
||||
const { id } = req.query;
|
||||
|
||||
if (typeof id !== 'string') {
|
||||
return res.status(400).json({ success: false, error: 'Invalid user ID' });
|
||||
}
|
||||
|
||||
try {
|
||||
const updatedUser = await UserService.toggleUserStatus(id);
|
||||
return res.status(200).json({ success: true, data: updatedUser });
|
||||
} catch (error) {
|
||||
console.error('Error toggling user status:', error);
|
||||
return res.status(500).json({ success: false, error: 'Failed to toggle user status' });
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user