118 lines
3.5 KiB
TypeScript
118 lines
3.5 KiB
TypeScript
import express from 'express';
|
|
import cors from 'cors';
|
|
import cookieParser from 'cookie-parser';
|
|
import { createServer } from 'http';
|
|
import { Server } from 'socket.io';
|
|
import { prisma } from './lib/prisma';
|
|
import { LogTracker } from './services/logTracker';
|
|
import dotenv from 'dotenv';
|
|
import authRoutes from './routes/authRoutes';
|
|
import aiRoutes from './routes/aiRoutes';
|
|
import { authenticateToken, socketAuthMiddleware } from './middleware/authMiddleware';
|
|
|
|
// Load environment variables from .env file
|
|
dotenv.config();
|
|
|
|
const app = express();
|
|
const httpServer = createServer(app);
|
|
const io = new Server(httpServer, {
|
|
cors: {
|
|
origin: process.env.CORS_ORIGIN ? [process.env.CORS_ORIGIN, "http://localhost:5173"] : ["http://localhost:5173"], // Spécifier l'origine pour les credentials
|
|
methods: ["GET", "POST", "DELETE", "OPTIONS"],
|
|
credentials: true
|
|
}
|
|
});
|
|
|
|
// Sécurisation des WebSockets
|
|
io.use(socketAuthMiddleware);
|
|
|
|
const logTracker = new LogTracker(io, prisma);
|
|
|
|
app.use(cors({
|
|
origin: process.env.CORS_ORIGIN ? [process.env.CORS_ORIGIN, "http://localhost:5173"] : ["http://localhost:5173"],
|
|
credentials: true
|
|
}));
|
|
app.use(express.json());
|
|
app.use(cookieParser());
|
|
|
|
// Public Routes
|
|
app.use('/api/auth', authRoutes);
|
|
|
|
// Protected AI Routes
|
|
app.use('/api/ai', authenticateToken as any, aiRoutes);
|
|
|
|
// Protected Routes Middleware
|
|
app.use('/api', authenticateToken as any);
|
|
|
|
// API Routes
|
|
app.get('/api/services', async (req, res) => {
|
|
const services = await prisma.service.findMany();
|
|
res.json(services);
|
|
});
|
|
|
|
app.post('/api/services', async (req, res) => {
|
|
const service = await prisma.service.create({
|
|
data: req.body
|
|
});
|
|
logTracker.trackService(service);
|
|
res.json(service);
|
|
});
|
|
|
|
app.delete('/api/services/:id', async (req, res) => {
|
|
const id = parseInt(req.params.id);
|
|
await prisma.service.delete({ where: { id } });
|
|
logTracker.stopTracking(id);
|
|
res.json({ success: true });
|
|
});
|
|
|
|
app.get('/api/services/:id/logs', (req, res) => {
|
|
const id = parseInt(req.params.id);
|
|
const logs = logTracker.getLogHistory(id);
|
|
res.json(logs);
|
|
});
|
|
|
|
app.get('/api/docker/containers', async (req, res) => {
|
|
const containers = await logTracker.getDockerContainers();
|
|
res.json(containers);
|
|
});
|
|
|
|
app.get('/api/os/services', async (req, res) => {
|
|
const services = await logTracker.getOsServices();
|
|
res.json(services);
|
|
});
|
|
|
|
app.get('/api/settings', async (req, res) => {
|
|
let settings = await prisma.globalSettings.findUnique({ where: { id: 1 } });
|
|
if (!settings) {
|
|
settings = await prisma.globalSettings.create({ data: { id: 1, notionToken: '', notionDbId: '', ollamaUrl: '', ollamaModel: '', searxngUrl: '' } });
|
|
}
|
|
res.json(settings);
|
|
});
|
|
|
|
app.post('/api/settings', async (req, res) => {
|
|
const { notionToken, notionDbId, ollamaUrl, ollamaModel, searxngUrl } = req.body;
|
|
const settings = await prisma.globalSettings.upsert({
|
|
where: { id: 1 },
|
|
update: { notionToken, notionDbId, ollamaUrl, ollamaModel, searxngUrl },
|
|
create: { id: 1, notionToken, notionDbId, ollamaUrl, ollamaModel, searxngUrl }
|
|
});
|
|
res.json(settings);
|
|
});
|
|
|
|
// Socket.io connection
|
|
io.on('connection', (socket) => {
|
|
console.log('✅ Client Socket.io connecté:', socket.id);
|
|
|
|
socket.on('disconnect', () => {
|
|
console.log('❌ Client Socket.io déconnecté:', socket.id);
|
|
});
|
|
});
|
|
|
|
const PORT = process.env.PORT || 3001;
|
|
httpServer.listen(PORT, async () => {
|
|
console.log(`Server running on port ${PORT}`);
|
|
|
|
// Start tracking existing services
|
|
const services = await prisma.service.findMany();
|
|
services.forEach(s => logTracker.trackService(s));
|
|
}); |