-
Notifications
You must be signed in to change notification settings - Fork 0
/
routes.go
66 lines (57 loc) · 1.51 KB
/
routes.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 (
"fmt"
"github.com/gin-contrib/cors"
"github.com/gin-gonic/gin"
"github.com/graphql-go/graphql"
"time"
auth "workour-api/authentication"
c "workour-api/config"
g "workour-api/gql"
)
var allowHeaders = []string{
"Accept",
"Accept-Encoding",
"Access-Control-Allow-Origin",
"Authorization",
"Content-Length",
"Content-Type",
"X-CSRF-Token",
}
func SetupRoutes(router *gin.Engine) {
router.Use(cors.New(cors.Config{
AllowOrigins: c.Configurations.CorsOrigins,
AllowMethods: []string{"POST", "PUT", "GET", "OPTIONS"},
AllowHeaders: allowHeaders,
AllowCredentials: true,
ExposeHeaders: []string{"Content-Length"},
MaxAge: 24 * time.Hour,
AllowFiles: true,
}))
publicRoutes(router)
adminRoutes(router)
}
// Set up public level routes
func publicRoutes(r *gin.Engine) {
rootQuery := g.NewRoot()
// Create a new graphql schema, passing in the root query
schema, err := graphql.NewSchema(
graphql.SchemaConfig{
Query: rootQuery.Query,
Mutation: rootQuery.Mutation,
},
)
if err != nil {
fmt.Println("error creating schema: ", err)
}
authController := new(auth.Controller)
r.POST("/login", authController.AuthenticateUser)
r.POST("/logout", authController.LogoutUser)
r.GET("/getCurrentUser", authController.GetCurrentUser)
r.POST("/public", g.GraphQL(schema))
}
// Set up role-protected routes
func adminRoutes(r *gin.Engine) {
admin := r.Group("/admin")
admin.Use(auth.VerifyAuthentication)
}