-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
87 lines (70 loc) · 2.16 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
package main
import (
"context"
"errors"
"html/template"
"log"
"net/http"
"time"
"firebase.google.com/go"
"github.com/99designs/gqlgen/handler"
"github.com/gin-gonic/gin"
"github.com/project-hermes/hermes-app/server/graph"
"google.golang.org/api/option"
"github.com/project-hermes/hermes-app/server/model"
"github.com/project-hermes/hermes-app/server/wrapper"
)
const (
// FirebaseApp is a constant for pulling app from gin
FirebaseApp = "FirebaseApp"
)
type templateParams struct {
Notice string
Name string
}
var (
indexTemplate = template.Must(template.ParseFiles("index.html"))
)
func init() {
}
func main() {
router := gin.Default()
router.Use(gin.ErrorLogger())
router.Use(gin.Recovery())
router.Use(InjectFirebase)
appGroup := router.Group("/")
appGroup.GET("/", helloForm)
gqlPlayground := gin.WrapH(handler.Playground("GraphQL playground", "/query"))
router.GET("/query", gqlPlayground)
dbClient, _ := wrapper.NewClient(context.Background(), "project-hermes-staging")
diveInt := model.NewDiveImplementation(dbClient)
resolver := graph.NewResolver(diveInt)
gql := gin.WrapH(handler.GraphQL(graph.NewExecutableSchema(graph.Config{Resolvers: &resolver})))
router.POST("/query", gql)
router.Run()
}
func helloForm(c *gin.Context) {
params := templateParams{}
indexTemplate.Execute(c.Writer, params)
return
}
// InjectFirebase will inject firebase
func InjectFirebase(c *gin.Context) {
log.Printf("Loading firebase app")
c.Keys = map[string]interface{}{}
ctx, _ := context.WithTimeout(context.Background(), 5*time.Second)
opt := option.WithHTTPClient(http.DefaultClient)
opt = option.WithScopes("https://www.googleapis.com/auth/cloud-platform", "https://www.googleapis.com/auth/userinfo.email")
databaseURL := "https://project-hermes-staging.firebaseio.com"
config := &firebase.Config{
DatabaseURL: databaseURL,
ProjectID: "project-hermes-staging",
}
if app, err := firebase.NewApp(ctx, config, opt); err != nil {
log.Fatalf("Unable to load firebase app: %s", err)
c.AbortWithError(http.StatusServiceUnavailable, errors.New("an error occurred while processing your request"))
} else {
c.Keys[FirebaseApp] = app
c.Next()
}
}