-
Notifications
You must be signed in to change notification settings - Fork 0
/
gateway.go
151 lines (125 loc) · 4 KB
/
gateway.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
package gateway
import (
"context"
"errors"
"net/http"
"os"
"strings"
"github.com/easeq/go-redis-access-control/gateway"
"github.com/easeq/go-service/component"
"github.com/easeq/go-service/logger"
"github.com/easeq/go-service/registry"
"github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
)
var (
// ErrGatewayConfigLoad returned when env config for gRPC-gateway results in an error
ErrGatewayConfigLoad = errors.New("error loading gateway config")
// ErrNotDefinedHTTPServiceHandlerRegistrar thrown when http service registration handler is not provided
ErrNotDefinedHTTPServiceHandlerRegistrar = errors.New("http service handler registration callback not provided")
// ErrHTTPServiceHandlerRegFailed returned when any HTTP service handler registration fails
ErrHTTPServiceHandlerRegFailed = errors.New("http service handler registration failed")
// ErrCannotAddMuxOptionAtPos returned when adding new mux option at the specified position is not possible
ErrCannotAddMuxOptionAtPos = errors.New("cannot add mux option at the position specified")
)
const (
// SERVER_TYPE is the type of the server.
SERVER_TYPE = "gateway"
)
// Option to pass as arg while creating new service
type Option func(*Gateway)
// Middleware type
type Middleware func(http.Handler) http.Handler
// HTTPServiceHandlerRegistrar HTTP service handler registration func
type HTTPServiceHandlerRegistrar func(context.Context, *Gateway) error
// Gateway handle gRPC gateway
type Gateway struct {
i component.Initializer
logger logger.Logger
Mux *runtime.ServeMux
Middleware []Middleware
HTTPServiceHandlerRegistrar HTTPServiceHandlerRegistrar
MuxOptions []runtime.ServeMuxOption
Server *http.Server
exit chan os.Signal
*Config
}
// NewGateway creates and returns gRPC-gateway
func NewGateway(opts ...Option) *Gateway {
g := &Gateway{
Middleware: []Middleware{gateway.Middleware},
MuxOptions: []runtime.ServeMuxOption{},
Config: NewConfig(),
exit: make(chan os.Signal),
}
for _, opt := range opts {
opt(g)
}
g.Mux = runtime.NewServeMux(g.MuxOptions...)
g.Server = &http.Server{
Addr: g.Address(),
Handler: chainMiddleware(g.Mux, g.Middleware...),
}
g.i = NewInitializer(g)
return g
}
func chainMiddleware(f http.Handler, m ...Middleware) http.Handler {
if len(m) == 0 {
return f
}
return m[0](chainMiddleware(f, m[1:cap(m)]...))
}
// WithMuxOptions adds mux options
func WithMuxOptions(opts ...runtime.ServeMuxOption) Option {
return func(g *Gateway) {
g.MuxOptions = opts
}
}
// WithMiddleware adds middleware to the rest handler
func WithMiddleware(middleware ...Middleware) Option {
return func(g *Gateway) {
g.Middleware = middleware
}
}
// WithHTTPServiceHandlerRegistrar add HTTP service handle registration callback
func WithHTTPServiceHandlerRegistrar(registrar HTTPServiceHandlerRegistrar) Option {
return func(g *Gateway) {
g.HTTPServiceHandlerRegistrar = registrar
}
}
// Address returns the server address
func (g *Gateway) Address() string {
return g.Config.Address()
}
// Host returns gateway server hostname
func (g *Gateway) Host() string {
return g.Config.Host
}
// Port returns gateway server port
func (g *Gateway) Port() int {
return g.Config.Port
}
// RegistryTags returns gateway server registry tags
func (g *Gateway) RegistryTags() []string {
return g.Config.GetTags()
}
// GetMetadata returns the metadata by key
func (g *Gateway) GetMetadata(key string) interface{} {
return g.Metadata.Get(key)
}
// String - Returns the type of the server
func (g *Gateway) String() string {
return SERVER_TYPE
}
// AddRegistryTags - sets the registry tags for the server
func (g *Gateway) AddRegistryTags(tags ...string) {
g.Config.Tags = strings.Join(
append(g.Config.GetTags(), tags...),
registry.TAGS_SEPARATOR,
)
}
func (g *Gateway) HasInitializer() bool {
return true
}
func (g *Gateway) Initializer() component.Initializer {
return g.i
}