72 lines
2.1 KiB
JavaScript
72 lines
2.1 KiB
JavaScript
const express = require('express');
|
|
const http = require('http');
|
|
const { WebSocketServer } = require('ws');
|
|
const cors = require('cors');
|
|
const path = require('path');
|
|
|
|
const db = require('./db');
|
|
const pdfRoutes = require('./routes/pdfs');
|
|
const conversationRoutes = require('./routes/conversations');
|
|
const chatRoutes = require('./routes/chat');
|
|
const progressRoutes = require('./routes/progress');
|
|
const notesRoutes = require('./routes/notes');
|
|
const modelRoutes = require('./routes/models');
|
|
const configRoutes = require('./routes/config');
|
|
|
|
const app = express();
|
|
const server = http.createServer(app);
|
|
const PORT = process.env.PORT || 3001;
|
|
|
|
// Middleware
|
|
app.use(cors({
|
|
origin: ['http://localhost:5173', 'http://localhost:3001'],
|
|
credentials: true,
|
|
}));
|
|
app.use(express.json({ limit: '10mb' }));
|
|
app.use(express.urlencoded({ extended: true }));
|
|
|
|
// API Routes
|
|
app.use('/api/pdfs', pdfRoutes);
|
|
app.use('/api/conversations', conversationRoutes);
|
|
app.use('/api/chat', chatRoutes);
|
|
app.use('/api/progress', progressRoutes);
|
|
app.use('/api/notes', notesRoutes);
|
|
app.use('/api/models', modelRoutes);
|
|
app.use('/api/config', configRoutes);
|
|
|
|
// Serve React build in production
|
|
const clientDist = path.resolve(__dirname, '..', 'client', 'dist');
|
|
app.use(express.static(clientDist));
|
|
app.get('*', (req, res, next) => {
|
|
if (req.path.startsWith('/api')) return next();
|
|
res.sendFile(path.join(clientDist, 'index.html'), (err) => {
|
|
if (err) next();
|
|
});
|
|
});
|
|
|
|
// WebSocket server
|
|
const wss = new WebSocketServer({ server });
|
|
wss.on('connection', (ws) => {
|
|
ws.on('message', (message) => {
|
|
// Echo — extensible for real-time notifications
|
|
});
|
|
try {
|
|
ws.send(JSON.stringify({ type: 'connected', message: 'StudyOS WebSocket connected' }));
|
|
} catch (err) {
|
|
console.error('[ws] send error:', err.message);
|
|
}
|
|
});
|
|
|
|
async function start() {
|
|
await db.initDB();
|
|
server.listen(PORT, () => {
|
|
console.log(`StudyOS server running on http://localhost:${PORT}`);
|
|
console.log(`WebSocket server running on ws://localhost:${PORT}`);
|
|
});
|
|
}
|
|
|
|
start().catch(err => {
|
|
console.error('Failed to start server:', err);
|
|
process.exit(1);
|
|
});
|