|
| 1 | +/* |
| 2 | +Copyright IBM Corp. All Rights Reserved. |
| 3 | +
|
| 4 | +SPDX-License-Identifier: Apache-2.0 |
| 5 | +*/ |
| 6 | + |
| 7 | +package fabxhttp |
| 8 | + |
| 9 | +import ( |
| 10 | + "context" |
| 11 | + "crypto/tls" |
| 12 | + "net" |
| 13 | + "net/http" |
| 14 | + "os" |
| 15 | + "time" |
| 16 | + |
| 17 | + "github.com/hyperledger/fabric-lib-go/common/flogging" |
| 18 | + |
| 19 | + "github.com/hyperledger/fabric-x-common/common/middleware" |
| 20 | + "github.com/hyperledger/fabric-x-common/common/util" |
| 21 | +) |
| 22 | + |
| 23 | +// Logger defines the logging interface for the HTTP server. |
| 24 | +type Logger interface { |
| 25 | + Warn(args ...any) |
| 26 | + Warnf(template string, args ...any) |
| 27 | +} |
| 28 | + |
| 29 | +// Options contains configuration options for the HTTP server. |
| 30 | +type Options struct { |
| 31 | + Logger Logger |
| 32 | + ListenAddress string |
| 33 | + TLS TLS |
| 34 | +} |
| 35 | + |
| 36 | +// Server represents an HTTP server with TLS support and middleware capabilities. |
| 37 | +type Server struct { |
| 38 | + logger Logger |
| 39 | + options Options |
| 40 | + httpServer *http.Server |
| 41 | + mux *http.ServeMux |
| 42 | + addr string |
| 43 | +} |
| 44 | + |
| 45 | +// NewServer creates a new HTTP server with the provided options. |
| 46 | +func NewServer(o Options) *Server { |
| 47 | + logger := o.Logger |
| 48 | + if logger == nil { |
| 49 | + logger = flogging.MustGetLogger("fabhttp") |
| 50 | + } |
| 51 | + |
| 52 | + server := &Server{ |
| 53 | + logger: logger, |
| 54 | + options: o, |
| 55 | + } |
| 56 | + |
| 57 | + server.initializeServer() |
| 58 | + |
| 59 | + return server |
| 60 | +} |
| 61 | + |
| 62 | +// Run starts the server and blocks until a signal is received, then stops the server. |
| 63 | +func (s *Server) Run(signals <-chan os.Signal, ready chan<- struct{}) error { |
| 64 | + err := s.Start() |
| 65 | + if err != nil { |
| 66 | + return err |
| 67 | + } |
| 68 | + |
| 69 | + close(ready) |
| 70 | + |
| 71 | + <-signals |
| 72 | + return s.Stop() |
| 73 | +} |
| 74 | + |
| 75 | +// Start begins listening and serving HTTP requests in a goroutine. |
| 76 | +func (s *Server) Start() error { |
| 77 | + listener, err := s.Listen() |
| 78 | + if err != nil { |
| 79 | + return err |
| 80 | + } |
| 81 | + s.addr = listener.Addr().String() |
| 82 | + |
| 83 | + go func() { |
| 84 | + if err := s.httpServer.Serve(listener); err != nil && err != http.ErrServerClosed { |
| 85 | + s.logger.Warnf("HTTP server stopped with error: %v", err) |
| 86 | + } |
| 87 | + }() |
| 88 | + |
| 89 | + return nil |
| 90 | +} |
| 91 | + |
| 92 | +// Stop gracefully shuts down the server with a timeout. |
| 93 | +func (s *Server) Stop() error { |
| 94 | + ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) |
| 95 | + defer cancel() |
| 96 | + |
| 97 | + return s.httpServer.Shutdown(ctx) |
| 98 | +} |
| 99 | + |
| 100 | +func (s *Server) initializeServer() { |
| 101 | + s.mux = http.NewServeMux() |
| 102 | + s.httpServer = &http.Server{ |
| 103 | + Addr: s.options.ListenAddress, |
| 104 | + Handler: s.mux, |
| 105 | + ReadTimeout: 10 * time.Second, |
| 106 | + WriteTimeout: 2 * time.Minute, |
| 107 | + } |
| 108 | +} |
| 109 | + |
| 110 | +// HandlerChain wraps the provided handler with middleware based on security requirements. |
| 111 | +func (s *Server) HandlerChain(h http.Handler, secure bool) http.Handler { |
| 112 | + if secure { |
| 113 | + return middleware.NewChain(middleware.RequireCert(), middleware.WithRequestID(util.GenerateUUID)).Handler(h) |
| 114 | + } |
| 115 | + return middleware.NewChain(middleware.WithRequestID(util.GenerateUUID)).Handler(h) |
| 116 | +} |
| 117 | + |
| 118 | +// RegisterHandler registers into the ServeMux a handler chain that borrows |
| 119 | +// its security properties from the fabhttp.Server. This method is thread |
| 120 | +// safe because ServeMux.Handle() is thread safe, and options are immutable. |
| 121 | +// This method can be called either before or after Server.Start(). If the |
| 122 | +// pattern exists the method panics. |
| 123 | +func (s *Server) RegisterHandler(pattern string, handler http.Handler, secure bool) { |
| 124 | + s.mux.Handle( |
| 125 | + pattern, |
| 126 | + s.HandlerChain( |
| 127 | + handler, |
| 128 | + secure, |
| 129 | + ), |
| 130 | + ) |
| 131 | +} |
| 132 | + |
| 133 | +// Listen creates a network listener with optional TLS configuration. |
| 134 | +func (s *Server) Listen() (net.Listener, error) { |
| 135 | + listener, err := net.Listen("tcp", s.options.ListenAddress) |
| 136 | + if err != nil { |
| 137 | + return nil, err |
| 138 | + } |
| 139 | + tlsConfig, err := s.options.TLS.Config() |
| 140 | + if err != nil { |
| 141 | + return nil, err |
| 142 | + } |
| 143 | + if tlsConfig != nil { |
| 144 | + listener = tls.NewListener(listener, tlsConfig) |
| 145 | + } |
| 146 | + return listener, nil |
| 147 | +} |
| 148 | + |
| 149 | +// Addr returns the server's listening address. |
| 150 | +func (s *Server) Addr() string { |
| 151 | + return s.addr |
| 152 | +} |
| 153 | + |
| 154 | +// Log logs a warning message with the provided key-value pairs. |
| 155 | +func (s *Server) Log(keyvals ...any) error { |
| 156 | + s.logger.Warn(keyvals...) |
| 157 | + return nil |
| 158 | +} |
0 commit comments