4
4
"net"
5
5
6
6
"github.com/fagongzi/log"
7
+ "github.com/labstack/echo"
7
8
"google.golang.org/grpc"
8
9
"google.golang.org/grpc/naming"
9
10
)
@@ -13,12 +14,13 @@ type ServiceRegister func(*grpc.Server) []Service
13
14
14
15
// GRPCServer is a grpc server
15
16
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
22
24
}
23
25
24
26
// NewGRPCServer returns a grpc server
@@ -35,6 +37,26 @@ func NewGRPCServer(addr string, register ServiceRegister, opts ...ServerOption)
35
37
}
36
38
}
37
39
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
+
38
60
// Start start this api server
39
61
func (s * GRPCServer ) Start () error {
40
62
defer func () {
@@ -56,14 +78,21 @@ func (s *GRPCServer) Start() error {
56
78
s .publishServices ()
57
79
58
80
if s .opts .httpServer != "" {
59
- s .httpServer = newHTTPServer ( s . opts . httpServer )
81
+ s .createHTTPServer ( )
60
82
for _ , service := range s .services {
61
83
if len (service .opts .httpEntrypoints ) > 0 {
62
- s .httpServer .addService (service )
84
+ s .httpServer .addHTTPEntrypoints (service . opts . httpEntrypoints ... )
63
85
log .Infof ("rpc: service %s added to http proxy" , service .Name )
64
86
}
65
87
}
88
+ }
66
89
90
+ if len (s .httpHandlers ) > 0 {
91
+ s .createHTTPServer ()
92
+ s .httpServer .addHTTPEntrypoints (s .httpHandlers ... )
93
+ }
94
+
95
+ if s .httpServer != nil {
67
96
go func () {
68
97
err := s .httpServer .start ()
69
98
if err != nil {
@@ -87,6 +116,20 @@ func (s *GRPCServer) GracefulStop() {
87
116
s .server .GracefulStop ()
88
117
}
89
118
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
+
90
133
func (s * GRPCServer ) publishServices () {
91
134
if s .opts .publisher != nil {
92
135
for _ , service := range s .services {
0 commit comments