Files
agent/mock/server.mock.ts
2026-02-16 12:46:37 +08:00

80 lines
2.6 KiB
TypeScript

import { Request, Response } from 'express';
const genServerList = (current: number, pageSize: number) => {
const tableListDataSource: any[] = [];
const statusList = ['online', 'offline', 'maintenance'];
for (let i = 0; i < pageSize; i += 1) {
const status = statusList[i % 3];
tableListDataSource.push({
key: i,
id: `srv-${i}`,
name: `Server-${i}`,
ip: `192.168.1.${i}`,
status,
os: 'Ubuntu 22.04 LTS',
cpu: Math.floor(Math.random() * 100),
memory: Math.floor(Math.random() * 100),
tags: ['web', 'production'],
updatedAt: new Date().toISOString(),
createdAt: new Date().toISOString(),
});
}
return tableListDataSource;
};
let tableListDataSource = genServerList(1, 40);
export default {
'GET /api/servers': (req: Request, res: Response) => {
const { current = 1, pageSize = 20 } = req.query as any; // Cast to any to access pagination params
// Simulate pagination filter
let dataSource = [...tableListDataSource].slice(
((current as number) - 1) * (pageSize as number),
(current as number) * (pageSize as number),
);
res.json({
data: dataSource,
total: tableListDataSource.length,
success: true,
pageSize,
current: parseInt(`${current}`, 10) || 1,
});
},
'POST /api/servers': (req: Request, res: Response) => {
const newData = {
...req.body,
updatedAt: new Date().toISOString(),
createdAt: new Date().toISOString(),
id: `srv-${Math.floor(Math.random() * 1000)}`,
key: Math.floor(Math.random() * 1000),
};
tableListDataSource.unshift(newData);
res.json(newData);
},
'PUT /api/servers/:id': (req: Request, res: Response) => {
const { id } = req.params;
const index = tableListDataSource.findIndex(item => item.id === id);
if (index !== -1) {
tableListDataSource[index] = { ...tableListDataSource[index], ...req.body };
res.json(tableListDataSource[index]);
} else {
res.status(404).json({ success: false });
}
},
'DELETE /api/servers/:id': (req: Request, res: Response) => {
const { id } = req.params;
tableListDataSource = tableListDataSource.filter(item => item.id !== id);
res.json({ success: true });
},
'POST /api/servers/:id/restart': (req: Request, res: Response) => {
res.json({ success: true, status: 'restarting' });
},
};