allow to use http middlewares
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
This commit is contained in:
parent
1e4d56d059
commit
dc1e05bb18
12
http.go
12
http.go
@ -375,7 +375,17 @@ func (h *httpServer) Start() error {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
go http.Serve(ts, handler)
|
fn := handler
|
||||||
|
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
|
||||||
|
for i := len(mwf); i > 0; i-- {
|
||||||
|
fn = mwf[i-1](fn)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
go http.Serve(ts, fn)
|
||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
t := new(time.Ticker)
|
t := new(time.Ticker)
|
||||||
|
13
options.go
13
options.go
@ -1,6 +1,11 @@
|
|||||||
package http
|
package http
|
||||||
|
|
||||||
import "context"
|
import (
|
||||||
|
"context"
|
||||||
|
"net/http"
|
||||||
|
|
||||||
|
"github.com/unistack-org/micro/v3/server"
|
||||||
|
)
|
||||||
|
|
||||||
type rspCodeKey struct{}
|
type rspCodeKey struct{}
|
||||||
type rspCodeVal struct {
|
type rspCodeVal struct {
|
||||||
@ -22,3 +27,9 @@ func GetRspCode(ctx context.Context) int {
|
|||||||
}
|
}
|
||||||
return code
|
return code
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type middlewareKey struct{}
|
||||||
|
|
||||||
|
func Middleware(mw ...func(http.Handler) http.Handler) server.Option {
|
||||||
|
return server.SetOption(middlewareKey{}, mw)
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user