secutorpro commited on
Commit
b8a98f2
·
verified ·
1 Parent(s): d1ce9f2

HTTP + WebSocket entrypoint

Browse files
Files changed (1) hide show
  1. server/src/index.js +32 -0
server/src/index.js ADDED
@@ -0,0 +1,32 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import express from 'express';
2
+ import cors from 'cors';
3
+ import http from 'node:http';
4
+ import path from 'node:path';
5
+ import { fileURLToPath } from 'node:url';
6
+ import { WebSocketServer } from 'ws';
7
+ import authRouter, { requireAuth } from './auth.js';
8
+ import routes from './routes.js';
9
+ import Hub from './realtime.js';
10
+
11
+ const __dirname = path.dirname(fileURLToPath(import.meta.url));
12
+ const PORT = Number(process.env.PORT) || 8080;
13
+
14
+ const app = express();
15
+ app.use(cors());
16
+ app.use(express.json());
17
+
18
+ app.use('/api', authRouter); // /api/login
19
+ app.use('/api', requireAuth, routes); // everything else
20
+
21
+ const webDist = path.join(__dirname, '..', 'web', 'dist');
22
+ app.use(express.static(webDist));
23
+ app.use((req, res) => {
24
+ if (req.path.startsWith('/api')) return res.status(404).json({ error: 'not found' });
25
+ res.sendFile(path.join(webDist, 'index.html'));
26
+ });
27
+
28
+ const server = http.createServer(app);
29
+ const wss = new WebSocketServer({ server, path: '/ws' });
30
+ app.set('hub', new Hub(wss));
31
+
32
+ server.listen(PORT, () => console.log(`[dispatch] listening on :${PORT}`));