2024-10-26 05:31:22 +03:00
|
|
|
import express from 'express';
|
|
|
|
import UserRouter from './routers/user.js';
|
2024-11-12 21:22:13 +03:00
|
|
|
import UserService from './services/user.js';
|
2024-10-26 05:31:22 +03:00
|
|
|
import GroupRouter from './routers/group.js';
|
2024-10-26 20:18:14 +03:00
|
|
|
import AbstractProductRouter from './routers/abstractproduct.js';
|
2024-10-27 05:45:12 +03:00
|
|
|
import log from './utils/log.js';
|
2024-10-26 05:31:22 +03:00
|
|
|
|
|
|
|
import config from '../config.json' with {type: "json"};
|
2024-10-27 04:45:13 +03:00
|
|
|
import ProductRouter from './routers/product.js';
|
2024-10-27 05:35:36 +03:00
|
|
|
import CategoryRouter from './routers/category.js';
|
2024-10-26 05:31:22 +03:00
|
|
|
|
2024-11-12 21:22:13 +03:00
|
|
|
import { WebSocketServer } from 'ws';
|
|
|
|
import http from 'http'
|
|
|
|
import jwt from 'jsonwebtoken';
|
|
|
|
|
2024-10-26 05:31:22 +03:00
|
|
|
const app = express();
|
2024-11-12 21:22:13 +03:00
|
|
|
const server = http.createServer(app);
|
|
|
|
const wss = new WebSocketServer({ port: config.wsport });
|
|
|
|
|
|
|
|
const clients = []
|
2024-10-26 05:31:22 +03:00
|
|
|
|
2024-10-28 17:17:47 +03:00
|
|
|
app.use(express.urlencoded({ extended: false, limit: "200mb", parameterLimit: 100000 }));
|
|
|
|
app.use(express.json({ limit: "200mb", parameterLimit: 1000000 }));
|
2024-10-27 04:45:13 +03:00
|
|
|
|
2024-10-26 05:31:22 +03:00
|
|
|
app.use('/api/user/', UserRouter);
|
|
|
|
app.use('/api/group/', GroupRouter);
|
2024-10-26 20:18:14 +03:00
|
|
|
app.use('/api/abstractproduct', AbstractProductRouter);
|
2024-10-27 04:45:13 +03:00
|
|
|
app.use('/api/product', ProductRouter);
|
2024-10-27 05:35:36 +03:00
|
|
|
app.use('/api/category', CategoryRouter);
|
2024-10-26 05:31:22 +03:00
|
|
|
|
2024-11-12 12:31:56 +03:00
|
|
|
app.get('/status', (req, res) => {
|
|
|
|
return res.status(200).send("All OK");
|
|
|
|
});
|
|
|
|
|
2024-11-12 21:22:13 +03:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
});
|
|
|
|
|
|
|
|
|
2024-10-26 05:31:22 +03:00
|
|
|
app.listen(config.port, () => {
|
|
|
|
log.info(`Application has started on port ${config.port}`)
|
2024-11-12 21:22:13 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
export default clients;
|