-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
134 lines (108 loc) · 2.46 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
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
122
123
124
125
126
127
128
129
130
131
132
133
134
package main
import (
"flag"
"log"
"os"
"time"
"github.com/gin-contrib/cors"
"github.com/gin-gonic/gin"
"github.com/joho/godotenv"
)
var secretKey string
const jwtExpireTime = time.Hour * 24
var myContext *MyContext
func main() {
LoadEnviromentVariables()
myContext = newMyContext()
port := os.Getenv("PORT")
if len(port) == 0 {
port = ":8080"
}
startGin(port)
}
func LoadEnviromentVariables() {
err := godotenv.Load()
if err != nil {
log.Printf("Error : %v\n", err)
}
secretKey = os.Getenv("SECRET_KEY")
if len(secretKey) == 0 {
panic("Secret key emty")
}
}
func CorsConfig() cors.Config {
return cors.Config{
AllowMethods: []string{"GET", "POST", "PUT", "PATCH", "DELETE", "HEAD"},
AllowHeaders: []string{"Origin", "Content-Length", "Content-Type", "Authorization"},
AllowCredentials: false,
MaxAge: 12 * time.Hour,
AllowAllOrigins: true, //Change later
}
}
func startGin(port string) *gin.Engine {
r := gin.Default()
// TODO : Look Before Production (Security)
/*config := cors.DefaultConfig()
config.AllowHeaders = append(config.AllowHeaders, "userid")
config.AllowAllOrigins = true
//config.AllowOrigins = []string{"http://localhost:3000"}*/
r.Use(cors.New(CorsConfig()))
r.GET("/", func(c *gin.Context) {
c.String(200, "Server is Running!")
})
user := r.Group("/user")
{
user.POST("/login", handleLogin)
user.POST("/register", handleRegister)
}
stream := r.Group("/stream")
{
stream.POST("/sdp/:roomid", sdpRoute)
}
//TODO : Remove later
test := r.Group("/test")
{
test.GET("/users/:roomid", listUsers)
test.GET("/listRoomCache", listRoomCache)
}
room := r.Group("/room")
{
room.POST("/get/:roomid", getRoomRoute)
//Add Test
room.POST("/chat/:roomid", chatRoomRoute)
room.GET("/ws/:roomid", wsRoomRoute)
}
r.Use(requiredAuthentication)
{
room := r.Group("/room")
{
room.POST("/join/:roomid", joinRoomRoute)
room.POST("/create", createRoomRoute)
room.POST("/listRooms", listRoomsRoute)
}
stream := r.Group("/stream_private")
{
stream.POST("/sdp/:roomid", sdpRoute)
}
}
/*if createTestUser {
println("Creating test users hlt and hlt2")
userRepository.RegisterUser(&User{
ID: 0,
Name: "hlt",
Password: "asdfg",
})
userRepository.RegisterUser(&User{
ID: 0,
Name: "hlt2",
Password: "asdfg",
})
}*/
if flag.Lookup("test.v") == nil {
r.Run(port)
} else {
go r.Run(port)
Info("Server Started")
}
return r
}