|
|
|
@ -6,7 +6,7 @@ import ( |
|
|
|
|
"net" |
|
|
|
|
"net/http" |
|
|
|
|
"net/url" |
|
|
|
|
"strings" |
|
|
|
|
"sync" |
|
|
|
|
"time" |
|
|
|
|
|
|
|
|
|
ic "github.com/go-kratos/kratos/v2/internal/context" |
|
|
|
@ -56,6 +56,8 @@ type Server struct { |
|
|
|
|
*http.Server |
|
|
|
|
ctx context.Context |
|
|
|
|
lis net.Listener |
|
|
|
|
once sync.Once |
|
|
|
|
err error |
|
|
|
|
network string |
|
|
|
|
address string |
|
|
|
|
endpoint *url.URL |
|
|
|
@ -112,35 +114,33 @@ func (s *Server) ServeHTTP(res http.ResponseWriter, req *http.Request) { |
|
|
|
|
// examples:
|
|
|
|
|
// http://127.0.0.1:8000?isSecure=false
|
|
|
|
|
func (s *Server) Endpoint() (*url.URL, error) { |
|
|
|
|
if s.lis == nil && strings.HasSuffix(s.address, ":0") { |
|
|
|
|
s.once.Do(func() { |
|
|
|
|
lis, err := net.Listen(s.network, s.address) |
|
|
|
|
if err != nil { |
|
|
|
|
return nil, err |
|
|
|
|
s.err = err |
|
|
|
|
return |
|
|
|
|
} |
|
|
|
|
addr, err := host.Extract(s.address, s.lis) |
|
|
|
|
if err != nil { |
|
|
|
|
lis.Close() |
|
|
|
|
s.err = err |
|
|
|
|
return |
|
|
|
|
} |
|
|
|
|
s.lis = lis |
|
|
|
|
s.endpoint = &url.URL{Scheme: "http", Host: addr} |
|
|
|
|
}) |
|
|
|
|
if s.err != nil { |
|
|
|
|
return nil, s.err |
|
|
|
|
} |
|
|
|
|
addr, err := host.Extract(s.address, s.lis) |
|
|
|
|
if err != nil { |
|
|
|
|
return nil, err |
|
|
|
|
} |
|
|
|
|
u := &url.URL{ |
|
|
|
|
Scheme: "http", |
|
|
|
|
Host: addr, |
|
|
|
|
} |
|
|
|
|
s.endpoint = u |
|
|
|
|
return u, nil |
|
|
|
|
return s.endpoint, nil |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// Start start the HTTP server.
|
|
|
|
|
func (s *Server) Start(ctx context.Context) error { |
|
|
|
|
s.ctx = ctx |
|
|
|
|
if s.lis == nil { |
|
|
|
|
lis, err := net.Listen(s.network, s.address) |
|
|
|
|
if err != nil { |
|
|
|
|
return err |
|
|
|
|
} |
|
|
|
|
s.lis = lis |
|
|
|
|
if _, err := s.Endpoint(); err != nil { |
|
|
|
|
return err |
|
|
|
|
} |
|
|
|
|
s.ctx = ctx |
|
|
|
|
s.log.Infof("[HTTP] server listening on: %s", s.lis.Addr().String()) |
|
|
|
|
if err := s.Serve(s.lis); !errors.Is(err, http.ErrServerClosed) { |
|
|
|
|
return err |
|
|
|
|