Documentation
¶
Overview ¶
Package server provides a HTTP server with custom timeouts and error handling.
This package is designed to be used with the wasabi package for handling HTTP requests. It provides a Server struct that wraps the standard http.Server with additional functionality.
The Server struct includes a base context, a net.Listener for accepting connections, and a mutex for thread safety. It also includes a ready channel that can be used to signal when the server is ready to accept connections.
The server uses custom read header and read timeouts, defined as constants. These timeouts help to prevent slow client attacks by limiting the amount of time the server will wait for a client to send its request.
Usage:
s := server.NewServer(":8080") s.AddChannel(channel.NewChannel("/path", handler)) err := s.Start() if err != nil { log.Fatal(err) }
This will start a new server on port 8080 with the provided handler.
Index ¶
Constants ¶
const ReadHeaderTimeoutSeconds = 3
const ReadTimeoutSeconds = 30
Variables ¶
var DefaultConfig = Config{ ReadHeaderTimeout: ReadHeaderTimeoutSeconds * time.Second, ReadTimeout: ReadTimeoutSeconds * time.Second, }
var ErrServerAlreadyRunning = fmt.Errorf("server is already running")
Functions ¶
This section is empty.
Types ¶
type Option ¶
type Option func(*Server)
func WithBaseContext ¶
BaseContext optionally specifies based context that will be used for all connections. If not specified, context.Background() will be used.
func WithProfilerEndpoint ¶ added in v0.5.0
func WithProfilerEndpoint() Option
WithProfilerEndpoint is an option function that enables the profiler endpoint for the server. Enabling the profiler endpoint allows profiling and performance monitoring of the server. The profiler endpoint is available at /debug/pprof/. To use the profiler endpoint, import the net/http/pprof package in your application. Example:
import _ "net/http/pprof"
The profiler endpoint is disabled by default.
func WithReadinessChan ¶ added in v0.4.0
func WithReadinessChan(ch chan<- struct{}) Option
WithReadinessChan sets ch to Server and will be closed once the Server is ready to accept connection. Typically used in testing after calling [Run] method and waiting for ch to close, before continuing with test logics.
func WithServerConfig ¶ added in v0.5.0
WithServerConfig is an option function that overrides the default configuration settings of the server. This can give the client more control over the server feature functions
func WithTLS ¶ added in v0.5.0
WithTLS is an option function that configures the server to use TLS (Transport Layer Security). It sets the certificate and key file paths, and optionally allows custom TLS configuration. The certificate and key file paths must be provided as arguments. If a custom TLS configuration is provided, it will be applied to the server's handler.
type Server ¶
type Server struct {
// contains filtered or unexported fields
}
func NewServer ¶
NewServer creates new instance of Wasabi server port - port to listen on returns new instance of Server
func (*Server) AddChannel ¶
AddChannel adds new channel to server
func (*Server) AddHandler ¶ added in v0.5.3
AddHandler adds new handler to server
func (*Server) Addr ¶ added in v0.3.0
Addr returns the server's network address. If the server is not running, it returns nil.
func (*Server) Close ¶
Shutdown gracefully shuts down the server and all its channels. It waits for all channels to be shut down before returning. If the context is canceled before all channels are shut down, it returns the context error. If any error occurs during the shutdown process, it returns the first error encountered.
func (*Server) GetServerConfig ¶ added in v0.5.0
Helper to fetch server config. Returns Default config if base ctx has no config set