-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
72 lines (54 loc) · 1.75 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
67
68
69
70
71
72
package main
import (
"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
"github.com/olahol/melody"
)
type User struct {
UserID string `json:"userID"`
Username string `json:"username"`
}
type Answer struct {
UserID string `json:"userID"`
QuestionID string `json:"questionID"`
TimeLapsed int `json:"timeLapsed"`
OptionIDs []int `json:"optionIDs"`
}
type Option struct {
OptionID int `json:"optionID"`
Option string `json:"option"`
}
type Question struct {
QuestionID string `json:"questionID"`
Question string `json:"question"`
Options []Option `json:"options"`
}
func main() {
e := echo.New()
m := melody.New()
m.Config.MaxMessageSize = 1024 * 1024
e.Use(middleware.Logger())
e.Use(middleware.Recover())
e.Use(middleware.CORSWithConfig(middleware.CORSConfig{
AllowOrigins: []string{"http://localhost:5173", "https://carp.iiitk.in", "https://iiitk-carp.pages.dev"},
AllowMethods: []string{echo.GET, echo.PUT, echo.POST, echo.DELETE},
}))
e.GET("/", func(c echo.Context) error {
return c.HTML(200, "<p>You're not supposed to be here</p><script>console.log(\"Hi there :) We're looking for curious people like you join the Cyber Security Club at IIITK https://discord.gg/pVShHhrfX4 \")</script>")
})
e.GET("/api/ws", func(c echo.Context) error {
m.HandleRequest(c.Response().Writer, c.Request())
return nil
})
ssessionID := generateID()
e.GET("/api/session", func(c echo.Context) error {
return c.JSON(200, map[string]string{"sessionID": ssessionID})
})
e.GET("/api/init", handleInit)
e.POST("/api/register", handleRegister)
e.POST("/api/leaderboard", handleLeaderboard)
m.HandleMessage(func(s *melody.Session, msg []byte) {
sockHandler(*m, s, msg)
})
e.Logger.Fatal(e.Start(":5000"))
}