You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
kratos/transport/http/server.go

156 lines
3.6 KiB

4 years ago
package http
import (
"context"
"errors"
4 years ago
"net"
"net/http"
"net/url"
"sync"
4 years ago
"time"
ic "github.com/go-kratos/kratos/v2/internal/context"
4 years ago
"github.com/go-kratos/kratos/v2/internal/host"
"github.com/go-kratos/kratos/v2/log"
"github.com/go-kratos/kratos/v2/transport"
"github.com/gorilla/mux"
)
var _ transport.Server = (*Server)(nil)
var _ transport.Endpointer = (*Server)(nil)
4 years ago
// ServerOption is an HTTP server option.
4 years ago
type ServerOption func(*Server)
// Network with server network.
func Network(network string) ServerOption {
return func(s *Server) {
s.network = network
}
}
// Address with server address.
func Address(addr string) ServerOption {
return func(s *Server) {
s.address = addr
}
}
// Timeout with server timeout.
func Timeout(timeout time.Duration) ServerOption {
return func(s *Server) {
s.timeout = timeout
}
}
// Logger with server logger.
func Logger(logger log.Logger) ServerOption {
return func(s *Server) {
s.log = log.NewHelper(logger)
4 years ago
}
}
// Server is an HTTP server wrapper.
4 years ago
type Server struct {
*http.Server
ctx context.Context
lis net.Listener
once sync.Once
err error
network string
address string
endpoint *url.URL
timeout time.Duration
router *mux.Router
log *log.Helper
4 years ago
}
// NewServer creates an HTTP server by options.
4 years ago
func NewServer(opts ...ServerOption) *Server {
srv := &Server{
network: "tcp",
address: ":0",
timeout: 1 * time.Second,
log: log.NewHelper(log.DefaultLogger),
4 years ago
}
for _, o := range opts {
o(srv)
}
srv.router = mux.NewRouter()
srv.Server = &http.Server{Handler: srv}
return srv
}
// Handle registers a new route with a matcher for the URL path.
func (s *Server) Handle(path string, h http.Handler) {
s.router.Handle(path, h)
}
// HandlePrefix registers a new route with a matcher for the URL path prefix.
func (s *Server) HandlePrefix(prefix string, h http.Handler) {
s.router.PathPrefix(prefix).Handler(h)
}
4 years ago
// HandleFunc registers a new route with a matcher for the URL path.
func (s *Server) HandleFunc(path string, h http.HandlerFunc) {
s.router.HandleFunc(path, h)
}
// ServeHTTP should write reply headers and data to the ResponseWriter and then return.
func (s *Server) ServeHTTP(res http.ResponseWriter, req *http.Request) {
ctx, cancel := ic.Merge(req.Context(), s.ctx)
4 years ago
defer cancel()
ctx = transport.NewContext(ctx, transport.Transport{Kind: transport.KindHTTP, Endpoint: s.endpoint.String()})
ctx = NewServerContext(ctx, ServerInfo{Request: req, Response: res})
if s.timeout > 0 {
ctx, cancel = context.WithTimeout(ctx, s.timeout)
defer cancel()
}
s.router.ServeHTTP(res, req.WithContext(ctx))
4 years ago
}
// Endpoint return a real address to registry endpoint.
// examples:
// http://127.0.0.1:8000?isSecure=false
func (s *Server) Endpoint() (*url.URL, error) {
s.once.Do(func() {
lis, err := net.Listen(s.network, s.address)
if err != nil {
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
}
return s.endpoint, nil
4 years ago
}
// Start start the HTTP server.
func (s *Server) Start(ctx context.Context) error {
if _, err := s.Endpoint(); err != nil {
return err
4 years ago
}
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
}
return nil
4 years ago
}
// Stop stop the HTTP server.
func (s *Server) Stop(ctx context.Context) error {
4 years ago
s.log.Info("[HTTP] server stopping")
return s.Shutdown(context.Background())
}