From e9a396d424f5d5e0eff2e24e30e866fbf090aa3c Mon Sep 17 00:00:00 2001 From: Vasiliy Tolstov Date: Sun, 30 Jan 2022 20:43:27 +0300 Subject: [PATCH] fixup old rpc http server Signed-off-by: Vasiliy Tolstov --- handler.go | 22 +++++++++++++++++++++- http.go | 3 ++- 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/handler.go b/handler.go index d697a92..cdda21e 100644 --- a/handler.go +++ b/handler.go @@ -97,7 +97,6 @@ func (h *httpServer) ServeHTTP(w http.ResponseWriter, r *http.Request) { h.errorHandler(ctx, nil, w, r, fmt.Errorf("path must starts with /"), http.StatusBadRequest) return } - cf, err := h.newCodec(ct) if err != nil { h.errorHandler(ctx, nil, w, r, err, http.StatusBadRequest) @@ -124,6 +123,27 @@ func (h *httpServer) ServeHTTP(w http.ResponseWriter, r *http.Request) { } } + if !match && h.registerRPC { + microMethod, mok := md.Get(metadata.HeaderEndpoint) + if mok { + serviceMethod := strings.Split(microMethod, ".") + if len(serviceMethod) == 2 { + if shdlr, ok := h.handlers[serviceMethod[0]]; ok { + hdlr := shdlr.(*httpHandler) + fh, mp, ok := hdlr.handlers.Search(http.MethodPost, "/"+microMethod) + if ok { + match = true + for k, v := range mp { + matches[k] = v + } + hldr = fh.(*patHandler) + handler = hdlr + } + } + } + } + } + if !match && h.hd != nil { if hdlr, ok := h.hd.Handler().(http.Handler); ok { hdlr.ServeHTTP(w, r) diff --git a/http.go b/http.go index 86a723c..cf807ce 100644 --- a/http.go +++ b/http.go @@ -221,8 +221,9 @@ func (h *httpServer) NewHandler(handler interface{}, opts ...server.HandlerOptio } if h.registerRPC { + h.opts.Logger.Infof(h.opts.Context, "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 handler for %s %s", md["Method"], md["Path"]) + h.opts.Logger.Errorf(h.opts.Context, "cant add rpc handler for http.MethodPost %s /%s", hn, hn) } } } -- 2.45.2