add ability to register RPC endpoint

Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
This commit is contained in:
Василий Толстов 2021-07-14 13:43:45 +03:00
parent 26e34124e0
commit c1e0ce26b5
3 changed files with 32 additions and 0 deletions

View File

@ -123,6 +123,7 @@ func (h *httpServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
var match bool var match bool
var hldr patHandler var hldr patHandler
var handler *httpHandler var handler *httpHandler
for _, hpat := range h.handlers { for _, hpat := range h.handlers {
handlertmp := hpat.(*httpHandler) handlertmp := hpat.(*httpHandler)
for _, hldrtmp := range handlertmp.handlers[r.Method] { for _, hldrtmp := range handlertmp.handlers[r.Method] {

24
http.go
View File

@ -33,6 +33,7 @@ type httpServer struct {
pathHandlers map[*regexp.Regexp]http.HandlerFunc pathHandlers map[*regexp.Regexp]http.HandlerFunc
contentTypeHandlers map[string]http.HandlerFunc contentTypeHandlers map[string]http.HandlerFunc
opts server.Options opts server.Options
registerRPC bool
sync.RWMutex sync.RWMutex
registered bool registered bool
init bool init bool
@ -80,6 +81,11 @@ func (h *httpServer) Init(opts ...server.Option) error {
if h.contentTypeHandlers == nil { if h.contentTypeHandlers == nil {
h.contentTypeHandlers = make(map[string]http.HandlerFunc) h.contentTypeHandlers = make(map[string]http.HandlerFunc)
} }
if v, ok := h.opts.Context.Value(registerRPCHandlerKey{}).(bool); ok {
h.registerRPC = v
}
if phs, ok := h.opts.Context.Value(pathHandlerKey{}).(*pathHandlerVal); ok && phs.h != nil { if phs, ok := h.opts.Context.Value(pathHandlerKey{}).(*pathHandlerVal); ok && phs.h != nil {
for pp, ph := range phs.h { for pp, ph := range phs.h {
exp, err := regexp.Compile(pp) exp, err := regexp.Compile(pp)
@ -240,6 +246,24 @@ func (h *httpServer) NewHandler(handler interface{}, opts ...server.HandlerOptio
pth := patHandler{pat: pat, mtype: mtype, name: name, rcvr: rcvr} pth := patHandler{pat: pat, mtype: mtype, name: name, rcvr: rcvr}
hdlr.name = name hdlr.name = name
hdlr.handlers[md["Method"]] = append(hdlr.handlers[md["Method"]], pth) hdlr.handlers[md["Method"]] = append(hdlr.handlers[md["Method"]], pth)
if !h.registerRPC {
continue
}
cmp, err = rutil.Parse("/" + hn)
if err != nil && h.opts.Logger.V(logger.ErrorLevel) {
h.opts.Logger.Errorf(h.opts.Context, "parsing path pattern err: %v", err)
continue
}
tpl = cmp.Compile()
pat, err = rutil.NewPattern(tpl.Version, tpl.OpCodes, tpl.Pool, tpl.Verb)
if err != nil && h.opts.Logger.V(logger.ErrorLevel) {
h.opts.Logger.Errorf(h.opts.Context, "creating new pattern err: %v", err)
continue
}
pth = patHandler{pat: pat, mtype: mtype, name: name, rcvr: rcvr}
hdlr.handlers[http.MethodPost] = append(hdlr.handlers[http.MethodPost], pth)
} }
return hdlr return hdlr

View File

@ -110,3 +110,10 @@ func ContentTypeHandler(ct string, h http.HandlerFunc) server.Option {
o.Context = context.WithValue(o.Context, contentTypeHandlerKey{}, v) o.Context = context.WithValue(o.Context, contentTypeHandlerKey{}, v)
} }
} }
type registerRPCHandlerKey struct{}
// RegisterRPCHandler registers compatibility endpoints with /ServiceName.ServiceEndpoint method POST
func RegisterRPCHandler(b bool) server.Option {
return server.SetOption(registerRPCHandlerKey{}, b)
}