/
server.go
48 lines (41 loc) · 1.26 KB
/
server.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
package runner
import (
"net/http"
"github.com/go-chi/chi"
"github.com/graph-gophers/graphql-go"
"github.com/graph-gophers/graphql-go/relay"
mw "github.com/ispec-inc/monorepo/go/pkg/middleware"
"github.com/ispec-inc/monorepo/go/pkg/presenter"
"github.com/ispec-inc/monorepo/go/svc/graphql-ddd/pkg/config"
"github.com/ispec-inc/monorepo/go/svc/graphql-ddd/pkg/handler"
"github.com/ispec-inc/monorepo/go/svc/graphql-ddd/pkg/registry"
"github.com/ispec-inc/monorepo/go/svc/graphql-ddd/pkg/schema"
)
func New() (http.Handler, func() error, error) {
s, err := schema.String()
if err != nil {
return nil, nil, err
}
rgst, cleanup, err := registry.New()
if err != nil {
return nil, cleanup, err
}
schema := graphql.MustParseSchema(s, handler.New(rgst))
h := &relay.Handler{Schema: schema}
r := chi.NewRouter()
r = mw.Common(r, mw.CommonConfig{
Timeout: config.Router.Timeout,
AllowOrigins: config.Router.AllowOrigins,
})
r.Mount("/", h)
r.Group(func(r chi.Router) {
r.Mount("/graphql", h)
})
r.Get("/schema.graphql", func(w http.ResponseWriter, r *http.Request) {
presenter.Text(w, s)
})
r.Get("/health", func(w http.ResponseWriter, r *http.Request) {
presenter.Response(w, map[string]string{"messsage": "ok"})
})
return r, cleanup, nil
}