lib

package
v0.0.0-...-474eaa9 Latest Latest
Warning

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

Go to latest
Published: Dec 27, 2023 License: AGPL-3.0 Imports: 10 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
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

func FuncErrHandler(handler Handler) http.Handler

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

func ServeStatic(
	subdirectory string,
	fs embed.FS,
) http.Handler

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

type ErrMiddleware = func(Handler) Handler

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

type Middleware = func(http.Handler) http.Handler

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.

func (NoopHandler) Enabled

Enabled returns a boolean value indicating whether logging is enabled for the given context and log level. It always returns false for the NoopLogger implementation.

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

func (r Route) Mount(router chi.Router)

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.

type Routes

type Routes []Route

Routes represents a collection of individual [Route]s.

func (Routes) Router

func (r Routes) Router() chi.Router

Router returns a chi.Router that is configured with the routes defined in the Routes slice. It iterates over each route in the Routes slice and mounts them onto the router. The configured router is then returned.

Jump to

Keyboard shortcuts

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