This commit is contained in:
2024-06-06 15:14:23 +05:00
parent af10788000
commit ccca61d2bf
9 changed files with 288 additions and 169 deletions
+1 -1
View File
@@ -2,7 +2,7 @@ import { connect } from "mongoose";
async function connectDB() {
try {
await connect(process.env.MONGO_URI!, { dbName: "pixel_streaming" });
await connect(process.env.MONGO_URI!, { dbName: "pixel_streaming2" });
console.log("MongoDB connected...");
} catch (error) {
if (error instanceof Error) {
+7 -66
View File
@@ -1,82 +1,23 @@
import { connect } from "mongoose";
import "dotenv/config";
import connectDB from "./config/db";
import express, { json } from "express";
import cors from "cors";
import connectDB from "./config/db";
import activeSessionRouter from "./routes/activeSession";
import { Server } from "socket.io";
import { createServer } from "http";
import getCountryCodeRouter from "./routes/getCountryCode";
import sendInviteRouter from "./routes/sendInvite";
connectDB();
const app = express();
const httpServer = createServer(app);
const port = process.env.PORT || 3000;
const port = process.env.PORT;
app.use(json());
app.use(cors());
app.get("/", (_req, res) => {
res.json({ ok: 1 });
});
app.use("/activeSessions", activeSessionRouter);
app.use("/getCountryCode", getCountryCodeRouter);
app.use("/sendInvite", sendInviteRouter);
const io = new Server(httpServer, {
cors: {
origin: "*",
},
});
interface User {
id: string;
name: string;
isAdmin: boolean;
}
interface Message {
name: string;
text: string;
}
interface Auth {
roomId: string;
user: User;
}
io.on("connection", async (socket) => {
const { roomId, user } = socket.handshake.auth as Auth;
if (!roomId || !user) return;
socket.data = { roomId, user };
socket.join(roomId);
const sockets = await io.to(roomId).fetchSockets();
const roomUsers = sockets.map((socket) => socket.handshake.auth.user);
socket.on("connection", () => {
console.log("connect");
});
setTimeout(() => {
io.to(roomId).emit("update", roomUsers);
}, 1);
socket.on("message", (message: Message) => {
io.to(roomId).emit("message", message);
});
socket.on("disconnect", async () => {
const sockets = await io.to(roomId).fetchSockets();
const roomUsers = sockets.map((socket) => socket.handshake.auth.user);
setTimeout(() => {
io.to(roomId).emit("update", roomUsers);
}, 1);
});
});
httpServer.listen(port, () => {
app.listen(port, () => {
console.log(`Server is listening on port ${port}`);
});
+5
View File
@@ -12,6 +12,11 @@ router.get("/:id", async (req, res) => {
}
const activeSession = await ActiveSession.findById(activeSessionId);
await ActiveSession.findByIdAndUpdate(activeSessionId, {
updatedAt: new Date(),
});
console.log("activeSession", activeSession);
res.json(activeSession);
});
+29
View File
@@ -0,0 +1,29 @@
import { Router } from "express";
import got from "got-cjs";
const router = Router();
router.get("/", async (req, res) => {
const ip = req.headers["x-real-ip"];
if (!ip)
return res.json({
error: "An error occurred while obtaining an IP address",
});
try {
const { countryCode }: { countryCode: string } = await got
.get(`http://ip-api.com/json/${ip}`)
.json();
res.json({ countryCode });
} catch (error) {
if (error instanceof Error) {
res.json({ error: error.message });
}
}
});
const getCountryCodeRouter = router;
export default getCountryCodeRouter;
-21
View File
@@ -1,21 +0,0 @@
import { Router } from "express";
import { isValidObjectId } from "mongoose";
import ActiveSession from "../models/ActiveSession";
const router = Router();
router.get("/:id", async (req, res) => {
const activeSessionId = req.params.id;
if (!isValidObjectId(activeSessionId)) {
return res.json({ status: "error", message: "Invalid ObjectId" });
}
const activeSession = await ActiveSession.findById(activeSessionId);
res.json(activeSession);
});
const roomRouter = router;
export default roomRouter;
+38
View File
@@ -0,0 +1,38 @@
import { Router } from "express";
import nodemailer from "nodemailer";
const router = Router();
router.post("/", async (req, res) => {
const { email, link } = req.body;
console.log("email", email);
console.log("link", link);
// create reusable transporter object using the default SMTP transport
let transporter = nodemailer.createTransport({
host: "mail.netangels.ru",
port: 587,
secure: false, // true for 465, false for other ports
auth: {
user: "stream@graff.tech", // generated ethereal user
pass: "zLUbt8Io7dh2F9KT", // generated ethereal password
},
});
// send mail with defined transport object
await transporter.sendMail({
from: "stream@graff.tech", // sender address
to: email, // list of receivers
subject: "Приглашение на демонстрацию - stream.graff.tech", // Subject line
html: `<div>
Ссылка для подключения к демонстрации: <a href="${link}" target="_blank">${link}</a>
</div>`,
});
res.json({ ok: 1 });
});
const sendInviteRouter = router;
export default sendInviteRouter;