Skip to content

Commit f0aeb92

Browse files
dev: add pther http entrypoint
1 parent a01ab85 commit f0aeb92

File tree

4 files changed

+80
-18
lines changed

4 files changed

+80
-18
lines changed

api.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package grpcx
2+
3+
// API api
4+
type API struct {
5+
Name string
6+
HTTP APIEntrypoint
7+
}
8+
9+
// APIEntrypoint api http entrypoint
10+
type APIEntrypoint struct {
11+
GET, PUT, DELETE, POST string
12+
}

http.go

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,14 @@ import (
1212
md "github.com/labstack/echo/middleware"
1313
)
1414

15+
type httpEntrypoint struct {
16+
path string
17+
method string
18+
reqFactory func() interface{}
19+
invoker func(interface{}, echo.Context) (interface{}, error)
20+
handler func(echo.Context) error
21+
}
22+
1523
type httpServer struct {
1624
addr string
1725
server *echo.Echo
@@ -35,8 +43,8 @@ func (s *httpServer) stop() error {
3543
return s.server.Shutdown(context.Background())
3644
}
3745

38-
func (s *httpServer) addService(service Service) {
39-
for _, ep := range service.opts.httpEntrypoints {
46+
func (s *httpServer) addHTTPEntrypoints(httpEntrypoints ...*httpEntrypoint) {
47+
for _, ep := range httpEntrypoints {
4048
m := strings.ToUpper(ep.method)
4149
switch m {
4250
case echo.GET:
@@ -60,10 +68,16 @@ func (s *httpServer) addService(service Service) {
6068
}
6169

6270
func (s *httpServer) handleHTTP(c echo.Context, ep *httpEntrypoint) error {
63-
if ep.invoker == nil || ep.reqFactory == nil {
71+
if ep.handler == nil &&
72+
ep.invoker == nil &&
73+
ep.reqFactory == nil {
6474
return c.NoContent(http.StatusServiceUnavailable)
6575
}
6676

77+
if ep.handler != nil {
78+
return ep.handler(c)
79+
}
80+
6781
data, err := ioutil.ReadAll(c.Request().Body)
6882
if err != nil {
6983
return c.String(http.StatusBadRequest, err.Error())

server.go

Lines changed: 51 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"net"
55

66
"github.com/fagongzi/log"
7+
"github.com/labstack/echo"
78
"google.golang.org/grpc"
89
"google.golang.org/grpc/naming"
910
)
@@ -13,12 +14,13 @@ type ServiceRegister func(*grpc.Server) []Service
1314

1415
// GRPCServer is a grpc server
1516
type GRPCServer struct {
16-
addr string
17-
httpServer *httpServer
18-
server *grpc.Server
19-
opts *serverOptions
20-
register ServiceRegister
21-
services []Service
17+
addr string
18+
httpServer *httpServer
19+
server *grpc.Server
20+
opts *serverOptions
21+
register ServiceRegister
22+
services []Service
23+
httpHandlers []*httpEntrypoint
2224
}
2325

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

40+
// AddGetHTTPHandler add get http handler
41+
func (s *GRPCServer) AddGetHTTPHandler(path string, handler func(echo.Context) error) {
42+
s.addHTTPHandler(path, echo.GET, handler)
43+
}
44+
45+
// AddPostHTTPHandler add post http handler
46+
func (s *GRPCServer) AddPostHTTPHandler(path string, handler func(echo.Context) error) {
47+
s.addHTTPHandler(path, echo.POST, handler)
48+
}
49+
50+
// AddPutHTTPHandler add put http handler
51+
func (s *GRPCServer) AddPutHTTPHandler(path string, handler func(echo.Context) error) {
52+
s.addHTTPHandler(path, echo.PUT, handler)
53+
}
54+
55+
// AddDeleteHTTPHandler add delete http handler
56+
func (s *GRPCServer) AddDeleteHTTPHandler(path string, handler func(echo.Context) error) {
57+
s.addHTTPHandler(path, echo.DELETE, handler)
58+
}
59+
3860
// Start start this api server
3961
func (s *GRPCServer) Start() error {
4062
defer func() {
@@ -56,14 +78,21 @@ func (s *GRPCServer) Start() error {
5678
s.publishServices()
5779

5880
if s.opts.httpServer != "" {
59-
s.httpServer = newHTTPServer(s.opts.httpServer)
81+
s.createHTTPServer()
6082
for _, service := range s.services {
6183
if len(service.opts.httpEntrypoints) > 0 {
62-
s.httpServer.addService(service)
84+
s.httpServer.addHTTPEntrypoints(service.opts.httpEntrypoints...)
6385
log.Infof("rpc: service %s added to http proxy", service.Name)
6486
}
6587
}
88+
}
6689

90+
if len(s.httpHandlers) > 0 {
91+
s.createHTTPServer()
92+
s.httpServer.addHTTPEntrypoints(s.httpHandlers...)
93+
}
94+
95+
if s.httpServer != nil {
6796
go func() {
6897
err := s.httpServer.start()
6998
if err != nil {
@@ -87,6 +116,20 @@ func (s *GRPCServer) GracefulStop() {
87116
s.server.GracefulStop()
88117
}
89118

119+
func (s *GRPCServer) addHTTPHandler(path, method string, handler func(echo.Context) error) {
120+
s.httpHandlers = append(s.httpHandlers, &httpEntrypoint{
121+
path: path,
122+
method: method,
123+
handler: handler,
124+
})
125+
}
126+
127+
func (s *GRPCServer) createHTTPServer() {
128+
if s.httpServer == nil {
129+
s.httpServer = newHTTPServer(s.opts.httpServer)
130+
}
131+
}
132+
90133
func (s *GRPCServer) publishServices() {
91134
if s.opts.publisher != nil {
92135
for _, service := range s.services {

service.go

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,6 @@ type serviceOptions struct {
1111
httpEntrypoints []*httpEntrypoint
1212
}
1313

14-
type httpEntrypoint struct {
15-
path string
16-
method string
17-
reqFactory func() interface{}
18-
invoker func(interface{}, echo.Context) (interface{}, error)
19-
}
20-
2114
// Service is a service define
2215
type Service struct {
2316
Name string

0 commit comments

Comments
 (0)