start express server with sio
This commit is contained in:
55
backend/server/src/index.ts
Normal file
55
backend/server/src/index.ts
Normal file
@ -0,0 +1,55 @@
|
||||
import express, { Express, NextFunction, Request, Response } from "express"
|
||||
import dotenv from "dotenv"
|
||||
import { createServer } from "http"
|
||||
import { Server } from "socket.io"
|
||||
|
||||
import { z } from "zod"
|
||||
|
||||
dotenv.config()
|
||||
|
||||
const app: Express = express()
|
||||
const port = process.env.PORT || 4000
|
||||
// app.use(cors())
|
||||
const httpServer = createServer(app)
|
||||
const io = new Server(httpServer, {
|
||||
cors: {
|
||||
origin: "*",
|
||||
},
|
||||
})
|
||||
|
||||
io.use(async (socket, next) => {
|
||||
const q = socket.handshake.query
|
||||
|
||||
console.log("middleware")
|
||||
console.log(q)
|
||||
|
||||
if (!q.userId || !q.sandboxId) {
|
||||
next(new Error("Invalid request."))
|
||||
}
|
||||
|
||||
const dbUser = await fetch(`http://localhost:8787/api/user?id=${q.userId}`)
|
||||
const dbUserJSON = await dbUser.json()
|
||||
|
||||
if (!dbUserJSON || !dbUserJSON.sandbox.includes(q.sandboxId)) {
|
||||
next(new Error("Invalid credentials."))
|
||||
}
|
||||
|
||||
next()
|
||||
})
|
||||
|
||||
io.on("connection", async (socket) => {
|
||||
console.log(`connection`)
|
||||
const userId = socket.handshake.query.userId
|
||||
|
||||
console.log(userId)
|
||||
|
||||
// socket.emit("loaded", {
|
||||
// rootContent: await fetchDir("/workspace", "")
|
||||
// });
|
||||
|
||||
// initHandlers(socket, replId);
|
||||
})
|
||||
|
||||
httpServer.listen(port, () => {
|
||||
console.log(`Server running on port ${port}`)
|
||||
})
|
Reference in New Issue
Block a user