-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdestination_service.go
50 lines (40 loc) · 1.09 KB
/
destination_service.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
package destination
import (
"errors"
"fmt"
"log"
"net/http"
"os"
"github.com/99designs/gqlgen/graphql/handler"
"github.com/GoogleCloudPlatform/functions-framework-go/functions"
"github.com/erictg/roadtrips/services/destination/graph"
"github.com/erictg/roadtrips/services/destination/service"
)
func init() {
hndlr, err := NewGQLHandler()
if err != nil {
log.Fatalf("failed to int gql handler, %v", err.Error())
}
functions.HTTP("DestinationGQL", hndlr.serveGQL)
}
type GQLHandler struct {
server *handler.Server
}
func NewGQLHandler() (*GQLHandler, error) {
mapsKey := os.Getenv("MAPS_API_KEY")
if mapsKey == "" {
return nil, errors.New("maps key not specified")
}
svc, err := service.NewDestinationService(mapsKey)
if err != nil {
return nil, fmt.Errorf("failed to init destination svc, %w", err)
}
res := graph.NewResolver(svc)
return &GQLHandler{
server: handler.NewDefaultServer(graph.NewExecutableSchema(graph.Config{Resolvers: res})),
}, nil
}
func (g *GQLHandler) serveGQL(w http.ResponseWriter, r *http.Request) {
log.Println("handler")
g.server.ServeHTTP(w, r)
}