Merge pull request 'issue_179' (#180) from devstigneev/micro-server-http:issue_179 into master
Reviewed-on: #180 Reviewed-by: Василий Толстов <v.tolstov@unistack.org>
This commit is contained in:
commit
28eeca30c0
24
.gitignore
vendored
Normal file
24
.gitignore
vendored
Normal file
@ -0,0 +1,24 @@
|
||||
# Binaries for programs and plugins
|
||||
*.exe
|
||||
*.exe~
|
||||
*.dll
|
||||
*.so
|
||||
*.dylib
|
||||
bin
|
||||
|
||||
# Test binary, built with `go test -c`
|
||||
*.test
|
||||
|
||||
# Output of the go coverage tool, specifically when used with LiteIDE
|
||||
*.out
|
||||
|
||||
# Dependency directories (remove the comment below to include it)
|
||||
# vendor/
|
||||
|
||||
# Go workspace file
|
||||
go.work
|
||||
|
||||
# General
|
||||
.DS_Store
|
||||
.idea
|
||||
.vscode
|
6
go.mod
6
go.mod
@ -5,9 +5,9 @@ go 1.19
|
||||
require (
|
||||
go.unistack.org/micro-codec-yaml/v4 v4.0.0
|
||||
go.unistack.org/micro-proto/v4 v4.0.1
|
||||
go.unistack.org/micro/v4 v4.0.6
|
||||
go.unistack.org/micro/v4 v4.0.17
|
||||
go.unistack.org/protoc-gen-go-micro/v4 v4.0.7
|
||||
golang.org/x/net v0.12.0
|
||||
golang.org/x/net v0.15.0
|
||||
)
|
||||
|
||||
require (
|
||||
@ -15,7 +15,7 @@ require (
|
||||
github.com/golang/protobuf v1.5.3 // indirect
|
||||
github.com/google/gnostic v0.6.9 // indirect
|
||||
golang.org/x/mod v0.12.0 // indirect
|
||||
golang.org/x/sys v0.10.0 // indirect
|
||||
golang.org/x/sys v0.12.0 // indirect
|
||||
golang.org/x/tools v0.11.0 // indirect
|
||||
google.golang.org/protobuf v1.31.0 // indirect
|
||||
gopkg.in/yaml.v2 v2.4.0 // indirect
|
||||
|
12
go.sum
12
go.sum
@ -73,8 +73,8 @@ go.unistack.org/micro-codec-yaml/v4 v4.0.0 h1:DK/+fA69gmnvYmGHe3kqVR0pukS8ank5lP
|
||||
go.unistack.org/micro-codec-yaml/v4 v4.0.0/go.mod h1:UG4ouILbUfgNL7nvp7CpmD2IIzO8bQHjzDIahOvIAgM=
|
||||
go.unistack.org/micro-proto/v4 v4.0.1 h1:2RKHgtCOOcAFgKsnngGK5bqM/6MWXOjVCdw03dbuoF8=
|
||||
go.unistack.org/micro-proto/v4 v4.0.1/go.mod h1:ArmK7o+uFvxSY3dbJhKBBX4Pm1rhWdLEFf3LxBrMtec=
|
||||
go.unistack.org/micro/v4 v4.0.6 h1:YFWvTh3VwyOd6NHYTQcf47n2TF5+p/EhpnbuBQX3qhk=
|
||||
go.unistack.org/micro/v4 v4.0.6/go.mod h1:bVEYTlPi0EsdgZZt311bIroDg9ict7ky3C87dSCCAGk=
|
||||
go.unistack.org/micro/v4 v4.0.17 h1:mF7uM+J4ILdG+1fcwzKYCwDlxhdbF/e1WnGzKKLnIXc=
|
||||
go.unistack.org/micro/v4 v4.0.17/go.mod h1:ZDgU9931vm2l7X6RN/6UuwRIVp24GRdmQ7dKmegArk4=
|
||||
go.unistack.org/protoc-gen-go-micro/v4 v4.0.7 h1:c3CeBvjWV9MBrGGPaQCcHZrnuLY9sVJHv7vACI/dvuM=
|
||||
go.unistack.org/protoc-gen-go-micro/v4 v4.0.7/go.mod h1:9bsKAlESlPXPBSmY/NDLL//smZbhnEMrnWyG+M8zVFA=
|
||||
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
|
||||
@ -94,8 +94,8 @@ golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn
|
||||
golang.org/x/net v0.0.0-20200822124328-c89045814202/go.mod h1:/O7V0waA8r7cgGh81Ro3o1hOxt32SMVPicZroKQ2sZA=
|
||||
golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4/go.mod h1:p54w0d4576C0XHj96bSt6lcn1PtDYWL6XObtHCRCNQM=
|
||||
golang.org/x/net v0.0.0-20210805182204-aaa1db679c0d/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
|
||||
golang.org/x/net v0.12.0 h1:cfawfvKITfUsFCeJIHJrbSxpeu/E81khclypR0GVT50=
|
||||
golang.org/x/net v0.12.0/go.mod h1:zEVYFnQC7m/vmpQFELhcD1EWkZlX69l4oqgmer6hfKA=
|
||||
golang.org/x/net v0.15.0 h1:ugBLEUaxABaB5AJqW9enI0ACdci2RUd4eP51NTBvuJ8=
|
||||
golang.org/x/net v0.15.0/go.mod h1:idbUs1IY1+zTqbi8yxTbhexhEEk5ur9LInksu6HrEpk=
|
||||
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
|
||||
golang.org/x/oauth2 v0.0.0-20200107190931-bf48bf16ab8d/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
|
||||
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
@ -111,8 +111,8 @@ golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7w
|
||||
golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20210510120138-977fb7262007/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.10.0 h1:SqMFp9UcQJZa+pmYuAKjd9xq1f0j5rLcDIk0mj4qAsA=
|
||||
golang.org/x/sys v0.10.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.12.0 h1:CM0HF96J0hcLAwsHPJZjfdNzs0gftsLfgKt57wWHJ0o=
|
||||
golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
|
||||
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
||||
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
|
||||
|
10
handler.go
10
handler.go
@ -22,7 +22,7 @@ var (
|
||||
DefaultErrorHandler = func(ctx context.Context, s interface{}, w http.ResponseWriter, r *http.Request, err error, status int) {
|
||||
w.WriteHeader(status)
|
||||
if _, cerr := w.Write([]byte(err.Error())); cerr != nil {
|
||||
logger.DefaultLogger.Errorf(ctx, "write failed: %v", cerr)
|
||||
logger.DefaultLogger.Error(ctx, fmt.Sprintf("write failed: %v", cerr))
|
||||
}
|
||||
}
|
||||
DefaultContentType = "application/json"
|
||||
@ -318,7 +318,7 @@ func (h *Server) HTTPHandlerFunc(handler interface{}) (http.HandlerFunc, error)
|
||||
}
|
||||
|
||||
if err != nil && handler.sopts.Logger.V(logger.ErrorLevel) {
|
||||
handler.sopts.Logger.Errorf(handler.sopts.Context, "handler err: %v", err)
|
||||
handler.sopts.Logger.Error(handler.sopts.Context, fmt.Sprintf("handler err: %v", err))
|
||||
return
|
||||
}
|
||||
|
||||
@ -328,7 +328,7 @@ func (h *Server) HTTPHandlerFunc(handler interface{}) (http.HandlerFunc, error)
|
||||
w.WriteHeader(scode)
|
||||
|
||||
if _, cerr := w.Write(buf); cerr != nil {
|
||||
handler.sopts.Logger.Errorf(ctx, "write failed: %v", cerr)
|
||||
handler.sopts.Logger.Error(ctx, fmt.Sprintf("write failed: %v", cerr))
|
||||
}
|
||||
}, nil
|
||||
}
|
||||
@ -581,7 +581,7 @@ func (h *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
if err != nil && handler.sopts.Logger.V(logger.ErrorLevel) {
|
||||
handler.sopts.Logger.Errorf(handler.sopts.Context, "handler err: %v", err)
|
||||
handler.sopts.Logger.Error(handler.sopts.Context, fmt.Sprintf("handler err: %v", err))
|
||||
return
|
||||
}
|
||||
|
||||
@ -591,6 +591,6 @@ func (h *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
w.WriteHeader(scode)
|
||||
|
||||
if _, cerr := w.Write(buf); cerr != nil {
|
||||
handler.sopts.Logger.Errorf(ctx, "write failed: %v", cerr)
|
||||
handler.sopts.Logger.Error(ctx, fmt.Sprintf("write failed: %v", cerr))
|
||||
}
|
||||
}
|
||||
|
80
http.go
80
http.go
@ -198,16 +198,16 @@ func (h *Server) newHTTPHandler(handler interface{}, opts ...options.Option) *ht
|
||||
}
|
||||
|
||||
if method.Name == "" && h.opts.Logger.V(logger.ErrorLevel) {
|
||||
h.opts.Logger.Errorf(h.opts.Context, "nil method for %s", mname)
|
||||
h.opts.Logger.Error(h.opts.Context, fmt.Sprintf("nil method for %s", mname))
|
||||
continue
|
||||
}
|
||||
|
||||
mtype, err := prepareEndpoint(method)
|
||||
if err != nil && h.opts.Logger.V(logger.ErrorLevel) {
|
||||
h.opts.Logger.Errorf(h.opts.Context, "%v", err)
|
||||
h.opts.Logger.Error(h.opts.Context, fmt.Sprintf("%v", err))
|
||||
continue
|
||||
} else if mtype == nil {
|
||||
h.opts.Logger.Errorf(h.opts.Context, "nil mtype for %s", mname)
|
||||
h.opts.Logger.Error(h.opts.Context, fmt.Sprintf("nil mtype for %s", mname))
|
||||
continue
|
||||
}
|
||||
|
||||
@ -218,13 +218,13 @@ func (h *Server) newHTTPHandler(handler interface{}, opts ...options.Option) *ht
|
||||
hdlr.name = name
|
||||
|
||||
if err := hdlr.handlers.Insert([]string{md["Method"]}, md["Path"], pth); err != nil {
|
||||
h.opts.Logger.Errorf(h.opts.Context, "cant add handler for %s %s", md["Method"], md["Path"])
|
||||
h.opts.Logger.Error(h.opts.Context, fmt.Sprintf("cant add handler for %s %s", md["Method"], md["Path"]))
|
||||
}
|
||||
|
||||
if h.registerRPC {
|
||||
h.opts.Logger.Infof(h.opts.Context, "register rpc handler for http.MethodPost %s /%s", hn, hn)
|
||||
h.opts.Logger.Info(h.opts.Context, fmt.Sprintf("register rpc handler for http.MethodPost %s /%s", hn, hn))
|
||||
if err := hdlr.handlers.Insert([]string{http.MethodPost}, "/"+hn, pth); err != nil {
|
||||
h.opts.Logger.Errorf(h.opts.Context, "cant add rpc handler for http.MethodPost %s /%s", hn, hn)
|
||||
h.opts.Logger.Error(h.opts.Context, fmt.Sprintf("cant add rpc handler for http.MethodPost %s /%s", hn, hn))
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -248,16 +248,16 @@ func (h *Server) newHTTPHandler(handler interface{}, opts ...options.Option) *ht
|
||||
}
|
||||
|
||||
if method.Name == "" && h.opts.Logger.V(logger.ErrorLevel) {
|
||||
h.opts.Logger.Errorf(h.opts.Context, "nil method for %s", mname)
|
||||
h.opts.Logger.Error(h.opts.Context, fmt.Sprintf("nil method for %s", mname))
|
||||
continue
|
||||
}
|
||||
|
||||
mtype, err := prepareEndpoint(method)
|
||||
if err != nil && h.opts.Logger.V(logger.ErrorLevel) {
|
||||
h.opts.Logger.Errorf(h.opts.Context, "%v", err)
|
||||
h.opts.Logger.Error(h.opts.Context, fmt.Sprintf("%v", err))
|
||||
continue
|
||||
} else if mtype == nil {
|
||||
h.opts.Logger.Errorf(h.opts.Context, "nil mtype for %s", mname)
|
||||
h.opts.Logger.Error(h.opts.Context, fmt.Sprintf("nil mtype for %s", mname))
|
||||
continue
|
||||
}
|
||||
|
||||
@ -268,13 +268,13 @@ func (h *Server) newHTTPHandler(handler interface{}, opts ...options.Option) *ht
|
||||
hdlr.name = name
|
||||
|
||||
if err := hdlr.handlers.Insert([]string{md.Method}, md.Path, pth); err != nil {
|
||||
h.opts.Logger.Errorf(h.opts.Context, "cant add handler for %s %s", md.Method, md.Path)
|
||||
h.opts.Logger.Error(h.opts.Context, fmt.Sprintf("cant add handler for %s %s", md.Method, md.Path))
|
||||
}
|
||||
|
||||
if h.registerRPC {
|
||||
h.opts.Logger.Infof(h.opts.Context, "register rpc handler for http.MethodPost %s /%s", hn, hn)
|
||||
h.opts.Logger.Info(h.opts.Context, fmt.Sprintf("register rpc handler for http.MethodPost %s /%s", hn, hn))
|
||||
if err := hdlr.handlers.Insert([]string{http.MethodPost}, "/"+hn, pth); err != nil {
|
||||
h.opts.Logger.Errorf(h.opts.Context, "cant add rpc handler for http.MethodPost %s /%s", hn, hn)
|
||||
h.opts.Logger.Error(h.opts.Context, fmt.Sprintf("cant add rpc handler for http.MethodPost %s /%s", hn, hn))
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -317,7 +317,7 @@ func (h *Server) Register() error {
|
||||
|
||||
if !registered {
|
||||
if config.Logger.V(logger.InfoLevel) {
|
||||
config.Logger.Infof(config.Context, "Register [%s] Registering node: %s", config.Register.String(), service.Nodes[0].ID)
|
||||
config.Logger.Info(config.Context, fmt.Sprintf("Register [%s] Registering node: %s", config.Register.String(), service.Nodes[0].ID))
|
||||
}
|
||||
}
|
||||
|
||||
@ -350,7 +350,7 @@ func (h *Server) Deregister() error {
|
||||
}
|
||||
|
||||
if config.Logger.V(logger.InfoLevel) {
|
||||
config.Logger.Infof(config.Context, "Deregistering node: %s", service.Nodes[0].ID)
|
||||
config.Logger.Info(config.Context, fmt.Sprintf("Deregistering node: %s", service.Nodes[0].ID))
|
||||
}
|
||||
|
||||
if err := server.DefaultDeregisterFunc(service, config); err != nil {
|
||||
@ -400,7 +400,7 @@ func (h *Server) Start() error {
|
||||
}
|
||||
|
||||
if config.Logger.V(logger.InfoLevel) {
|
||||
config.Logger.Infof(config.Context, "Listening on %s", ts.Addr().String())
|
||||
config.Logger.Info(config.Context, fmt.Sprintf("Listening on %s", ts.Addr().String()))
|
||||
}
|
||||
|
||||
h.Lock()
|
||||
@ -408,7 +408,6 @@ func (h *Server) Start() error {
|
||||
h.Unlock()
|
||||
|
||||
var handler http.Handler
|
||||
var srvFunc func(net.Listener) error
|
||||
|
||||
// nolint: nestif
|
||||
if h.opts.Context != nil {
|
||||
@ -441,7 +440,7 @@ func (h *Server) Start() error {
|
||||
|
||||
if err := config.RegisterCheck(h.opts.Context); err != nil {
|
||||
if config.Logger.V(logger.ErrorLevel) {
|
||||
config.Logger.Errorf(config.Context, "Server %s-%s register check error: %s", config.Name, config.ID, err)
|
||||
config.Logger.Error(config.Context, fmt.Sprintf("Server %s-%s register check error: %s", config.Name, config.ID, err))
|
||||
}
|
||||
} else {
|
||||
if err = h.Register(); err != nil {
|
||||
@ -451,6 +450,7 @@ func (h *Server) Start() error {
|
||||
|
||||
fn := handler
|
||||
|
||||
var hs *http.Server
|
||||
if h.opts.Context != nil {
|
||||
if mwf, ok := h.opts.Context.Value(middlewareKey{}).([]func(http.Handler) http.Handler); ok && len(mwf) > 0 {
|
||||
// wrap the handler func
|
||||
@ -458,25 +458,19 @@ func (h *Server) Start() error {
|
||||
fn = mwf[i-1](fn)
|
||||
}
|
||||
}
|
||||
if hs, ok := h.opts.Context.Value(serverKey{}).(*http.Server); ok && hs != nil {
|
||||
var ok bool
|
||||
if hs, ok = h.opts.Context.Value(serverKey{}).(*http.Server); ok && hs != nil {
|
||||
hs.Handler = fn
|
||||
srvFunc = hs.Serve
|
||||
} else {
|
||||
hs = &http.Server{Handler: fn}
|
||||
}
|
||||
}
|
||||
|
||||
if srvFunc != nil {
|
||||
go func() {
|
||||
if cerr := srvFunc(ts); cerr != nil && !errors.Is(cerr, net.ErrClosed) {
|
||||
h.opts.Logger.Error(h.opts.Context, cerr)
|
||||
}
|
||||
}()
|
||||
} else {
|
||||
go func() {
|
||||
if cerr := http.Serve(ts, fn); cerr != nil && !errors.Is(cerr, net.ErrClosed) {
|
||||
h.opts.Logger.Error(h.opts.Context, cerr)
|
||||
}
|
||||
}()
|
||||
}
|
||||
go func() {
|
||||
if cerr := hs.Serve(ts); cerr != nil && !errors.Is(cerr, net.ErrClosed) {
|
||||
h.opts.Logger.Error(h.opts.Context, fmt.Sprint(cerr))
|
||||
}
|
||||
}()
|
||||
|
||||
go func() {
|
||||
t := new(time.Ticker)
|
||||
@ -502,28 +496,28 @@ func (h *Server) Start() error {
|
||||
// nolint: nestif
|
||||
if rerr != nil && registered {
|
||||
if config.Logger.V(logger.ErrorLevel) {
|
||||
config.Logger.Errorf(config.Context, "Server %s-%s register check error: %s, deregister it", config.Name, config.ID, rerr)
|
||||
config.Logger.Error(config.Context, fmt.Sprintf("Server %s-%s register check error: %s, deregister it", config.Name, config.ID, rerr))
|
||||
}
|
||||
// deregister self in case of error
|
||||
if err := h.Deregister(); err != nil {
|
||||
if config.Logger.V(logger.ErrorLevel) {
|
||||
config.Logger.Errorf(config.Context, "Server %s-%s deregister error: %s", config.Name, config.ID, err)
|
||||
config.Logger.Error(config.Context, fmt.Sprintf("Server %s-%s deregister error: %s", config.Name, config.ID, err))
|
||||
}
|
||||
}
|
||||
} else if rerr != nil && !registered {
|
||||
if config.Logger.V(logger.ErrorLevel) {
|
||||
config.Logger.Errorf(config.Context, "Server %s-%s register check error: %s", config.Name, config.ID, rerr)
|
||||
config.Logger.Error(config.Context, fmt.Sprintf("Server %s-%s register check error: %s", config.Name, config.ID, rerr))
|
||||
}
|
||||
continue
|
||||
}
|
||||
if err := h.Register(); err != nil {
|
||||
if config.Logger.V(logger.ErrorLevel) {
|
||||
config.Logger.Errorf(config.Context, "Server %s-%s register error: %s", config.Name, config.ID, err)
|
||||
config.Logger.Error(config.Context, fmt.Sprintf("Server %s-%s register error: %s", config.Name, config.ID, err))
|
||||
}
|
||||
}
|
||||
|
||||
if err := h.Register(); err != nil {
|
||||
config.Logger.Errorf(config.Context, "Server register error: %s", err)
|
||||
config.Logger.Error(config.Context, fmt.Sprintf("Server register error: %s", err))
|
||||
}
|
||||
// wait for exit
|
||||
case ch = <-h.exit:
|
||||
@ -533,10 +527,18 @@ func (h *Server) Start() error {
|
||||
|
||||
// deregister
|
||||
if err := h.Deregister(); err != nil {
|
||||
config.Logger.Errorf(config.Context, "Server deregister error: %s", err)
|
||||
config.Logger.Error(config.Context, fmt.Sprintf("Server deregister error: %s", err))
|
||||
}
|
||||
|
||||
ch <- ts.Close()
|
||||
ctx, cancel := context.WithTimeout(context.Background(), h.opts.GracefulTimeout)
|
||||
defer cancel()
|
||||
|
||||
err := hs.Shutdown(ctx)
|
||||
if err != nil {
|
||||
err = hs.Close()
|
||||
}
|
||||
|
||||
ch <- err
|
||||
}()
|
||||
|
||||
return nil
|
||||
|
Loading…
Reference in New Issue
Block a user