-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
69 lines (58 loc) · 1.37 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
package main
import (
"net/http"
"strings"
"github.com/gin-contrib/cors"
"github.com/gin-gonic/gin"
"jjungs_backend/components/auth"
"jjungs_backend/routes"
)
func main() {
g := gin.Default()
config := cors.DefaultConfig()
config.AllowOrigins = []string{"http://localhost:8080", "http://jjungs.kr", "https://jjungs.kr"}
config.AllowHeaders = []string{"Content-Type", "Authorization"}
config.AllowCredentials = true
g.Use(cors.New(config))
g.Use(PreHandler)
admin := g.Group("/admin")
authRoute := g.Group("/auth")
boards := g.Group("/board")
posts := g.Group("/post")
files := g.Group("/file")
admin.Use(OnlyAdmin)
routes.AdminRegister(admin)
routes.AuthRegister(authRoute)
routes.BoardRegister(boards)
routes.PostRegister(posts)
routes.FileRegister(files)
g.Run()
}
func PreHandler(c *gin.Context) {
authToken := c.GetHeader("Authorization")
if authToken != "" {
v, _ := auth.ValidateToken(strings.Split(authToken, " ")[1])
if v == "JJUNGS" {
c.Set("permissions", v)
}
}
c.Next()
}
func OnlyAdmin(c *gin.Context) {
authToken := c.GetHeader("Authorization")
if authToken == "" {
c.JSON(http.StatusUnauthorized, gin.H{
"error": "ERR401",
})
c.Abort()
return
}
if _, err := auth.ValidateToken(strings.Split(authToken, " ")[1]); err != nil {
c.JSON(http.StatusUnauthorized, gin.H{
"error": "ERR401",
})
c.Abort()
return
}
c.Next()
}