fixes with ErrorHandler and passed *http.Server

Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
This commit is contained in:
Василий Толстов 2021-03-09 23:54:46 +03:00
parent a46c9d395a
commit 39e6d9f586
2 changed files with 28 additions and 9 deletions

View File

@ -40,7 +40,6 @@ type httpHandler struct {
eps []*register.Endpoint eps []*register.Endpoint
hd interface{} hd interface{}
handlers map[string][]patHandler handlers map[string][]patHandler
//errorHandler func(context.Context, server.Handler, http.ResponseWriter, *http.Request, error, int)
} }
func (h *httpHandler) newCodec(ct string) (codec.Codec, error) { func (h *httpHandler) newCodec(ct string) (codec.Codec, error) {
@ -74,6 +73,7 @@ func (h *httpServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
path := r.URL.Path path := r.URL.Path
if !strings.HasPrefix(path, "/") { if !strings.HasPrefix(path, "/") {
h.errorHandler(ctx, nil, w, r, fmt.Errorf("path must contains /"), http.StatusBadRequest) h.errorHandler(ctx, nil, w, r, fmt.Errorf("path must contains /"), http.StatusBadRequest)
return
} }
ct := DefaultContentType ct := DefaultContentType
@ -84,6 +84,7 @@ func (h *httpServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
cf, err := h.newCodec(ct) cf, err := h.newCodec(ct)
if err != nil { if err != nil {
h.errorHandler(ctx, nil, w, r, err, http.StatusBadRequest) h.errorHandler(ctx, nil, w, r, err, http.StatusBadRequest)
return
} }
components := strings.Split(path[1:], "/") components := strings.Split(path[1:], "/")
@ -138,6 +139,7 @@ func (h *httpServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
umd, err := rflutil.URLMap(r.URL.RawQuery) umd, err := rflutil.URLMap(r.URL.RawQuery)
if err != nil { if err != nil {
h.errorHandler(ctx, handler, w, r, err, http.StatusBadRequest) h.errorHandler(ctx, handler, w, r, err, http.StatusBadRequest)
return
} }
for k, v := range umd { for k, v := range umd {
matches[k] = v matches[k] = v
@ -168,6 +170,7 @@ func (h *httpServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if err = cf.ReadBody(r.Body, argv.Interface()); err != nil && err != io.EOF { if err = cf.ReadBody(r.Body, argv.Interface()); err != nil && err != io.EOF {
h.errorHandler(ctx, handler, w, r, err, http.StatusInternalServerError) h.errorHandler(ctx, handler, w, r, err, http.StatusInternalServerError)
return
} }
matches = rflutil.FlattenMap(matches) matches = rflutil.FlattenMap(matches)

32
http.go
View File

@ -375,15 +375,32 @@ func (h *httpServer) Start() error {
h.Unlock() h.Unlock()
var handler http.Handler var handler http.Handler
if h.hd == nil { var srvFunc func(net.Listener) error
handler = h
} else if hdlr, ok := h.hd.Handler().(http.Handler); ok { if h.opts.Context != nil {
handler = hdlr if hs, ok := h.opts.Context.Value(serverKey{}).(*http.Server); ok && hs != nil {
if hs.Handler == nil && h.hd != nil {
if hdlr, ok := h.hd.Handler().(http.Handler); ok {
hs.Handler = hdlr
handler = hs.Handler
}
} else {
handler = hs.Handler
}
}
} }
//if !ok { if handler == nil && h.hd == nil {
// return errors.New("Server required http.Handler") handler = h
//} } else if handler == nil && h.hd != nil {
if hdlr, ok := h.hd.Handler().(http.Handler); ok {
handler = hdlr
}
}
if handler == nil {
return fmt.Errorf("cant process with nil handler")
}
if err := config.Broker.Connect(h.opts.Context); err != nil { if err := config.Broker.Connect(h.opts.Context); err != nil {
return err return err
@ -400,7 +417,6 @@ func (h *httpServer) Start() error {
} }
fn := handler fn := handler
var srvFunc func(net.Listener) error
if h.opts.Context != nil { if h.opts.Context != nil {
if mwf, ok := h.opts.Context.Value(middlewareKey{}).([]func(http.Handler) http.Handler); ok && len(mwf) > 0 { if mwf, ok := h.opts.Context.Value(middlewareKey{}).([]func(http.Handler) http.Handler); ok && len(mwf) > 0 {