277 lines
7.1 KiB
JavaScript
277 lines
7.1 KiB
JavaScript
const express = require('express');
|
|
const path = require('path');
|
|
const db = require('./db');
|
|
|
|
const app = express();
|
|
const PORT = process.env.PORT || 3000;
|
|
|
|
// Middleware
|
|
app.use(express.json());
|
|
app.use(express.static(path.join(__dirname, '../public')));
|
|
|
|
// API Routes
|
|
|
|
// Projects
|
|
app.get('/api/projects', async (req, res) => {
|
|
try {
|
|
const projects = await db.getAllProjects();
|
|
res.json(projects);
|
|
} catch (err) {
|
|
res.status(500).json({ error: err.message });
|
|
}
|
|
});
|
|
|
|
app.get('/api/projects/:id', async (req, res) => {
|
|
try {
|
|
const project = await db.getProject(req.params.id);
|
|
if (!project) {
|
|
res.status(404).json({ error: 'Project not found' });
|
|
} else {
|
|
res.json(project);
|
|
}
|
|
} catch (err) {
|
|
res.status(500).json({ error: err.message });
|
|
}
|
|
});
|
|
|
|
app.post('/api/projects', async (req, res) => {
|
|
try {
|
|
const { name, description } = req.body;
|
|
const project = await db.createProject(name, description);
|
|
res.status(201).json(project);
|
|
} catch (err) {
|
|
res.status(500).json({ error: err.message });
|
|
}
|
|
});
|
|
|
|
app.put('/api/projects/:id', async (req, res) => {
|
|
try {
|
|
const { name, description } = req.body;
|
|
await db.updateProject(req.params.id, name, description);
|
|
res.json({ success: true });
|
|
} catch (err) {
|
|
res.status(500).json({ error: err.message });
|
|
}
|
|
});
|
|
|
|
app.delete('/api/projects/:id', async (req, res) => {
|
|
try {
|
|
await db.deleteProject(req.params.id);
|
|
res.json({ success: true });
|
|
} catch (err) {
|
|
res.status(500).json({ error: err.message });
|
|
}
|
|
});
|
|
|
|
// Racks
|
|
app.get('/api/racks', async (req, res) => {
|
|
try {
|
|
const projectId = req.query.projectId || 1;
|
|
const racks = await db.getAllRacks(projectId);
|
|
res.json(racks);
|
|
} catch (err) {
|
|
res.status(500).json({ error: err.message });
|
|
}
|
|
});
|
|
|
|
app.get('/api/racks/next-name', async (req, res) => {
|
|
try {
|
|
const projectId = req.query.projectId || 1;
|
|
const prefix = req.query.prefix || 'RACK';
|
|
const name = await db.getNextRackName(projectId, prefix);
|
|
res.json({ name });
|
|
} catch (err) {
|
|
res.status(500).json({ error: err.message });
|
|
}
|
|
});
|
|
|
|
app.post('/api/racks', async (req, res) => {
|
|
try {
|
|
const { projectId, name, x, y } = req.body;
|
|
const rack = await db.createRack(projectId || 1, name, x, y);
|
|
res.status(201).json(rack);
|
|
} catch (err) {
|
|
res.status(500).json({ error: err.message });
|
|
}
|
|
});
|
|
|
|
app.put('/api/racks/:id/position', async (req, res) => {
|
|
try {
|
|
const { x, y } = req.body;
|
|
await db.updateRackPosition(req.params.id, x, y);
|
|
res.json({ success: true });
|
|
} catch (err) {
|
|
res.status(500).json({ error: err.message });
|
|
}
|
|
});
|
|
|
|
app.put('/api/racks/:id/name', async (req, res) => {
|
|
try {
|
|
const { name } = req.body;
|
|
await db.updateRackName(req.params.id, name);
|
|
res.json({ success: true });
|
|
} catch (err) {
|
|
res.status(500).json({ error: err.message });
|
|
}
|
|
});
|
|
|
|
app.delete('/api/racks/:id', async (req, res) => {
|
|
try {
|
|
await db.deleteRack(req.params.id);
|
|
res.json({ success: true });
|
|
} catch (err) {
|
|
res.status(500).json({ error: err.message });
|
|
}
|
|
});
|
|
|
|
// Device Types
|
|
app.get('/api/device-types', async (req, res) => {
|
|
try {
|
|
const types = await db.getAllDeviceTypes();
|
|
res.json(types);
|
|
} catch (err) {
|
|
res.status(500).json({ error: err.message });
|
|
}
|
|
});
|
|
|
|
// Devices
|
|
app.get('/api/devices', async (req, res) => {
|
|
try {
|
|
const projectId = req.query.projectId || 1;
|
|
const devices = await db.getAllDevices(projectId);
|
|
res.json(devices);
|
|
} catch (err) {
|
|
res.status(500).json({ error: err.message });
|
|
}
|
|
});
|
|
|
|
app.post('/api/devices', async (req, res) => {
|
|
try {
|
|
const { deviceTypeId, rackId, position, name } = req.body;
|
|
const device = await db.createDevice(deviceTypeId, rackId, position, name);
|
|
res.status(201).json(device);
|
|
} catch (err) {
|
|
res.status(500).json({ error: err.message });
|
|
}
|
|
});
|
|
|
|
app.delete('/api/devices/:id', async (req, res) => {
|
|
try {
|
|
await db.deleteDevice(req.params.id);
|
|
res.json({ success: true });
|
|
} catch (err) {
|
|
res.status(500).json({ error: err.message });
|
|
}
|
|
});
|
|
|
|
app.put('/api/devices/:id/rack', async (req, res) => {
|
|
try {
|
|
const { rackId, position } = req.body;
|
|
await db.updateDeviceRack(req.params.id, rackId, position);
|
|
res.json({ success: true });
|
|
} catch (err) {
|
|
res.status(500).json({ error: err.message });
|
|
}
|
|
});
|
|
|
|
app.put('/api/devices/:id/logical-position', async (req, res) => {
|
|
try {
|
|
const { x, y } = req.body;
|
|
await db.updateDeviceLogicalPosition(req.params.id, x, y);
|
|
res.json({ success: true });
|
|
} catch (err) {
|
|
res.status(500).json({ error: err.message });
|
|
}
|
|
});
|
|
|
|
app.put('/api/devices/:id/name', async (req, res) => {
|
|
try {
|
|
const { name } = req.body;
|
|
await db.updateDeviceName(req.params.id, name);
|
|
res.json({ success: true });
|
|
} catch (err) {
|
|
res.status(500).json({ error: err.message });
|
|
}
|
|
});
|
|
|
|
app.put('/api/devices/:id/rack-units', async (req, res) => {
|
|
try {
|
|
const { rackUnits } = req.body;
|
|
await db.updateDeviceRackUnits(req.params.id, rackUnits);
|
|
res.json({ success: true });
|
|
} catch (err) {
|
|
res.status(500).json({ error: err.message });
|
|
}
|
|
});
|
|
|
|
app.get('/api/devices/:id/used-ports', async (req, res) => {
|
|
try {
|
|
const ports = await db.getUsedPorts(req.params.id);
|
|
res.json(ports);
|
|
} catch (err) {
|
|
res.status(500).json({ error: err.message });
|
|
}
|
|
});
|
|
|
|
// Connections
|
|
app.get('/api/connections', async (req, res) => {
|
|
try {
|
|
const projectId = req.query.projectId || 1;
|
|
const connections = await db.getAllConnections(projectId);
|
|
res.json(connections);
|
|
} catch (err) {
|
|
res.status(500).json({ error: err.message });
|
|
}
|
|
});
|
|
|
|
app.post('/api/connections', async (req, res) => {
|
|
try {
|
|
const { sourceDeviceId, sourcePort, targetDeviceId, targetPort } = req.body;
|
|
const connection = await db.createConnection(sourceDeviceId, sourcePort, targetDeviceId, targetPort);
|
|
res.status(201).json(connection);
|
|
} catch (err) {
|
|
res.status(500).json({ error: err.message });
|
|
}
|
|
});
|
|
|
|
app.put('/api/connections/:id/waypoints', async (req, res) => {
|
|
try {
|
|
const { waypoints, view } = req.body;
|
|
await db.updateConnectionWaypoints(req.params.id, waypoints, view);
|
|
res.json({ success: true });
|
|
} catch (err) {
|
|
res.status(500).json({ error: err.message });
|
|
}
|
|
});
|
|
|
|
app.put('/api/connections/:id', async (req, res) => {
|
|
try {
|
|
const { sourceDeviceId, sourcePort, targetDeviceId, targetPort } = req.body;
|
|
await db.updateConnection(req.params.id, sourceDeviceId, sourcePort, targetDeviceId, targetPort);
|
|
res.json({ success: true });
|
|
} catch (err) {
|
|
res.status(500).json({ error: err.message });
|
|
}
|
|
});
|
|
|
|
app.delete('/api/connections/:id', async (req, res) => {
|
|
try {
|
|
await db.deleteConnection(req.params.id);
|
|
res.json({ success: true });
|
|
} catch (err) {
|
|
res.status(500).json({ error: err.message });
|
|
}
|
|
});
|
|
|
|
// Initialize database and start server
|
|
db.init()
|
|
.then(() => {
|
|
app.listen(PORT, () => {
|
|
console.log(`Server running on http://localhost:${PORT}`);
|
|
});
|
|
})
|
|
.catch((err) => {
|
|
console.error('Failed to initialize database:', err);
|
|
process.exit(1);
|
|
});
|