This repository has been archived by the owner on Nov 24, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
main.go
108 lines (78 loc) · 2.57 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
package main // import "github.com/monopayments/iban.im
import (
"context"
"encoding/json"
"log"
"net/http"
graphql "github.com/graph-gophers/graphql-go"
"github.com/monopayments/iban.im/config"
"github.com/monopayments/iban.im/handler"
_ "github.com/monopayments/iban.im/model"
// "github.com/monopayments/iban.im/model"
"github.com/monopayments/iban.im/resolvers"
"github.com/monopayments/iban.im/schema"
jwt "github.com/appleboy/gin-jwt/v2"
"fmt"
"github.com/gin-gonic/gin"
)
const identityKey = "UserID"
func main() {
router := gin.New()
router.Use(gin.Logger())
router.LoadHTMLGlob("templates/*.tmpl.html")
router.Static("/static", "static")
defer config.DB.Close()
context.Background()
authMiddleware, err := handler.AuthMiddleware()
if err != nil {
log.Fatal("JWT Error:" + err.Error())
}
router.POST("/api/login", authMiddleware.LoginHandler)
auth := router.Group("/auth")
auth.GET("/refresh_token", authMiddleware.RefreshHandler)
router.GET("/graph", func(c *gin.Context) {
fmt.Println("inside get graph")
c.HTML(http.StatusOK, "graph.tmpl.html", nil)
})
authMW := authMiddleware.MiddlewareFunc()
router.POST("/graph", func(c *gin.Context) {
fmt.Println("inside post graph")
ctx := c.Request.Context()
if _, ok := c.Request.Header["Authorization"]; ok {
authMW(c)
claims := jwt.ExtractClaims(c)
currentID, ok := claims[identityKey].(float64)
if !ok {
currentID = 0
}
ctx = context.WithValue(ctx, handler.ContextKey("UserID"), int(currentID))
}
var params struct {
Query string `json:"query"`
OperationName string `json:"operationName"`
Variables map[string]interface{} `json:"variables"`
}
if err := json.NewDecoder(c.Request.Body).Decode(¶ms); err != nil {
log.Println("decode error", err)
c.String(http.StatusInternalServerError, err.Error())
return
}
log.Println("params")
log.Println(params)
opts := []graphql.SchemaOpt{graphql.UseFieldResolvers()}
schema := graphql.MustParseSchema(*schema.NewSchema(), &resolvers.Resolvers{}, opts...)
response := schema.Exec(ctx, params.Query, params.OperationName, params.Variables)
if err != nil {
log.Println("graph response error", err.Error())
c.String(http.StatusInternalServerError, err.Error())
return
}
log.Println("response")
log.Printf("%v", string(response.Data))
c.JSON(200, response)
})
router.GET("/", func(c *gin.Context) {
c.HTML(http.StatusOK, "index.tmpl.html", nil)
})
log.Fatal(http.ListenAndServe(fmt.Sprintf(":%d", config.Config.App.Port), router))
}