89 lines
2.4 KiB
JavaScript
89 lines
2.4 KiB
JavaScript
const express = require('express');
|
|
const { authMiddleware } = require('../middleware/auth');
|
|
const projectService = require('../services/projectService');
|
|
const processManager = require('../services/processManager');
|
|
|
|
const router = express.Router();
|
|
|
|
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 {
|
|
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; |