Skip to content

Commit

Permalink
dev: add pther http entrypoint
Browse files Browse the repository at this point in the history
  • Loading branch information
zhangxu19830126 committed Dec 24, 2017
1 parent a01ab85 commit f0aeb92
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 18 deletions.
12 changes: 12 additions & 0 deletions api.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package grpcx

// API api
type API struct {
Name string
HTTP APIEntrypoint
}

// APIEntrypoint api http entrypoint
type APIEntrypoint struct {
GET, PUT, DELETE, POST string
}
20 changes: 17 additions & 3 deletions http.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,14 @@ import (
md "github.com/labstack/echo/middleware"
)

type httpEntrypoint struct {
path string
method string
reqFactory func() interface{}
invoker func(interface{}, echo.Context) (interface{}, error)
handler func(echo.Context) error
}

type httpServer struct {
addr string
server *echo.Echo
Expand All @@ -35,8 +43,8 @@ func (s *httpServer) stop() error {
return s.server.Shutdown(context.Background())
}

func (s *httpServer) addService(service Service) {
for _, ep := range service.opts.httpEntrypoints {
func (s *httpServer) addHTTPEntrypoints(httpEntrypoints ...*httpEntrypoint) {
for _, ep := range httpEntrypoints {
m := strings.ToUpper(ep.method)
switch m {
case echo.GET:
Expand All @@ -60,10 +68,16 @@ func (s *httpServer) addService(service Service) {
}

func (s *httpServer) handleHTTP(c echo.Context, ep *httpEntrypoint) error {
if ep.invoker == nil || ep.reqFactory == nil {
if ep.handler == nil &&
ep.invoker == nil &&
ep.reqFactory == nil {
return c.NoContent(http.StatusServiceUnavailable)
}

if ep.handler != nil {
return ep.handler(c)
}

data, err := ioutil.ReadAll(c.Request().Body)
if err != nil {
return c.String(http.StatusBadRequest, err.Error())
Expand Down
59 changes: 51 additions & 8 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"net"

"github.com/fagongzi/log"
"github.com/labstack/echo"
"google.golang.org/grpc"
"google.golang.org/grpc/naming"
)
Expand All @@ -13,12 +14,13 @@ type ServiceRegister func(*grpc.Server) []Service

// GRPCServer is a grpc server
type GRPCServer struct {
addr string
httpServer *httpServer
server *grpc.Server
opts *serverOptions
register ServiceRegister
services []Service
addr string
httpServer *httpServer
server *grpc.Server
opts *serverOptions
register ServiceRegister
services []Service
httpHandlers []*httpEntrypoint
}

// NewGRPCServer returns a grpc server
Expand All @@ -35,6 +37,26 @@ func NewGRPCServer(addr string, register ServiceRegister, opts ...ServerOption)
}
}

// AddGetHTTPHandler add get http handler
func (s *GRPCServer) AddGetHTTPHandler(path string, handler func(echo.Context) error) {
s.addHTTPHandler(path, echo.GET, handler)
}

// AddPostHTTPHandler add post http handler
func (s *GRPCServer) AddPostHTTPHandler(path string, handler func(echo.Context) error) {
s.addHTTPHandler(path, echo.POST, handler)
}

// AddPutHTTPHandler add put http handler
func (s *GRPCServer) AddPutHTTPHandler(path string, handler func(echo.Context) error) {
s.addHTTPHandler(path, echo.PUT, handler)
}

// AddDeleteHTTPHandler add delete http handler
func (s *GRPCServer) AddDeleteHTTPHandler(path string, handler func(echo.Context) error) {
s.addHTTPHandler(path, echo.DELETE, handler)
}

// Start start this api server
func (s *GRPCServer) Start() error {
defer func() {
Expand All @@ -56,14 +78,21 @@ func (s *GRPCServer) Start() error {
s.publishServices()

if s.opts.httpServer != "" {
s.httpServer = newHTTPServer(s.opts.httpServer)
s.createHTTPServer()
for _, service := range s.services {
if len(service.opts.httpEntrypoints) > 0 {
s.httpServer.addService(service)
s.httpServer.addHTTPEntrypoints(service.opts.httpEntrypoints...)
log.Infof("rpc: service %s added to http proxy", service.Name)
}
}
}

if len(s.httpHandlers) > 0 {
s.createHTTPServer()
s.httpServer.addHTTPEntrypoints(s.httpHandlers...)
}

if s.httpServer != nil {
go func() {
err := s.httpServer.start()
if err != nil {
Expand All @@ -87,6 +116,20 @@ func (s *GRPCServer) GracefulStop() {
s.server.GracefulStop()
}

func (s *GRPCServer) addHTTPHandler(path, method string, handler func(echo.Context) error) {
s.httpHandlers = append(s.httpHandlers, &httpEntrypoint{
path: path,
method: method,
handler: handler,
})
}

func (s *GRPCServer) createHTTPServer() {
if s.httpServer == nil {
s.httpServer = newHTTPServer(s.opts.httpServer)
}
}

func (s *GRPCServer) publishServices() {
if s.opts.publisher != nil {
for _, service := range s.services {
Expand Down
7 changes: 0 additions & 7 deletions service.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,6 @@ type serviceOptions struct {
httpEntrypoints []*httpEntrypoint
}

type httpEntrypoint struct {
path string
method string
reqFactory func() interface{}
invoker func(interface{}, echo.Context) (interface{}, error)
}

// Service is a service define
type Service struct {
Name string
Expand Down

0 comments on commit f0aeb92

Please sign in to comment.