-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
66 lines (55 loc) · 1.32 KB
/
main.go
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
package main
import (
"altmis-alti/room"
"github.com/gin-gonic/gin"
"github.com/gorilla/websocket"
"log"
"net/http"
)
var wsUpgrader = websocket.Upgrader{
ReadBufferSize: 1024,
WriteBufferSize: 1024,
}
func main() {
go room.H.Run()
r := gin.Default()
r.Static("/cards", "./templates/cards")
r.Static("/scripts", "./templates/scripts")
r.LoadHTMLGlob("templates/views/*")
r.GET("/ping", func(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{
"message": "pong",
})
})
r.GET("/", func(c *gin.Context) {
c.HTML(http.StatusOK, "index.html", gin.H{})
})
r.POST("/room", func(c *gin.Context) {
roomId := room.NewRequestId().String()
c.JSON(http.StatusOK, gin.H{
"roomId": roomId,
})
})
r.GET("/room/:roomId", func(c *gin.Context) {
c.HTML(http.StatusOK, "room.html", gin.H{
"roomId": c.Param("roomId"),
})
})
r.GET("/ws/:roomId", func(c *gin.Context) {
roomId := c.Param("roomId")
serveWs(c.Writer, c.Request, roomId)
})
r.Run()
}
func serveWs(w http.ResponseWriter, r *http.Request, roomId string) {
ws, err := wsUpgrader.Upgrade(w, r, nil)
if err != nil {
log.Println(err.Error())
return
}
c := &room.Connection{Id: room.NewRequestId().String(), Send: make(chan []byte, 256), Ws: ws}
s := room.Subscription{Conn: c, Room: roomId}
room.H.Register <- s
go s.WritePump()
go s.ReadPump()
}