Documentation
¶
Index ¶
- Variables
- func Duration(req *http.Request) time.Duration
- func ID(req *http.Request) string
- func Logger(req *http.Request) *slog.Logger
- func Var(req *http.Request, key string) string
- func Vars(req *http.Request) map[string]string
- type Application
- type Config
- type ErrorHandlerFunc
- type Middleware
- type Middlewares
- type Route
- type Server
Constants ¶
This section is empty.
Variables ¶
var ( // ErrMethodNotAllowed is used for requests with a method that's not allowed by a route. ErrMethodNotAllowed = errors.New("luci: method not allowed") // ErrNotFound is used for requests with a path that doesn't match any routes. ErrNotFound = errors.New("luci: not found") // ErrForcedShutdown is used when server shutdown takes longer than the configured timeout. ErrForcedShutdown = errors.New("luci: forced server shutdown") )
Functions ¶
func Duration ¶
Duration returns the duration of the request from the time it was started to the time Duration was called.
Types ¶
type Application ¶
type Application interface { // Routes defines the routes an application supports. Routes() []Route // Middlewares defines the middlewares to run before any route specific middlewares. Middlewares() Middlewares // Error defines how the application responds to errors when handling a request. Error(http.ResponseWriter, *http.Request, int, error) // Respond defines how the application reponds to requests that are successful. Respond(http.ResponseWriter, *http.Request, any) }
Application is used to define a server application and it's request/response behavior.
type Config ¶
type Config struct { // Address defines the address to listen on. Address string // RouteTimeout defines the default timeout duration for defined routes. RouteTimeout time.Duration // ReadHeaderTimeout defines the timeout to read request headers. // See net/http.Server.ReadHeaderTimeout for details. ReadHeaderTimeout time.Duration // ShutdownTimeout defines the timeout for the server to gracefully // shutdown on context cancellation. ShutdownTimeout time.Duration // Logger defines the logger the server uses when logging startup/shutdown/requests. Logger *slog.Logger }
Config defines how a server should behave when it's running. See DefaultConfig for configuration defaults.
type ErrorHandlerFunc ¶
ErrorHandlerFunc is used to define functions that handle error specific responses.
type Middleware ¶
Middleware defines functionality that runs before an endpoint handler.
func WithValue ¶
func WithValue(key, value any) Middleware
WithValue is a middleware that adds the key/value to the request context.
type Middlewares ¶
type Middlewares []Middleware
Middlewares defines a list of ordered middleware to run before an endpoint handler.
func (Middlewares) Handler ¶
func (middlewares Middlewares) Handler(next http.Handler) http.Handler
Handler creates a handler that runs the middlewares in the defined order and then calls the given handler.
func (Middlewares) HandlerFunc ¶
func (middlewares Middlewares) HandlerFunc(next http.HandlerFunc) http.Handler
Handler creates a handler that runs the middlewares in the defined order and then calls the given handler function.
type Route ¶
type Route struct { // Name is used to uniquely identify a route by name. Name string // Timeout defines the timeout for a request to a route. // Once the timeout has been reached a timeout response is // sent and the request context is cancelled. If Timeout is // not set the default RouteTimeout will be used instead. Timeout time.Duration // Method may be optionally used to specify the method a route supports. // If not set the route will be used for all methods. Method string // Defines the pattern that the route should match on. // Refer to github.com/go-chi/chi/v5 for details on defining patterns. Pattern string // Middlewares define the route specific middlewares to run after the application middlewares. Middlewares Middlewares // HandlerFunc defines the handler function to call to handle the request. HandlerFunc http.HandlerFunc }
Route defines an endpoint an application supports.
func RequestRoute ¶
RequestRoute retrieves the route that's associated with the given request.
type Server ¶
type Server struct {
// contains filtered or unexported fields
}
Server maintains the running state of an application.
func NewServer ¶
func NewServer(config Config, app Application) *Server
NewServer creates a server for the given application using the given configuration. NewServer panics if any route does not have a name, the name is not unique, or if the route doesn't have a handler defined.
func (*Server) Address ¶
Address can be used to retrieve the address the server is listening on. Address blocks until the server has begun listening on the address.
func (*Server) ListenAndServe ¶
ListenAndServe listens on the configured address and serves requests until the given context has been cancelled. ListenAndServe will gracefully shutdown on context cancellation up until the configured shutdown timeout has been reached, if the shutdown timeout is reached ErrForcedShutdown is returned.