145 lines
4.0 KiB
JavaScript
145 lines
4.0 KiB
JavaScript
const express = require('express');
|
|
const path = require('path');
|
|
const { authMiddleware } = require('../middleware/auth');
|
|
const projectService = require('../services/projectService');
|
|
const processManager = require('../services/processManager');
|
|
|
|
const router = express.Router();
|
|
|
|
router.get('/nginx/status', authMiddleware, (req, res) => {
|
|
const nginxManager = require('../services/nginxManager');
|
|
const config = require('../config');
|
|
|
|
res.json({
|
|
useNginx: config.useNginx,
|
|
nginxAvailable: nginxManager.checkNginxAvailable(),
|
|
configPath: path.resolve(config.nginxConfigDir, 'auto-deploy.conf')
|
|
});
|
|
});
|
|
|
|
router.post('/nginx/reload', authMiddleware, (req, res) => {
|
|
const nginxManager = require('../services/nginxManager');
|
|
const config = require('../config');
|
|
|
|
if (!config.isNginxMode()) {
|
|
return res.status(400).json({ error: 'Not in Nginx mode' });
|
|
}
|
|
|
|
try {
|
|
const result = nginxManager.safeReload();
|
|
res.json(result);
|
|
} catch (error) {
|
|
res.status(500).json({ error: error.message });
|
|
}
|
|
});
|
|
|
|
router.post('/nginx/regenerate', authMiddleware, (req, res) => {
|
|
const nginxManager = require('../services/nginxManager');
|
|
const projectService = require('../services/projectService');
|
|
const config = require('../config');
|
|
|
|
if (!config.isNginxMode()) {
|
|
return res.status(400).json({ error: 'Not in Nginx mode' });
|
|
}
|
|
|
|
try {
|
|
const projects = projectService.getAllProjects();
|
|
const runningProjects = projects.filter(p => p.status === 'running');
|
|
|
|
for (const project of runningProjects) {
|
|
nginxManager.addProjectLocation(project);
|
|
}
|
|
|
|
const result = nginxManager.safeReload();
|
|
res.json({
|
|
...result,
|
|
regeneratedCount: runningProjects.length
|
|
});
|
|
} catch (error) {
|
|
res.status(500).json({ error: error.message });
|
|
}
|
|
});
|
|
|
|
router.post('/:id/start', authMiddleware, async (req, res) => {
|
|
const project = projectService.getProjectById(req.params.id);
|
|
|
|
if (!project) {
|
|
return res.status(404).json({ error: 'Project not found' });
|
|
}
|
|
|
|
if (project.status === 'running') {
|
|
return res.status(400).json({ error: 'Project is already running' });
|
|
}
|
|
|
|
try {
|
|
projectService.updateProjectStatus(project.id, 'running', null, null);
|
|
const result = await processManager.startProject(project);
|
|
projectService.updateProjectStatus(project.id, 'running', result.port, result.url);
|
|
|
|
res.json({
|
|
message: 'Project started successfully',
|
|
url: result.url,
|
|
port: result.port
|
|
});
|
|
} catch (error) {
|
|
res.status(500).json({ error: error.message });
|
|
}
|
|
});
|
|
|
|
router.post('/:id/stop', authMiddleware, (req, res) => {
|
|
const project = projectService.getProjectById(req.params.id);
|
|
|
|
if (!project) {
|
|
return res.status(404).json({ error: 'Project not found' });
|
|
}
|
|
|
|
if (project.status !== 'running') {
|
|
return res.status(400).json({ error: 'Project is not running' });
|
|
}
|
|
|
|
try {
|
|
processManager.stopProject(project);
|
|
projectService.updateProjectStatus(project.id, 'stopped');
|
|
|
|
res.json({ message: 'Project stopped successfully' });
|
|
} catch (error) {
|
|
res.status(500).json({ error: error.message });
|
|
}
|
|
});
|
|
|
|
router.get('/ports/status', authMiddleware, async (req, res) => {
|
|
try {
|
|
const status = await processManager.getPortStatus();
|
|
res.json(status);
|
|
} catch (error) {
|
|
res.status(500).json({ error: error.message });
|
|
}
|
|
});
|
|
|
|
router.get('/:id/logs', authMiddleware, (req, res) => {
|
|
const project = projectService.getProjectById(req.params.id);
|
|
|
|
if (!project) {
|
|
return res.status(404).json({ error: 'Project not found' });
|
|
}
|
|
|
|
const logs = projectService.getProjectLogs(project.id);
|
|
res.json(logs);
|
|
});
|
|
|
|
router.get('/:id/status', authMiddleware, (req, res) => {
|
|
const project = projectService.getProjectById(req.params.id);
|
|
|
|
if (!project) {
|
|
return res.status(404).json({ error: 'Project not found' });
|
|
}
|
|
|
|
res.json({
|
|
status: project.status,
|
|
port: project.port,
|
|
url: project.url,
|
|
lastDeployed: project.lastDeployed
|
|
});
|
|
});
|
|
|
|
module.exports = router; |