-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.ts
121 lines (99 loc) · 2.96 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
import express, { NextFunction, Request, Response } from "express"
const app = express();
import compression from "compression"
import { createServer } from "http"
import { configDotenv } from "dotenv";
import session from "express-session";
import { connectDatabase } from "./config/db";
import cors from "cors"
import path from "path"
import morgan from "morgan"
var MongoDBStore = require('connect-mongodb-session')(session);
import UserRoutes from "./routes/user.routes"
import BotRoutes from "./routes/bot.routes"
import { Server } from "socket.io";
import { Socket } from "socket.io";
import { ControlMessage } from "./utils/ControlMessage";
import { createWhatsappClient, getCurrentUser, userJoin, userLeave } from "./utils/CreateWhatsappClient";
export const server = createServer(app)
let AppSocket: Socket;
configDotenv()
const PORT = Number(process.env.PORT) || 5000
const HOST = process.env.HOST || "http://localhost"
const ENV = process.env.NODE_ENV
connectDatabase()
let origin = ""
if (ENV === "development") {
origin = "http://localhost:3000"
app.use(cors({
origin: [origin],
credentials: true
}))
}
let io: Server | undefined = undefined
io = new Server(server, {
cors: {
origin: origin
}
});
io.on("connection", (socket) => {
console.log("socket connected")
AppSocket = socket
socket.on('JoinRoom', async (id: string, path: string) => {
console.log("running in room", id, path)
const user = userJoin(id)
socket.join(user.id)
if (io)
createWhatsappClient(id, path, io)
socket.on("disconnect", (reason) => {
let user = getCurrentUser(id)
if (user)
userLeave(user.id)
console.log(`socket ${socket.id} disconnected due to ${reason}`);
});
})
});
const store = new MongoDBStore({
uri: process.env.DB_URL || `mongodb://127.0.0.1:27017/BOT`,
collection: 'sessions'
})
store.on('error', function (error: any) {
console.log(error);
});
app.use(express.json())
app.use(compression())
app.use(morgan('tiny'))
app.use(session({
secret: 'secret',
cookie: {
maxAge: 1000 * 60 * 60 * 24 * 7 // 1 week
},
store: store,
resave: true,
saveUninitialized: false
}))
app.use("/api/v1", UserRoutes)
app.use("/api/v1", BotRoutes)
//serve client
if (ENV === "production") {
app.use(express.static(path.join(__dirname, "build")))
app.get('*', (req, res) => {
res.sendFile(path.join(__dirname, "build/", "index.html"));
})
}
else {
app.use("*", (_req: Request, res: Response, _next: NextFunction) => {
res.status(404).json({ message: "resource not found" })
})
}
//error handler
app.use((err: Error, req: Request, res: Response, next: NextFunction) => {
console.log(err);
res.status(500).json({
message: err.message || "unknown error occured"
})
})
server.listen(PORT, () => {
console.log(`running on ${HOST}:${PORT}`)
});
export { io }