/
modules.go
118 lines (97 loc) · 4.16 KB
/
modules.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
package core
import (
stdContext "context"
"fmt"
"net/http"
"sync"
"github.com/pkg/errors"
"github.com/wundergraph/graphql-go-tools/v2/pkg/graphqlerrors"
"go.uber.org/zap"
)
var (
modules = make(map[string]ModuleInfo)
modulesMu sync.RWMutex
)
type ModuleID string
type ModuleInfo struct {
// Name is the name of the module
ID ModuleID
// New is the function that creates a new instance of the module
New func() Module
}
type Module interface {
Module() ModuleInfo
}
func RegisterModule(instance Module) {
mod := instance.Module()
if mod.ID == "" {
panic("module ID missing")
}
if val := mod.New(); val == nil {
panic("ModuleInfo.New must return a non-nil module instance")
}
modulesMu.Lock()
defer modulesMu.Unlock()
if _, ok := modules[string(mod.ID)]; ok {
panic(fmt.Sprintf("module already registered: %s", mod.ID))
}
modules[string(mod.ID)] = mod
}
// Module Interfaces
// RouterMiddlewareHandler allows you to add a middleware to the router.
// The middleware is called for every request. It allows you to modify the request before it is processed by the router.
// The same semantics of http.Handler apply here. Don't manipulate / consume the body of the request unless
// you know what you are doing. If you consume the body of the request it will not be available for the next handler.
type RouterMiddlewareHandler interface {
// Middleware is the middleware handler
Middleware(ctx RequestContext, next http.Handler)
}
// EnginePreOriginHandler allows you to add a handler to the router engine origin requests.
// The handler is called before the request is sent to the origin. All origin handlers are called sequentially.
// It allows you to modify the request before it is sent or return a custom response. The same semantics of http.RoundTripper apply here.
// Don't manipulate / consume the body of the request unless you know what you are doing.
// If you consume the body of the request it will not be available for the next handler.
type EnginePreOriginHandler interface {
// OnOriginRequest is called before the request is sent to the origin
// Might be called multiple times if there are multiple origins
OnOriginRequest(req *http.Request, ctx RequestContext) (*http.Request, *http.Response)
}
// EnginePostOriginHandler allows you to add a handler to the router engine origin requests.
// The handler is called after the response was received from the origin. All origin handlers are called sequentially.
// It allows you to return a custom response to the client. If your return nil as response, the next handler is called.
// The same semantics of http.RoundTripper apply here. In order to modify the response, you have to return a new response.
type EnginePostOriginHandler interface {
// OnOriginResponse is called after the request is sent to the origin.
// Might be called multiple times if there are multiple origins
OnOriginResponse(resp *http.Response, ctx RequestContext) *http.Response
}
// Provisioner is called before the server starts
// It allows you to initialize your module e.g. create a database connection
// or load a configuration file
type Provisioner interface {
// Provision is called before the server starts
Provision(*ModuleContext) error
}
type Cleaner interface {
// Cleanup is called after the server stops
Cleanup() error
}
// ModuleContext is a type which defines the lifetime of modules that are registered with the router.
type ModuleContext struct {
stdContext.Context
Module Module
Logger *zap.Logger
}
// WriteResponseError writes the given error as a GraphQL error response to the http.ResponseWriter
// associated with the given RequestContext. If err is nil, a generic "Internal Error" error is returned.
// Please never write errors directly to the http.ResponseWriter. The function takes care of logging and tracking
// the error in the underlying telemetry system.
func WriteResponseError(ctx RequestContext, err error) {
var errs graphqlerrors.RequestErrors
if err != nil {
errs = graphqlerrors.RequestErrorsFromError(err)
} else {
errs = graphqlerrors.RequestErrorsFromError(errors.New("Internal Error"))
}
writeRequestErrors(ctx.Request(), ctx.ResponseWriter(), http.StatusInternalServerError, errs, ctx.Logger())
}