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

@ -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() {