-
Notifications
You must be signed in to change notification settings - Fork 130
/
main.go
116 lines (90 loc) · 2.89 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
109
110
111
112
113
114
115
116
package main
import (
"context"
"fmt"
"net/http"
"os"
"strings"
"time"
"github.com/gobwas/ws"
log "github.com/jensneuse/abstractlogger"
"go.uber.org/zap"
gatewayHttp "github.com/wundergraph/graphql-go-tools/examples/federation/gateway/http"
"github.com/wundergraph/graphql-go-tools/execution/engine"
"github.com/wundergraph/graphql-go-tools/execution/graphql"
"github.com/wundergraph/graphql-go-tools/v2/pkg/playground"
)
// It's just a simple example of graphql federation gateway server, it's NOT a production ready code.
func logger() log.Logger {
logger, err := zap.NewDevelopmentConfig().Build()
if err != nil {
panic(err)
}
return log.NewZapLogger(logger, log.DebugLevel)
}
func fallback(sc *ServiceConfig) (string, error) {
dat, err := os.ReadFile(sc.Name + "/graph/schema.graphqls")
if err != nil {
return "", err
}
return string(dat), nil
}
func startServer() {
logger := logger()
logger.Info("logger initialized")
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
upgrader := &ws.DefaultHTTPUpgrader
upgrader.Header = http.Header{}
upgrader.Header.Add("Sec-Websocket-Protocol", "graphql-ws")
graphqlEndpoint := "/query"
playgroundURLPrefix := "/playground"
playgroundURL := ""
httpClient := http.DefaultClient
mux := http.NewServeMux()
datasourceWatcher := NewDatasourcePoller(httpClient, DatasourcePollerConfig{
Services: []ServiceConfig{
{Name: "accounts", URL: "http://localhost:4001/query", Fallback: fallback},
{Name: "products", URL: "http://localhost:4002/query", WS: "ws://localhost:4002/query"},
{Name: "reviews", URL: "http://localhost:4003/query"},
},
PollingInterval: 30 * time.Second,
})
p := playground.New(playground.Config{
PathPrefix: "",
PlaygroundPath: playgroundURLPrefix,
GraphqlEndpointPath: graphqlEndpoint,
GraphQLSubscriptionEndpointPath: graphqlEndpoint,
})
handlers, err := p.Handlers()
if err != nil {
logger.Fatal("configure handlers", log.Error(err))
return
}
for i := range handlers {
mux.Handle(handlers[i].Path, handlers[i].Handler)
}
enableART := true
var gqlHandlerFactory HandlerFactoryFn = func(schema *graphql.Schema, engine *engine.ExecutionEngine) http.Handler {
return gatewayHttp.NewGraphqlHTTPHandler(schema, engine, upgrader, logger, enableART)
}
gateway := NewGateway(ctx, gqlHandlerFactory, httpClient, logger)
datasourceWatcher.Register(gateway)
go datasourceWatcher.Run(ctx)
gateway.Ready()
mux.Handle("/query", gateway)
addr := "0.0.0.0:4000"
logger.Info("Listening",
log.String("add", addr),
)
fmt.Printf("Access Playground on: http://%s%s%s\n", prettyAddr(addr), playgroundURLPrefix, playgroundURL)
logger.Fatal("failed listening",
log.Error(http.ListenAndServe(addr, mux)),
)
}
func prettyAddr(addr string) string {
return strings.Replace(addr, "0.0.0.0", "localhost", -1)
}
func main() {
startServer()
}