-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
92 lines (76 loc) · 2.64 KB
/
Copy pathmain.go
File metadata and controls
92 lines (76 loc) · 2.64 KB
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
package main
import (
"context"
"fmt"
"github.com/99designs/gqlgen/graphql"
"github.com/KnightHacks/knighthacks_shared/auth"
"github.com/KnightHacks/knighthacks_shared/database"
"github.com/KnightHacks/knighthacks_shared/pagination"
"github.com/KnightHacks/knighthacks_shared/utils"
"github.com/KnightHacks/knighthacks_sponsors/repository"
"github.com/gin-gonic/gin"
"github.com/jackc/pgx/v5/pgxpool"
"github.com/vektah/gqlparser/v2/gqlerror"
"log"
"os"
"runtime/debug"
"github.com/99designs/gqlgen/graphql/handler"
"github.com/99designs/gqlgen/graphql/playground"
"github.com/KnightHacks/knighthacks_sponsors/graph"
"github.com/KnightHacks/knighthacks_sponsors/graph/generated"
)
const defaultPort = "8080"
func main() {
log.SetFlags(log.Ldate | log.Ltime | log.Lmicroseconds | log.Llongfile)
port := os.Getenv("PORT")
if port == "" {
port = defaultPort
}
pool, err := database.ConnectWithRetries(utils.GetEnvOrDie("DATABASE_URI"))
if err != nil {
log.Fatalf("Unable to connect to database: %v\n", err)
}
newAuth, err := auth.NewAuthWithEnvironment()
if err != nil {
log.Fatalf("An error occured when trying to create an instance of Auth: %s\n", err)
}
ginRouter := gin.Default()
ginRouter.Use(auth.AuthContextMiddleware(newAuth))
ginRouter.Use(utils.GinContextMiddleware())
ginRouter.POST("/query", graphqlHandler(newAuth, pool))
ginRouter.GET("/", playgroundHandler())
log.Fatal(ginRouter.Run(":" + port))
}
func graphqlHandler(a *auth.Auth, pool *pgxpool.Pool) gin.HandlerFunc {
// TODO: Sponsor doesn't have a sense of ownership, maybe we should have sponsor linked users?
hasRoleDirective := auth.HasRoleDirective{GetUserId: auth.DefaultGetUserId}
config := generated.Config{
Resolvers: &graph.Resolver{
Repository: repository.NewDatabaseRepository(pool),
Auth: a,
},
Directives: generated.DirectiveRoot{
HasRole: hasRoleDirective.Direct,
Pagination: pagination.Pagination,
},
}
srv := handler.NewDefaultServer(generated.NewExecutableSchema(config))
srv.SetRecoverFunc(func(ctx context.Context, iErr interface{}) error {
err := fmt.Errorf("%v", iErr)
log.Printf("runtime error: %v\n\n%v\n", err, string(debug.Stack()))
return gqlerror.Errorf("Internal server error! Check logs for more details!")
})
srv.SetErrorPresenter(func(ctx context.Context, err error) *gqlerror.Error {
log.Println("Error presented: ", err)
return graphql.DefaultErrorPresenter(ctx, err)
})
return func(c *gin.Context) {
srv.ServeHTTP(c.Writer, c.Request)
}
}
func playgroundHandler() gin.HandlerFunc {
h := playground.Handler("GraphQL", "/query")
return func(c *gin.Context) {
h.ServeHTTP(c.Writer, c.Request)
}
}