Documentation
¶
Index ¶
- Variables
- func ErrHandler(w http.ResponseWriter, r *http.Request, err error)
- func FuncErrHandler(handler Handler) http.Handler
- func ServeStatic(subdirectory string, fs embed.FS) http.Handler
- func ServeTemplateHTML(w http.ResponseWriter, path string, bind any) error
- type App
- type ErrMiddleware
- type Handler
- type Middleware
- type NoopHandler
- type Route
- type Routes
Constants ¶
This section is empty.
Variables ¶
var NoopLogger = slog.New(&NoopHandler{})
NoopLogger is a logger that discards all log messages.
Functions ¶
func ErrHandler ¶
func ErrHandler(w http.ResponseWriter, r *http.Request, err error)
ErrHandler handles errors by writing an HTTP 500 status code and the error message to the response. It takes in the response writer, the request, and the error as parameters. If the error is not nil, it writes the error message to the response writer.
func FuncErrHandler ¶
FuncErrHandler converts a Handler into a http.Handler, by wrapping it with a function that handles errors and converts them to logged errors, and 500 HTTP responses.
func ServeStatic ¶
ServeStatic returns an http.Handler that serves static files on the specified path prefix, under the provided subdirectory, using the provided embed.FS.
func ServeTemplateHTML ¶
func ServeTemplateHTML(w http.ResponseWriter, path string, bind any) error
ServeTemplateHTML renders a template based on the request path and binds it with the provided data. It takes a fiber.Ctx object and a bind parameter as input. The request path is used to determine the template to render. The bind parameter is used to bind data to the template. It returns an error if there is any issue rendering the template.
Types ¶
type App ¶
type App interface { // DeclareRoutes declares the routes for the application. DeclareRoutes() Routes // Close closes the application. Close() error }
App represents an application interface.
type ErrMiddleware ¶
ErrMiddleware adds error handling to the Middleware type.
func LogErrors ¶
func LogErrors(logger *slog.Logger) ErrMiddleware
LogErrors is a middleware function that logs any errors that occur during request handling. The modified Handler function logs any errors that occur during request handling using the provided logger. If an error occurs, it is passed to the logger.ErrorContext method along with additional context information. The original error is then returned.
type Handler ¶
type Handler func(w http.ResponseWriter, r *http.Request) error
Handler represents a function that handles HTTP requests. It takes in a http.ResponseWriter and a http.Request as parameters, and returns an error if any occurred during the handling process.
type Middleware ¶
Middleware represents a function that wraps an http.Handler with additional functionality.
func AddPrefix ¶
func AddPrefix(prefix string) Middleware
AddPrefix is a middleware that adds a prefix to the URL path of incoming requests. It takes a prefix string as input and returns a Middleware function.
func StripPrefix ¶
func StripPrefix(prefix string) Middleware
StripPrefix returns a middleware that strips the given prefix from the request URL path before passing it to the next handler. The prefix parameter specifies the prefix to be stripped. The returned middleware function takes an http.Handler as input and returns a new http.Handler that performs the prefix stripping.
type NoopHandler ¶
type NoopHandler struct {
slog.TextHandler
}
NoopHandler represents a logger handler implementation that does nothing.
type Route ¶
type Route struct { Method string Path string Handler Handler Middleware []Middleware }
Method represents the HTTP method of the route. Path represents the URL path of the route. Handler is the function that handles the route request. Middleware is an optional list of middleware functions that are applied to the route.
func (Route) Mount ¶
Mount mounts the Route onto the provided chi.Router. If the Route has no middleware, it directly registers the handler function with the specified method and path. If the Route has middleware, it creates a new router group, applies the middleware, and then registers the handler function with the specified method and path.