Documentation
¶
Overview ¶
Package server provides HTTP server functionality with routing capabilities, middleware support, and session management built on top of standard Go net/http. It offers a simple API for building web applications with clean routing, middleware chains, and error handling.
Index ¶
- func Error(w http.ResponseWriter, err error, HTTPStatus int)
- func Errorf(w http.ResponseWriter, HTTPStatus int, message string, args ...any)
- func InCtxMiddleware(key string, value interface{}) func(http.Handler) http.Handler
- func New(options ...Option) *mux
- func Valuer(ctx context.Context) valueReader
- type Middleware
- type Option
- type Router
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Error ¶
func Error(w http.ResponseWriter, err error, HTTPStatus int)
Error writes an HTTP error response, logging the error message. Unlike http.Error, this function determines the Content-Type dynamically depending on whether a message is found in the errorMessageMap for the given HTTPStatus. If no error message is registered, it defaults to the error's message content type.
func Errorf ¶
func Errorf(w http.ResponseWriter, HTTPStatus int, message string, args ...any)
Errorf is a convenient function to write an HTTP error response with a formatted message it under the hood uses fmt.Errorf and then calls Error to write the response and log the error.
func InCtxMiddleware ¶
InCtxMiddleware allows to specify a key/value that should be set on each request context. This is useful for services that could be used by the handlers.
Types ¶
type Middleware ¶
Middleware is a function that receives a http.Handler and returns a http.Handler that can be used to wrap the original handler with some functionality.
type Option ¶
type Option func(*mux)
Option for the server
func WithAssets ¶
WithAssets allows to serve embedded assets from a given embedded filesystem at a specified serving path. It sets up three context values: "assetManager", "assetPath", and "importMap". These values can be used by other components to manage and serve assets efficiently.
func WithErrorMessage ¶
func WithHost ¶
WithHost allows to specify the host to run the server at if not specified it defaults to 0.0.0.0
type Router ¶
type Router interface {
// Use allows to specify a middleware that should be executed for all the handlers
Use(middleware ...Middleware)
// ResetMiddleware clears the list of middleware on the router by setting the baseMiddleware.
ResetMiddleware()
// Handle allows to register a new handler for a specific pattern
Handle(pattern string, handler http.Handler)
// HandleFunc allows to register a new handler function for a specific pattern
HandleFunc(pattern string, handler http.HandlerFunc)
// Folder allows to serve static files from a directory
Folder(prefix string, fs fs.FS)
// Group allows to create a new group of routes with a common prefix
Group(prefix string, fn func(Router))
}
Router is the interface that wraps the basic methods for a router