From 524fdd1207920b3946d98b1e18678179a0d7ad9f Mon Sep 17 00:00:00 2001 From: "zhangxu19830126@gmail.com" Date: Mon, 18 Dec 2017 22:55:36 +0800 Subject: [PATCH] dev: opt http entrypoint --- http.go | 20 +++++++++++++++++--- service.go | 41 +++++++++++++++++++++++++++++++++-------- 2 files changed, 50 insertions(+), 11 deletions(-) diff --git a/http.go b/http.go index 2442cca..6993850 100644 --- a/http.go +++ b/http.go @@ -2,6 +2,7 @@ package grpcx import ( "context" + "encoding/json" "io/ioutil" "net/http" "strings" @@ -59,7 +60,7 @@ func (s *httpServer) addService(service Service) { } func (s *httpServer) handleHTTP(c echo.Context, ep *httpEntrypoint) error { - if ep.invoker == nil { + if ep.invoker == nil || ep.reqFactory == nil { return c.NoContent(http.StatusServiceUnavailable) } @@ -68,9 +69,22 @@ func (s *httpServer) handleHTTP(c echo.Context, ep *httpEntrypoint) error { return c.String(http.StatusBadRequest, err.Error()) } - result, err := ep.invoker(data) + req := ep.reqFactory() + if len(data) > 0 { + err = json.Unmarshal(data, req) + if err != nil { + return c.String(http.StatusBadRequest, err.Error()) + } + } + + rsp, err := ep.invoker(req) + if err != nil { + return c.String(http.StatusInternalServerError, err.Error()) + } + + result, err := json.Marshal(rsp) if err != nil { - return c.String(http.StatusServiceUnavailable, err.Error()) + return c.String(http.StatusInternalServerError, err.Error()) } return c.JSONBlob(http.StatusOK, result) diff --git a/service.go b/service.go index 97b409a..34c7c94 100644 --- a/service.go +++ b/service.go @@ -1,5 +1,9 @@ package grpcx +import ( + "github.com/labstack/echo" +) + //ServiceOption service options type ServiceOption func(*serviceOptions) @@ -8,9 +12,10 @@ type serviceOptions struct { } type httpEntrypoint struct { - path string - method string - invoker func([]byte) ([]byte, error) + path string + method string + reqFactory func() interface{} + invoker func(interface{}) (interface{}, error) } // Service is a service define @@ -36,13 +41,33 @@ func NewService(name string, metadata interface{}, opts ...ServiceOption) Servic return service } -// WithAddHTTPEntrypoint add a http service metadata -func WithAddHTTPEntrypoint(path, method string, invoker func([]byte) ([]byte, error)) ServiceOption { +// WithAddGetHTTPEntrypoint add a http get service metadata +func WithAddGetHTTPEntrypoint(path string, reqFactory func() interface{}, invoker func(interface{}) (interface{}, error)) ServiceOption { + return withAddHTTPEntrypoint(path, echo.GET, reqFactory, invoker) +} + +// WithAddPutHTTPEntrypoint add a http put service metadata +func WithAddPutHTTPEntrypoint(path string, reqFactory func() interface{}, invoker func(interface{}) (interface{}, error)) ServiceOption { + return withAddHTTPEntrypoint(path, echo.PUT, reqFactory, invoker) +} + +// WithAddPostHTTPEntrypoint add a http post service metadata +func WithAddPostHTTPEntrypoint(path string, reqFactory func() interface{}, invoker func(interface{}) (interface{}, error)) ServiceOption { + return withAddHTTPEntrypoint(path, echo.POST, reqFactory, invoker) +} + +// WithAddDeleteHTTPEntrypoint add a http post service metadata +func WithAddDeleteHTTPEntrypoint(path string, reqFactory func() interface{}, invoker func(interface{}) (interface{}, error)) ServiceOption { + return withAddHTTPEntrypoint(path, echo.DELETE, reqFactory, invoker) +} + +func withAddHTTPEntrypoint(path, method string, reqFactory func() interface{}, invoker func(interface{}) (interface{}, error)) ServiceOption { return func(opt *serviceOptions) { opt.httpEntrypoints = append(opt.httpEntrypoints, &httpEntrypoint{ - path: path, - method: method, - invoker: invoker, + path: path, + method: method, + reqFactory: reqFactory, + invoker: invoker, }) } }