342 lines
10 KiB
JavaScript
342 lines
10 KiB
JavaScript
const express = require('express');
|
|
const router = express.Router();
|
|
const {
|
|
getConfigurations,
|
|
getConfigurationById,
|
|
createConfiguration,
|
|
updateConfiguration,
|
|
deleteConfiguration,
|
|
getLoadModelGroups,
|
|
getLoadModelGroupById,
|
|
createLoadModelGroup,
|
|
updateLoadModelGroup,
|
|
deleteLoadModelGroup,
|
|
deleteLoadModelGroupByConf,
|
|
getLoadModels,
|
|
getLoadModelById,
|
|
createLoadModel,
|
|
updateLoadModel,
|
|
deleteLoadModel,
|
|
deleteLoadModelsByGroup,
|
|
getLoadServices,
|
|
getLoadServiceById,
|
|
createLoadService,
|
|
updateLoadService,
|
|
deleteLoadService,
|
|
deleteLoadServicesByConf
|
|
} = require('../utils/configuration-utils');
|
|
const { validatePaths } = require('../utils/path-utils');
|
|
|
|
// 获取所有构型列表
|
|
router.get('/configurations', (req, res) => {
|
|
try {
|
|
const planeName = req.query.plane;
|
|
const configs = getConfigurations(planeName);
|
|
res.json(configs);
|
|
} catch (error) {
|
|
console.error('获取构型列表失败:', error);
|
|
res.status(500).json({ error: '获取构型列表失败', details: error.message });
|
|
}
|
|
});
|
|
|
|
// 根据ID获取构型
|
|
router.get('/configurations/:id', (req, res) => {
|
|
try {
|
|
const config = getConfigurationById(req.params.id);
|
|
if (!config) {
|
|
return res.status(404).json({ error: '构型不存在' });
|
|
}
|
|
res.json(config);
|
|
} catch (error) {
|
|
console.error('获取构型详情失败:', error);
|
|
res.status(500).json({ error: '获取构型详情失败', details: error.message });
|
|
}
|
|
});
|
|
|
|
// 创建新构型
|
|
router.post('/configurations', (req, res) => {
|
|
try {
|
|
const result = createConfiguration(req.body);
|
|
res.status(201).json(result);
|
|
} catch (error) {
|
|
console.error('创建构型失败:', error);
|
|
res.status(500).json({ error: '创建构型失败', details: error.message });
|
|
}
|
|
});
|
|
|
|
// 更新构型
|
|
router.put('/configurations/:id', async (req, res) => {
|
|
try {
|
|
const configData = { ...req.body, ConfID: req.params.id };
|
|
|
|
// 验证路径
|
|
const pathValidation = await validatePaths({
|
|
workPath: configData.WorkPath,
|
|
modelsPath: configData.ModelsPath,
|
|
servicesPath: configData.ServicesPath
|
|
});
|
|
|
|
// 检查是否有无效路径
|
|
const invalidPaths = Object.entries(pathValidation)
|
|
.filter(([_, result]) => !result.valid)
|
|
.map(([key, result]) => ({
|
|
path: key,
|
|
error: result.error
|
|
}));
|
|
|
|
if (invalidPaths.length > 0) {
|
|
return res.status(400).json({
|
|
error: '路径验证失败',
|
|
details: invalidPaths
|
|
});
|
|
}
|
|
|
|
// 使用规范化后的路径更新构型
|
|
configData.WorkPath = pathValidation.workPath.normalizedPath;
|
|
configData.ModelsPath = pathValidation.modelsPath.normalizedPath;
|
|
configData.ServicesPath = pathValidation.servicesPath.normalizedPath;
|
|
|
|
const result = updateConfiguration(configData);
|
|
res.json(result);
|
|
} catch (error) {
|
|
console.error('更新构型失败:', error);
|
|
res.status(500).json({ error: '更新构型失败', details: error.message });
|
|
}
|
|
});
|
|
|
|
// 删除构型
|
|
router.delete('/configurations/:id', (req, res) => {
|
|
try {
|
|
const result = deleteConfiguration(req.params.id);
|
|
res.json(result);
|
|
} catch (error) {
|
|
console.error('删除构型失败:', error);
|
|
res.status(500).json({ error: '删除构型失败', details: error.message });
|
|
}
|
|
});
|
|
|
|
// 模型组相关路由
|
|
// 获取构型下的所有模型组
|
|
router.get('/configurations/:id/model-groups', (req, res) => {
|
|
try {
|
|
const groups = getLoadModelGroups(req.params.id);
|
|
res.json(groups);
|
|
} catch (error) {
|
|
console.error('获取模型组列表失败:', error);
|
|
res.status(500).json({ error: '获取模型组列表失败', details: error.message });
|
|
}
|
|
});
|
|
|
|
// 获取特定模型组
|
|
router.get('/model-groups/:groupId', (req, res) => {
|
|
try {
|
|
const group = getLoadModelGroupById(req.params.groupId);
|
|
if (!group) {
|
|
return res.status(404).json({ error: '模型组不存在' });
|
|
}
|
|
res.json(group);
|
|
} catch (error) {
|
|
console.error('获取模型组详情失败:', error);
|
|
res.status(500).json({ error: '获取模型组详情失败', details: error.message });
|
|
}
|
|
});
|
|
|
|
// 创建新模型组
|
|
router.post('/configurations/:id/model-groups', (req, res) => {
|
|
try {
|
|
const groupData = { ...req.body, ConfID: req.params.id };
|
|
const result = createLoadModelGroup(groupData);
|
|
res.status(201).json(result);
|
|
} catch (error) {
|
|
console.error('创建模型组失败:', error);
|
|
res.status(500).json({ error: '创建模型组失败', details: error.message });
|
|
}
|
|
});
|
|
|
|
// 更新模型组
|
|
router.put('/model-groups/:groupId', (req, res) => {
|
|
try {
|
|
const groupData = { ...req.body, GroupID: req.params.groupId };
|
|
const result = updateLoadModelGroup(groupData);
|
|
res.json(result);
|
|
} catch (error) {
|
|
console.error('更新模型组失败:', error);
|
|
res.status(500).json({ error: '更新模型组失败', details: error.message });
|
|
}
|
|
});
|
|
|
|
// 删除模型组
|
|
router.delete('/model-groups/:groupId', (req, res) => {
|
|
try {
|
|
const result = deleteLoadModelGroup(req.params.groupId);
|
|
res.json(result);
|
|
} catch (error) {
|
|
console.error('删除模型组失败:', error);
|
|
res.status(500).json({ error: '删除模型组失败', details: error.message });
|
|
}
|
|
});
|
|
|
|
// 模型相关路由
|
|
// 获取组下的所有模型
|
|
router.get('/model-groups/:groupId/models', (req, res) => {
|
|
try {
|
|
const models = getLoadModels(req.params.groupId);
|
|
res.json(models);
|
|
} catch (error) {
|
|
console.error('获取模型列表失败:', error);
|
|
res.status(500).json({ error: '获取模型列表失败', details: error.message });
|
|
}
|
|
});
|
|
|
|
// 获取特定模型
|
|
router.get('/model-groups/:groupId/models/:className', (req, res) => {
|
|
try {
|
|
const model = getLoadModelById(req.params.groupId, req.params.className);
|
|
if (!model) {
|
|
return res.status(404).json({ error: '模型不存在' });
|
|
}
|
|
res.json(model);
|
|
} catch (error) {
|
|
console.error('获取模型详情失败:', error);
|
|
res.status(500).json({ error: '获取模型详情失败', details: error.message });
|
|
}
|
|
});
|
|
|
|
// 创建新模型
|
|
router.post('/model-groups/:groupId/models', (req, res) => {
|
|
try {
|
|
const modelData = { ...req.body, GroupID: req.params.groupId };
|
|
const result = createLoadModel(modelData);
|
|
res.status(201).json(result);
|
|
} catch (error) {
|
|
console.error('创建模型失败:', error);
|
|
res.status(500).json({ error: '创建模型失败', details: error.message });
|
|
}
|
|
});
|
|
|
|
// 更新模型
|
|
router.put('/model-groups/:groupId/models/:className', (req, res) => {
|
|
try {
|
|
const modelData = {
|
|
...req.body,
|
|
GroupID: req.params.groupId,
|
|
ClassName: req.params.className
|
|
};
|
|
const result = updateLoadModel(modelData);
|
|
res.json(result);
|
|
} catch (error) {
|
|
console.error('更新模型失败:', error);
|
|
res.status(500).json({ error: '更新模型失败', details: error.message });
|
|
}
|
|
});
|
|
|
|
// 删除模型
|
|
router.delete('/model-groups/:groupId/models/:className', (req, res) => {
|
|
try {
|
|
const result = deleteLoadModel(req.params.groupId, req.params.className);
|
|
res.json(result);
|
|
} catch (error) {
|
|
console.error('删除模型失败:', error);
|
|
res.status(500).json({ error: '删除模型失败', details: error.message });
|
|
}
|
|
});
|
|
|
|
// 服务相关路由
|
|
// 获取构型下的所有服务
|
|
router.get('/configurations/:id/services', (req, res) => {
|
|
try {
|
|
const services = getLoadServices(req.params.id);
|
|
res.json(services);
|
|
} catch (error) {
|
|
console.error('获取服务列表失败:', error);
|
|
res.status(500).json({ error: '获取服务列表失败', details: error.message });
|
|
}
|
|
});
|
|
|
|
// 获取特定服务
|
|
router.get('/configurations/:id/services/:className', (req, res) => {
|
|
try {
|
|
const service = getLoadServiceById(req.params.id, req.params.className);
|
|
if (!service) {
|
|
return res.status(404).json({ error: '服务不存在' });
|
|
}
|
|
res.json(service);
|
|
} catch (error) {
|
|
console.error('获取服务详情失败:', error);
|
|
res.status(500).json({ error: '获取服务详情失败', details: error.message });
|
|
}
|
|
});
|
|
|
|
// 创建新服务
|
|
router.post('/configurations/:id/services', (req, res) => {
|
|
try {
|
|
const serviceData = { ...req.body, ConfID: req.params.id };
|
|
const result = createLoadService(serviceData);
|
|
res.status(201).json(result);
|
|
} catch (error) {
|
|
console.error('创建服务失败:', error);
|
|
res.status(500).json({ error: '创建服务失败', details: error.message });
|
|
}
|
|
});
|
|
|
|
// 更新服务
|
|
router.put('/configurations/:id/services/:className', (req, res) => {
|
|
try {
|
|
const serviceData = {
|
|
...req.body,
|
|
ConfID: req.params.id,
|
|
ClassName: req.params.className
|
|
};
|
|
const result = updateLoadService(serviceData);
|
|
res.json(result);
|
|
} catch (error) {
|
|
console.error('更新服务失败:', error);
|
|
res.status(500).json({ error: '更新服务失败', details: error.message });
|
|
}
|
|
});
|
|
|
|
// 删除服务
|
|
router.delete('/configurations/:id/services/:className', (req, res) => {
|
|
try {
|
|
const result = deleteLoadService(req.params.id, req.params.className);
|
|
res.json(result);
|
|
} catch (error) {
|
|
console.error('删除服务失败:', error);
|
|
res.status(500).json({ error: '删除服务失败', details: error.message });
|
|
}
|
|
});
|
|
|
|
// 删除构型下的所有模型组
|
|
router.delete('/configurations/:id/model-groups', (req, res) => {
|
|
try {
|
|
const result = deleteLoadModelGroupByConf(req.params.id);
|
|
res.json(result);
|
|
} catch (error) {
|
|
console.error('删除构型下所有模型组失败:', error);
|
|
res.status(500).json({ error: '删除构型下所有模型组失败', details: error.message });
|
|
}
|
|
});
|
|
|
|
// 删除组下的所有模型
|
|
router.delete('/model-groups/:groupId/models', (req, res) => {
|
|
try {
|
|
const result = deleteLoadModelsByGroup(req.params.groupId);
|
|
res.json(result);
|
|
} catch (error) {
|
|
console.error('删除组下所有模型失败:', error);
|
|
res.status(500).json({ error: '删除组下所有模型失败', details: error.message });
|
|
}
|
|
});
|
|
|
|
// 删除构型下的所有服务
|
|
router.delete('/configurations/:id/services', (req, res) => {
|
|
try {
|
|
const result = deleteLoadServicesByConf(req.params.id);
|
|
res.json(result);
|
|
} catch (error) {
|
|
console.error('删除构型下所有服务失败:', error);
|
|
res.status(500).json({ error: '删除构型下所有服务失败', details: error.message });
|
|
}
|
|
});
|
|
|
|
module.exports = router;
|