mirror of
https://github.com/go-kratos/kratos.git
synced 2025-01-14 02:33:03 +02:00
add endpoint option in grpc server (#1488)
This commit is contained in:
parent
039ce62778
commit
db2122860d
@ -89,6 +89,13 @@ func Options(opts ...grpc.ServerOption) ServerOption {
|
||||
}
|
||||
}
|
||||
|
||||
// Endpoint with server endpoint.
|
||||
func Endpoint(endpoint *url.URL) ServerOption {
|
||||
return func(o *Server) {
|
||||
o.endpoint = endpoint
|
||||
}
|
||||
}
|
||||
|
||||
// Server is a gRPC server wrapper.
|
||||
type Server struct {
|
||||
*grpc.Server
|
||||
@ -151,6 +158,9 @@ func NewServer(opts ...ServerOption) *Server {
|
||||
// grpc://127.0.0.1:9000?isSecure=false
|
||||
func (s *Server) Endpoint() (*url.URL, error) {
|
||||
s.once.Do(func() {
|
||||
if s.endpoint != nil {
|
||||
return
|
||||
}
|
||||
lis, err := net.Listen(s.network, s.address)
|
||||
if err != nil {
|
||||
s.err = err
|
||||
|
@ -82,6 +82,15 @@ func TestMiddleware(t *testing.T) {
|
||||
assert.Equal(t, v, o.middleware)
|
||||
}
|
||||
|
||||
func TestEndpoint(t *testing.T) {
|
||||
u, err := url.Parse("grpc://hello/world")
|
||||
assert.NoError(t, err)
|
||||
o := &Server{}
|
||||
Endpoint(u)(o)
|
||||
assert.Equal(t, "hello", o.endpoint.Host)
|
||||
assert.Equal(t, "grpc", o.endpoint.Scheme)
|
||||
}
|
||||
|
||||
type mockLogger struct {
|
||||
level log.Level
|
||||
key string
|
||||
|
Loading…
Reference in New Issue
Block a user