-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathhandlers.go
163 lines (148 loc) · 5.22 KB
/
handlers.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
package main
import (
"context"
"database/sql"
"github.com/99designs/gqlgen/graphql"
"github.com/99designs/gqlgen/graphql/handler"
"github.com/ansel1/merry/v2"
"github.com/aws/aws-sdk-go-v2/service/s3"
"github.com/bcc-code/bcc-media-platform/backend/analytics"
"github.com/bcc-code/bcc-media-platform/backend/auth0"
"github.com/bcc-code/bcc-media-platform/backend/common"
"github.com/bcc-code/bcc-media-platform/backend/email"
graphadmin "github.com/bcc-code/bcc-media-platform/backend/graph/admin"
graphadmingenerated "github.com/bcc-code/bcc-media-platform/backend/graph/admin/generated"
graphapi "github.com/bcc-code/bcc-media-platform/backend/graph/api"
graphapigenerated "github.com/bcc-code/bcc-media-platform/backend/graph/api/generated"
"github.com/bcc-code/bcc-media-platform/backend/graph/gqltracer"
graphpublic "github.com/bcc-code/bcc-media-platform/backend/graph/public"
graphpublicgenerated "github.com/bcc-code/bcc-media-platform/backend/graph/public/generated"
"github.com/bcc-code/bcc-media-platform/backend/log"
"github.com/bcc-code/bcc-media-platform/backend/remotecache"
"github.com/bcc-code/bcc-media-platform/backend/search"
"github.com/bcc-code/bcc-media-platform/backend/signing"
"github.com/bcc-code/bcc-media-platform/backend/sqlc"
"github.com/bcc-code/bcc-media-platform/backend/user"
"github.com/bcc-code/bcc-media-platform/backend/utils"
"github.com/bcc-code/bmm-sdk-golang"
"github.com/gin-gonic/gin"
"github.com/vektah/gqlparser/v2/gqlerror"
)
// Defining the Graphql handler
func graphqlHandler(
db *sql.DB,
queries *sqlc.Queries,
loaders *common.BatchLoaders,
searchService *search.Service,
emailService *email.Service,
urlSigner *signing.Signer,
config envConfig,
s3client *s3.Client,
analyticsSalt string,
authClient *auth0.Client,
remoteCache *remotecache.Client,
analyticsClient *analytics.Service,
bmmClient *bmm.APIClient,
) gin.HandlerFunc {
resolver := graphapi.Resolver{
Queries: queries,
Loaders: loaders,
FilteredLoaders: filteredLoaderFactory(db, queries, loaders.CollectionLoader),
ProfileLoaders: profileLoaderFactory(queries),
SearchService: searchService,
EmailService: emailService,
URLSigner: urlSigner,
S3Client: s3client,
APIConfig: config.CDNConfig,
AWSConfig: config.AWS,
RedirectConfig: config.Redirect,
AuthClient: authClient,
RemoteCache: remoteCache,
AnalyticsClient: analyticsClient,
BMMClient: bmmClient,
AnalyticsIDFactory: func(ctx context.Context) string {
ginCtx, err := utils.GinCtx(ctx)
p := user.GetProfileFromCtx(ginCtx)
if err != nil || p == nil {
return "anonymous"
}
u := user.GetFromCtx(ginCtx)
aID := analytics.GenerateID(p.ID, analyticsSalt)
if u.IsActiveBCC() {
return aID
}
return "ext-" + aID
},
}
tracer := &gqltracer.GraphTracer{}
// NewExecutableSchema and Config are in the generated.go file
// Resolver is in the resolver.go file
h := handler.NewDefaultServer(graphapigenerated.NewExecutableSchema(graphapigenerated.Config{Resolvers: &resolver}))
h.Use(tracer)
h.SetErrorPresenter(func(ctx context.Context, err error) *gqlerror.Error {
gqlError := graphql.DefaultErrorPresenter(ctx, err)
if code := merry.Value(err, "code"); code != nil {
if gqlError.Extensions == nil {
gqlError.Extensions = map[string]any{}
}
gqlError.Extensions["code"] = code
}
if userMessage := merry.UserMessage(err); userMessage != "" {
log.L.Warn().Err(gqlError).Msg("GraphQL exception: " + userMessage)
gqlError.Message = userMessage
} else {
if _, ok := err.(*gqlerror.Error); ok {
return gqlError
}
log.L.Error().Err(err).Send()
gqlError.Message = "error occurred"
}
return gqlError
})
return func(c *gin.Context) {
h.ServeHTTP(c.Writer, c.Request)
}
}
func publicGraphqlHandler(loaders *common.BatchLoaders) gin.HandlerFunc {
resolver := graphpublic.Resolver{
Loaders: &graphpublic.Loaders{
EpisodeLoader: loaders.EpisodeLoader,
SeasonLoader: loaders.SeasonLoader,
ShowLoader: loaders.ShowLoader,
},
}
tracer := &gqltracer.GraphTracer{}
// NewExecutableSchema and Config are in the generated.go file
// Resolver is in the resolver.go file
h := handler.NewDefaultServer(graphpublicgenerated.NewExecutableSchema(graphpublicgenerated.Config{Resolvers: &resolver}))
h.Use(tracer)
return func(c *gin.Context) {
h.ServeHTTP(c.Writer, c.Request)
}
}
func adminGraphqlHandler(config envConfig, db *sql.DB, queries *sqlc.Queries, loaders *common.BatchLoaders) gin.HandlerFunc {
resolver := graphadmin.Resolver{
DB: db,
Queries: queries,
Loaders: loaders,
}
// NewExecutableSchema and Config are in the generated.go file
// Resolver is in the resolver.go file
h := handler.NewDefaultServer(graphadmingenerated.NewExecutableSchema(graphadmingenerated.Config{Resolvers: &resolver}))
directusSecret := config.Secrets.Directus
if directusSecret == "" {
log.L.Debug().Msg("No secret for Directus found in environment. Disabling endpoint")
return func(c *gin.Context) {
c.AbortWithStatus(404)
return
}
}
return func(c *gin.Context) {
headerValue := c.GetHeader("x-api-key")
if headerValue != directusSecret {
c.AbortWithStatus(403)
return
}
h.ServeHTTP(c.Writer, c.Request)
}
}