add http middleware, add option to use custom mux, improved logger kvs

This commit is contained in:
2021-10-13 17:05:59 +02:00
parent 2916a61b3b
commit 43357bc790
9 changed files with 72 additions and 15 deletions

View File

@ -21,9 +21,9 @@ func (s *service) gateway(opts ...runtime.ServeMuxOption) error {
return err
}
if s.opts.gatewayPrefix != "" {
s.mux.Handle(s.opts.gatewayPrefix+"/", http.StripPrefix(s.opts.gatewayPrefix, mux))
s.opts.mux.Handle(s.opts.gatewayPrefix+"/", http.StripPrefix(s.opts.gatewayPrefix, mux))
} else {
s.mux.Handle("/", mux)
s.opts.mux.Handle("/", mux)
}
return nil
}

15
service/mux.go Normal file
View File

@ -0,0 +1,15 @@
package service
import (
"net/http"
"github.com/justinas/alice"
)
type ServeMux interface {
ServeHTTP(http.ResponseWriter, *http.Request)
Handle(pattern string, handler http.Handler)
HandleFunc(pattern string, handler func(http.ResponseWriter, *http.Request))
}
type Middleware = alice.Constructor

View File

@ -83,8 +83,8 @@ type Options interface {
ClientInterceptors() []grpc.UnaryClientInterceptor
StreamClientInterceptors() []grpc.StreamClientInterceptor
Cors() cors.Options
Mux() ServeMux
GRPCWeb() bool
GRPCWebPrefix() string
GRPCWebOpts() []grpcweb.Option
@ -266,6 +266,18 @@ func WithCors(opts cors.Options) Option {
}
}
func WithMux(mux ServeMux) Option {
return func(o *options) {
o.mux = mux
}
}
func WithMiddlewares(m ...Middleware) Option {
return func(o *options) {
o.middlewares = m
}
}
func WithGRPCWeb(b bool) Option {
return func(o *options) {
o.grpcWeb = b
@ -334,11 +346,13 @@ type options struct {
clientInterceptors []grpc.UnaryClientInterceptor
streamClientInterceptors []grpc.StreamClientInterceptor
mux ServeMux
middlewares []Middleware
grpcWeb bool
grpcWebOpts []grpcweb.Option
grpcWebPrefix string
gateway RegisterGatewayFunc
gatewayOpts []runtime.ServeMuxOption
gateway RegisterGatewayFunc
gatewayOpts []runtime.ServeMuxOption
cors cors.Options
error error
@ -433,6 +447,10 @@ func (o *options) Cors() cors.Options {
return o.cors
}
func (o *options) Mux() ServeMux {
return o.mux
}
func (o *options) GRPCWeb() bool {
return o.grpcWeb
}

View File

@ -18,6 +18,7 @@ import (
"github.com/google/uuid"
grpcmiddleware "github.com/grpc-ecosystem/go-grpc-middleware"
"github.com/jinzhu/gorm"
"github.com/justinas/alice"
"github.com/rs/cors"
"github.com/sirupsen/logrus"
"github.com/soheilhy/cmux"
@ -54,7 +55,6 @@ type service struct {
mu sync.Mutex
running bool
mux *http.ServeMux
// inproc Channel is used to serve grpc gateway
inproc *inprocgrpc.Channel
@ -71,7 +71,6 @@ func newService(opts ...Option) (*service, error) {
opts: parseFlags(NewOptions()),
cmd: cmd,
id: uuid.New().String(),
mux: http.NewServeMux(),
inproc: &inprocgrpc.Channel{},
}
s.mu.Lock()
@ -79,6 +78,9 @@ func newService(opts ...Option) (*service, error) {
for _, f := range opts {
f(s.opts)
}
if s.opts.mux == nil {
s.opts.mux = http.NewServeMux()
}
if s.opts.error != nil {
return nil, s.opts.error
}
@ -194,7 +196,7 @@ func (s *service) run() error {
}
}
hServer := &http.Server{
Handler: cors.New(s.opts.cors).Handler(s.mux),
Handler: alice.New(s.opts.middlewares...).Then(cors.New(s.opts.cors).Handler(s.opts.mux)),
}
if s.opts.Gateway() || s.opts.grpcWeb {
go func() {

View File

@ -26,9 +26,9 @@ func (s *service) grpcWeb(opts ...grpcweb.Option) error {
h := grpcweb.WrapServer(s.server, append(defaultWebOptions, opts...)...)
for _, v := range grpcweb.ListGRPCResources(s.server) {
if s.opts.grpcWebPrefix != "" {
s.mux.Handle(s.opts.grpcWebPrefix+v, http.StripPrefix(s.opts.grpcWebPrefix, h))
s.opts.mux.Handle(s.opts.grpcWebPrefix+v, http.StripPrefix(s.opts.grpcWebPrefix, h))
} else {
s.mux.Handle(v, h)
s.opts.mux.Handle(v, h)
}
}
return nil