Skip to content

Commit

Permalink
dev: opt http entrypoint
Browse files Browse the repository at this point in the history
  • Loading branch information
zhangxu19830126 committed Dec 18, 2017
1 parent 2f75ee9 commit 524fdd1
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 11 deletions.
20 changes: 17 additions & 3 deletions http.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package grpcx

import (
"context"
"encoding/json"
"io/ioutil"
"net/http"
"strings"
Expand Down Expand Up @@ -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)
}

Expand All @@ -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)
Expand Down
41 changes: 33 additions & 8 deletions service.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package grpcx

import (
"github.com/labstack/echo"
)

//ServiceOption service options
type ServiceOption func(*serviceOptions)

Expand All @@ -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
Expand All @@ -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,
})
}
}

0 comments on commit 524fdd1

Please sign in to comment.