Documentation ¶
Index ¶
- Constants
- func BuildHTTPMux(ctx context.Context, options ...Option) http.Handler
- func ListenAndServe(parentContext context.Context, srv *http.Server, options ...ListenOption) (err error)
- func NewDefaultServer(ctx context.Context, address string, routes ...Route) (*http.Server, error)
- func NewLogHandlerMiddleware(handler http.Handler, logger *slog.Logger, level slog.Level, message string) http.Handler
- func NewRecoveryHandlerMiddleware(handler http.Handler, logger *slog.Logger) http.Handler
- func NewServer(ctx context.Context, address string, options ...Option) (*http.Server, error)
- type ListenOption
- type Option
- type PanicError
- type Route
Constants ¶
const ( HeaderReadTimeout = 3 * time.Second MetricsRoutePath = "/metrics" )
Variables ¶
This section is empty.
Functions ¶
func BuildHTTPMux ¶
BuildHTTPMux creates an HTTP handler based on the provided register function.
The registerFn parameter is a function that registers handlers for specific patterns. It takes a RegisterHandlerFn as a parameter, which is a function that associates a pattern with a handler function.
The BuildHTTPMux function creates a new ServeMux and uses the handleFunc helper function to register handlers using the registerFn.
Example usage:
ctx, cancel := context.WithCancel(context.Background()) defer cancel() handler := httpserv.BuildHTTPMux(ctx, func(register httpserv.RegisterHandlerFn) { register("GET /helloworld", func(w servehttp.ResponseWriter, r *servehttp.Request) { w.WriteHeader(http.StatusOK) _, _ = w.Write([]byte("Hello World")) }) }) srv := &servehttp.Server{ Addr: address, Handler: handler, } err := httpserv.ListenAndServe(srv, httpserv.WithContext(ctx, cancel)) if err != nil { log.Fatal(err) }`
func ListenAndServe ¶
func ListenAndServe(parentContext context.Context, srv *http.Server, options ...ListenOption) (err error)
ListenAndServe starts an HTTP server and listens for incoming requests. It takes a context.Context, an *servehttp.Server, and optional configuration options. The returned error indicates if there was an error starting the server or shutting it down. It uses OpenTelemetry to set up tracing and metrics for the server.
The server’s BaseContext is set to the provided context.Context.
If the context.Context is canceled, the server is gracefully shutdown.
Example usage: err := ListenAndServe(ctx, srv)
if err != nil { log.Fatal(err) }
func NewDefaultServer ¶
func NewLogHandlerMiddleware ¶
Types ¶
type ListenOption ¶
type ListenOption interface {
// contains filtered or unexported methods
}
ListenOption is an interface that represents a configuration option for the serveListenConfigOptions struct. All implementations of ListenOption must implement the apply method, which takes a *serveConfigOptions parameter and applies the configuration option to it.
func WithListenSSL ¶
func WithListenSSL(certFile, keyFile string) ListenOption
WithListenSSL returns an Option that configures the server with SSL.
The certFile parameter specifies the path to the certificate file.
The keyFile parameter specifies the path to the private key file.
Example usage:
opts := []Option{ WithListenSSL("cert.pem", "key.pem"), } server := NewServer(opts...)
The server will use the provided SSL certificate and private key files to servehttp the connections securely over HTTPS. If WithListenSSL is not used, the server will listen on HTTP.
type Option ¶
type Option interface {
// contains filtered or unexported methods
}
func WithAddress ¶
WithAddress returns an Option that configures the server with the given address.
The address parameter specifies the address to use for the server.
Example usage:
opts := []Option{ WithAddress(address), } server := NewServer(opts...)
The server will use the provided address for the server.
func WithLogger ¶
WithLogger returns an Option that configures the server with the given logger.
The logger parameter specifies the logger to use for logging.
Example usage:
opts := []Option{ WithLogger(logger), } server := NewServer(opts...)
The server will use the provided logger for logging.
func WithMiddlewareLogLevel ¶
WithMiddlewareLogLevel returns an Option that configures the server with the given middleware log level.
The level parameter specifies the log level to use for middleware logging.
Example usage:
opts := []Option{ WithMiddlewareLogLevel(slog.LevelInfo), } server := NewServer(opts...)
The server will use the provided log level for middleware logging.
func WithRoute ¶
WithRoute returns an Option that configures the server with the given route.
The pattern parameter specifies the pattern to use for the route.
The handler parameter specifies the handler to use for the route.
Example usage:
opts := []Option{ WithRoute("/helloworld", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusOK) _, _ = w.Write([]byte("Hello World")) })), } server := NewServer(opts...)
The server will use the provided route for the server.
func WithRoutes ¶
WithRoutes returns an Option that configures the server with the given routes.
The routes parameter specifies the routes to use for the server.
Example usage:
opts := []Option{ WithRoutes(routes...), } server := NewServer(opts...)
The server will use the provided routes for the server.
type PanicError ¶
type PanicError struct {
// contains filtered or unexported fields
}
func (*PanicError) Error ¶
func (e *PanicError) Error() string
func (*PanicError) Stack ¶
func (e *PanicError) Stack() string