-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
55 lines (45 loc) · 1.55 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
package main
import (
"github.com/abcd-edu/gentoo-users/internal/configs"
"github.com/abcd-edu/gentoo-users/internal/models"
"github.com/abcd-edu/gentoo-users/internal/services"
"github.com/gin-gonic/gin"
"github.com/spf13/viper"
)
func main() {
configs.InitializeViper()
services.InitializeOAuthGoogle()
models.InitializeDB()
router := gin.Default()
//store := cookie.NewStore([]byte("secret"))
//router.Use(sessions.Sessions("gentoo", store))
router.Use(CORSMiddleware())
v1 := router.Group("/v1")
v1.Use(CORSMiddleware())
{
v1.GET("/", services.HandleMain)
v1.GET("/login-gl", services.HandleGoogleLogin)
v1.GET("/callback-gl", services.CallBackFromGoogle)
v1.POST("/register", services.Register)
v1.GET("/logout", services.HandleLogout)
v1.Any("/auth", services.HandleAuthentication)
v1.GET("/user", services.GetUser)
v1.POST("/user/mute", services.MuteUser)
v1.POST("/user/ban", services.BanUser)
}
port := viper.GetString("serverPort")
router.Run(":" + port)
}
func CORSMiddleware() gin.HandlerFunc {
return func(c *gin.Context) {
c.Writer.Header().Set("Access-Control-Allow-Origin", "http://localhost:3000")
c.Writer.Header().Set("Access-Control-Allow-Credentials", "true")
c.Writer.Header().Set("Access-Control-Allow-Headers", "Content-Type, Content-Length, Accept-Encoding, X-CSRF-Token, Authorization, accept, origin, Cache-Control, X-Requested-With")
c.Writer.Header().Set("Access-Control-Allow-Methods", "POST, OPTIONS, GET, PUT")
if c.Request.Method == "OPTIONS" {
c.AbortWithStatus(204)
return
}
c.Next()
}
}