From bb763c71b5f37ddb3dc5c4157f325e313385da16 Mon Sep 17 00:00:00 2001 From: Vasiliy Tolstov Date: Tue, 13 Apr 2021 23:42:33 +0300 Subject: [PATCH] add content type handlers Signed-off-by: Vasiliy Tolstov --- .github/renovate.json | 1 + go.sum | 44 ------------------------------------------- handler.go | 15 ++++++++++----- http.go | 18 ++++++++++++++---- options.go | 20 ++++++++++++++++++++ 5 files changed, 45 insertions(+), 53 deletions(-) diff --git a/.github/renovate.json b/.github/renovate.json index 52d2918..c597bf3 100644 --- a/.github/renovate.json +++ b/.github/renovate.json @@ -2,6 +2,7 @@ "extends": [ "config:base" ], + "postUpdateOptions": ["gomodTidy"], "packageRules": [ { "matchUpdateTypes": ["minor", "patch", "pin", "digest"], diff --git a/go.sum b/go.sum index 030b0ed..bbf6bfb 100644 --- a/go.sum +++ b/go.sum @@ -6,56 +6,12 @@ github.com/imdario/mergo v0.3.12/go.mod h1:jmQim1M+e3UYxmgPu/WyfjB3N3VflVyUjjjwH github.com/patrickmn/go-cache v2.1.0+incompatible h1:HRMgzkcYKYpi3C8ajMPV8OFXaaRUnok+kx1WdO15EQc= github.com/patrickmn/go-cache v2.1.0+incompatible/go.mod h1:3Qf8kWWT7OJRJbdiICTKqZju1ZixQ/KpMGzzAfe6+WQ= github.com/silas/dag v0.0.0-20210121180416-41cf55125c34/go.mod h1:7RTUFBdIRC9nZ7/3RyRNH1bdqIShrDejd1YbLwgPS+I= -github.com/unistack-org/micro/v3 v3.3.0 h1:pEj/8QVFzMlNMEL//q/Te8qgG+XI6LTYIQrb6hMymgk= -github.com/unistack-org/micro/v3 v3.3.0/go.mod h1:iJwCWq2PECMxigfqe6TPC5GLWvj6P94Kk+PTVZGL3w8= -github.com/unistack-org/micro/v3 v3.3.1 h1:2KUgb5THy5GUjYgMcOf3TXYIJ0h2RbJHb2j6eeWdRx0= -github.com/unistack-org/micro/v3 v3.3.1/go.mod h1:iJwCWq2PECMxigfqe6TPC5GLWvj6P94Kk+PTVZGL3w8= -github.com/unistack-org/micro/v3 v3.3.2 h1:1r7fmaobJVZBKDhIewSBy8R4H/6YazQFetsDgULV6Mw= -github.com/unistack-org/micro/v3 v3.3.2/go.mod h1:tX95c0Qx4w6oqU7qKThs9lya9P507BdZ29MsTVDmU6w= -github.com/unistack-org/micro/v3 v3.3.4 h1:wSYjL1sfrqtMmgFfYKylrKpAepMHrPcOpe10hvQAKuo= -github.com/unistack-org/micro/v3 v3.3.4/go.mod h1:tX95c0Qx4w6oqU7qKThs9lya9P507BdZ29MsTVDmU6w= -github.com/unistack-org/micro/v3 v3.3.7 h1:AKQEevrlT2aqHYRFGd6BEFNnurHLvJyqvWvLCFP7iX4= -github.com/unistack-org/micro/v3 v3.3.7/go.mod h1:M3RGgKZX1OlTn0QB+3R1tlSoGwmEg6rb/Isr0lLYmjQ= -github.com/unistack-org/micro/v3 v3.3.8 h1:4e4gWuZI/cRUGZ6ZZpdrIDOF2MLO6cjHxeFfZzy610Y= -github.com/unistack-org/micro/v3 v3.3.8/go.mod h1:M3RGgKZX1OlTn0QB+3R1tlSoGwmEg6rb/Isr0lLYmjQ= -github.com/unistack-org/micro/v3 v3.3.9 h1:NFF4wymDDxgYeZU++04BxvETrrAnd0iNzVyfm/ssQI4= -github.com/unistack-org/micro/v3 v3.3.9/go.mod h1:M3RGgKZX1OlTn0QB+3R1tlSoGwmEg6rb/Isr0lLYmjQ= -github.com/unistack-org/micro/v3 v3.3.10 h1:yMSiyplupFQ7xy/kNXL/zqSnqGSjrlNa2GTCwea5BVg= -github.com/unistack-org/micro/v3 v3.3.10/go.mod h1:5ragE2E8ER5d4FZQJG9pB6qdfOoLXLfKW89l77Dy3jQ= -github.com/unistack-org/micro/v3 v3.3.11 h1:Jr0gAw5lLqgddiHKQeWUOUeP6ZqgRhz52EA9zJ5MJ3U= -github.com/unistack-org/micro/v3 v3.3.11/go.mod h1:PPCt675o3HPcODFbJ4iRWPmQFAk1WQ+asQSOb/syq6U= github.com/unistack-org/micro/v3 v3.3.13 h1:y4bDDkbwnjgOckrhFkC6D/o42tr75X33UbrB+Ko0M68= github.com/unistack-org/micro/v3 v3.3.13/go.mod h1:98hNcMXp/WyWJwLwCuwrhN1Jm7aCWaRNsMfRjK8Fq+Y= -golang.org/x/net v0.0.0-20210316092652-d523dce5a7f4/go.mod h1:RBQZq4jEuRlivfhVLdyRGr576XBO4/greRjx4P4O3yc= -golang.org/x/net v0.0.0-20210323141857-08027d57d8cf h1:sewfyKLWuY3ko6EI4hbFziQ8bHkfammpzCDfLT92I1c= -golang.org/x/net v0.0.0-20210323141857-08027d57d8cf/go.mod h1:RBQZq4jEuRlivfhVLdyRGr576XBO4/greRjx4P4O3yc= -golang.org/x/net v0.0.0-20210324051636-2c4c8ecb7826 h1:lNRDRnwZWawoPHDS50ebYHTOHjctRMLSrUSQFcAHiW4= -golang.org/x/net v0.0.0-20210324051636-2c4c8ecb7826/go.mod h1:RBQZq4jEuRlivfhVLdyRGr576XBO4/greRjx4P4O3yc= -golang.org/x/net v0.0.0-20210324205630-d1beb07c2056 h1:sANdAef76Ioam9aQUUdcAqricwY/WUaMc4+7LY4eGg8= -golang.org/x/net v0.0.0-20210324205630-d1beb07c2056/go.mod h1:uSPa2vr4CLtc/ILN5odXGNXS6mhrKVzTaCXzk9m6W3k= -golang.org/x/net v0.0.0-20210326060303-6b1517762897 h1:KrsHThm5nFk34YtATK1LsThyGhGbGe1olrte/HInHvs= -golang.org/x/net v0.0.0-20210326060303-6b1517762897/go.mod h1:uSPa2vr4CLtc/ILN5odXGNXS6mhrKVzTaCXzk9m6W3k= -golang.org/x/net v0.0.0-20210326220855-61e056675ecf h1:WUcCxqQqDT0aXO4VnQbfMvp4zh7m1Gb2clVuHUAGGRE= -golang.org/x/net v0.0.0-20210326220855-61e056675ecf/go.mod h1:uSPa2vr4CLtc/ILN5odXGNXS6mhrKVzTaCXzk9m6W3k= -golang.org/x/net v0.0.0-20210329181859-df645c7b52b1 h1:GDp1VG8WvY8lq4ic4L7GzKC3fNRlOa3UqUEpib8aBQE= -golang.org/x/net v0.0.0-20210329181859-df645c7b52b1/go.mod h1:uSPa2vr4CLtc/ILN5odXGNXS6mhrKVzTaCXzk9m6W3k= -golang.org/x/net v0.0.0-20210330075724-22f4162a9025 h1:WkEv4x2mvGbnkuK9cWl/Ru2pzfVxAgpSDLT/holPSug= -golang.org/x/net v0.0.0-20210330075724-22f4162a9025/go.mod h1:uSPa2vr4CLtc/ILN5odXGNXS6mhrKVzTaCXzk9m6W3k= -golang.org/x/net v0.0.0-20210330142815-c8897c278d10 h1:j6SlnKgJhONcqBZAH/cLNz6hg79L7Q18RY4BiN0r0LI= -golang.org/x/net v0.0.0-20210330142815-c8897c278d10/go.mod h1:uSPa2vr4CLtc/ILN5odXGNXS6mhrKVzTaCXzk9m6W3k= -golang.org/x/net v0.0.0-20210330230544-e57232859fb2 h1:nGCZOty+lVDsc4H2qPFksI5Se296+V+GhMiL/TzmYNk= -golang.org/x/net v0.0.0-20210330230544-e57232859fb2/go.mod h1:p54w0d4576C0XHj96bSt6lcn1PtDYWL6XObtHCRCNQM= -golang.org/x/net v0.0.0-20210331060903-cb1fcc7394e5 h1:zuP3axpB9rV3xH0EA7n3/gCrNPZm2SRl0l4mVH2BRj4= -golang.org/x/net v0.0.0-20210331060903-cb1fcc7394e5/go.mod h1:p54w0d4576C0XHj96bSt6lcn1PtDYWL6XObtHCRCNQM= -golang.org/x/net v0.0.0-20210331212208-0fccb6fa2b5c h1:KHUzaHIpjWVlVVNh65G3hhuj3KB1HnjY6Cq5cTvRQT8= -golang.org/x/net v0.0.0-20210331212208-0fccb6fa2b5c/go.mod h1:p54w0d4576C0XHj96bSt6lcn1PtDYWL6XObtHCRCNQM= -golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4 h1:4nGaVu0QrbjT/AK2PRLuQfQuh6DJve+pELhqTdAj3x0= golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4/go.mod h1:p54w0d4576C0XHj96bSt6lcn1PtDYWL6XObtHCRCNQM= golang.org/x/net v0.0.0-20210410081132-afb366fc7cd1 h1:4qWs8cYYH6PoEFy4dfhDFgoMGkwAcETd+MmPdCPMzUc= golang.org/x/net v0.0.0-20210410081132-afb366fc7cd1/go.mod h1:9tjilg8BloeKEkVJvy7fQ90B1CfIiPueXVOjqfkSzI8= golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20210315160823-c6e025ad8005/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20210324051608-47abb6519492/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= diff --git a/handler.go b/handler.go index 62ca13c..0d2b618 100644 --- a/handler.go +++ b/handler.go @@ -74,6 +74,16 @@ func (h *httpServer) ServeHTTP(w http.ResponseWriter, r *http.Request) { } } + ct := DefaultContentType + if htype := r.Header.Get("Content-Type"); htype != "" { + ct = htype + } + + if ph, ok := h.contentTypeHandlers[strings.Split(ct, ";")[0]]; ok { + ph(w, r) + return + } + ctx := metadata.NewContext(r.Context(), nil) defer r.Body.Close() @@ -84,11 +94,6 @@ func (h *httpServer) ServeHTTP(w http.ResponseWriter, r *http.Request) { return } - ct := DefaultContentType - if htype := r.Header.Get("Content-Type"); htype != "" { - ct = htype - } - var cf codec.Codec var err error switch ct { diff --git a/http.go b/http.go index ac181d9..cb46571 100644 --- a/http.go +++ b/http.go @@ -33,10 +33,11 @@ type httpServer struct { // used for first registration registered bool // register service instance - rsvc *register.Service - init bool - errorHandler func(context.Context, server.Handler, http.ResponseWriter, *http.Request, error, int) - pathHandlers map[*regexp.Regexp]http.HandlerFunc + rsvc *register.Service + init bool + errorHandler func(context.Context, server.Handler, http.ResponseWriter, *http.Request, error, int) + pathHandlers map[*regexp.Regexp]http.HandlerFunc + contentTypeHandlers map[string]http.HandlerFunc } func (h *httpServer) newCodec(ct string) (codec.Codec, error) { @@ -73,6 +74,9 @@ func (h *httpServer) Init(opts ...server.Option) error { if h.pathHandlers == nil { h.pathHandlers = make(map[*regexp.Regexp]http.HandlerFunc) } + if h.contentTypeHandlers == nil { + h.contentTypeHandlers = make(map[string]http.HandlerFunc) + } if phs, ok := h.opts.Context.Value(pathHandlerKey{}).(*pathHandlerVal); ok && phs.h != nil { for pp, ph := range phs.h { exp, err := regexp.Compile(pp) @@ -82,6 +86,12 @@ func (h *httpServer) Init(opts ...server.Option) error { h.pathHandlers[exp] = ph } } + if phs, ok := h.opts.Context.Value(contentTypeHandlerKey{}).(*contentTypeHandlerVal); ok && phs.h != nil { + for pp, ph := range phs.h { + h.contentTypeHandlers[pp] = ph + } + } + if err := h.opts.Register.Init(); err != nil { return err } diff --git a/options.go b/options.go index b116a23..66fa6fc 100644 --- a/options.go +++ b/options.go @@ -84,3 +84,23 @@ func PathHandler(path string, h http.HandlerFunc) server.Option { o.Context = context.WithValue(o.Context, pathHandlerKey{}, v) } } + +type contentTypeHandlerKey struct{} +type contentTypeHandlerVal struct { + h map[string]http.HandlerFunc +} + +// ContentTypeHandler specifies http handler for Content-Type +func ContentTypeHandler(ct string, h http.HandlerFunc) server.Option { + return func(o *server.Options) { + if o.Context == nil { + o.Context = context.Background() + } + v, ok := o.Context.Value(contentTypeHandlerKey{}).(*contentTypeHandlerVal) + if !ok { + v = &contentTypeHandlerVal{h: make(map[string]http.HandlerFunc)} + } + v.h[ct] = h + o.Context = context.WithValue(o.Context, contentTypeHandlerKey{}, v) + } +}