import express from 'express'; import UserRouter from './routers/user.js'; import UserService from './services/user.js'; import GroupRouter from './routers/group.js'; import AbstractProductRouter from './routers/abstractproduct.js'; import log from './utils/log.js'; import config from '../config.json' with {type: "json"}; import ProductRouter from './routers/product.js'; import CategoryRouter from './routers/category.js'; import { WebSocketServer } from 'ws'; import http from 'http' import jwt from 'jsonwebtoken'; const app = express(); const server = http.createServer(app); const wss = new WebSocketServer({ port: config.wsport }); const clients = [] app.use(express.urlencoded({ extended: false, limit: "200mb", parameterLimit: 100000 })); app.use(express.json({ limit: "200mb", parameterLimit: 1000000 })); app.use('/api/user/', UserRouter); app.use('/api/group/', GroupRouter); app.use('/api/abstractproduct', AbstractProductRouter); app.use('/api/product', ProductRouter); app.use('/api/category', CategoryRouter); app.get('/status', (req, res) => { return res.status(200).send("All OK"); }); wss.on('connection', (client) => { client.on('message', async (message) => { let parsed = JSON.parse(message) let token = parsed.token let currentGroup = parsed.currentGroup if (!jwt.verify(token, config.secret)) { client.send("Invalid token"); return } if (!await UserService.isInGroup(jwt.decode(token, config.secret).login.id, currentGroup)) { client.send("Not a member of specified group") return } clients.push({ socket: client, token, currentGroup }); }); client.on('close', () => { for (let i = 0; i < clients.length; i++) { if (clients[i].socket == client) { clients.splice(i, 1); break; } } }) }); app.listen(config.port, () => { log.info(`Application has started on port ${config.port}`) }); export default clients;