apiserv

package
v0.0.37 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 8, 2024 License: MIT Imports: 14 Imported by: 0

Documentation

Index

Constants

View Source
const (
	HeaderReadTimeout = 3 * time.Second
	MetricsRoutePath  = "/metrics"
)

Variables

This section is empty.

Functions

func BuildHTTPMux

func BuildHTTPMux(ctx context.Context, options ...Option) http.Handler

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 NewDefaultServer(ctx context.Context, address string, routes ...Route) (*http.Server, error)

func NewLogHandlerMiddleware

func NewLogHandlerMiddleware(handler http.Handler, logger *slog.Logger, level slog.Level, message string) http.Handler

func NewRecoveryHandlerMiddleware

func NewRecoveryHandlerMiddleware(handler http.Handler, logger *slog.Logger) http.Handler

func NewServer

func NewServer(ctx context.Context, address string, options ...Option) (*http.Server, error)

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

func WithAddress(address string) Option

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

func WithLogger(logger *slog.Logger) Option

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

func WithMiddlewareLogLevel(level slog.Level) Option

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

func WithRoute(pattern string, handler http.Handler) Option

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

func WithRoutes(routes ...Route) Option

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

type Route

type Route struct {
	// GET /helloworld
	Pattern string
	Handler http.Handler
}

func NewRoute

func NewRoute(pattern string, handler http.Handler) Route

func (*Route) Path

func (r *Route) Path() string

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL