Documentation
¶
Overview ¶
Package ki is a thin wrapper around the standard Go mux.
Index ¶
- Variables
- func GetRequestID(ctx context.Context) string
- func MustGetLanguage(ctx context.Context) language.Tag
- func MustGetLogger(ctx context.Context) *slog.Logger
- func SetLanguage(ctx context.Context, lang language.Tag) context.Context
- func SetLogger(ctx context.Context, logger *slog.Logger) context.Context
- func SetLoggerLevelByText(s string)
- func SetRegistry(ctx context.Context, registry *Registry) context.Context
- func SetRequestID(ctx context.Context, requestID string) context.Context
- type BufferedResponseWriter
- func (w *BufferedResponseWriter) Flush() (int, error)
- func (w *BufferedResponseWriter) Header() http.Header
- func (w *BufferedResponseWriter) Size() int
- func (w *BufferedResponseWriter) StatusCode() int
- func (w *BufferedResponseWriter) Write(b []byte) (int, error)
- func (w *BufferedResponseWriter) WriteHeader(statusCode int)
- type Location
- func (l Location) Method() string
- func (l Location) Pattern() string
- func (l Location) URL() *url.URL
- func (l Location) WithPathParams(params ...string) Location
- func (l Location) WithPrefix(prefix string) Location
- func (l Location) WithQuery(query url.Values) Location
- func (l Location) WithQueryParam(key, value string) Location
- type Mux
- func (m *Mux) Delete(pattern string, handler http.HandlerFunc, options ...RouteOption) Location
- func (m *Mux) Get(pattern string, handler http.HandlerFunc, options ...RouteOption) Location
- func (m *Mux) Group(fn func(Router)) Router
- func (m *Mux) Method(method, pattern string, handler http.HandlerFunc, options ...RouteOption) Location
- func (m *Mux) Mount(prefix string, handler http.Handler)
- func (m *Mux) Patch(pattern string, handler http.HandlerFunc, options ...RouteOption) Location
- func (m *Mux) Post(pattern string, handler http.HandlerFunc, options ...RouteOption) Location
- func (m *Mux) Put(pattern string, handler http.HandlerFunc, options ...RouteOption) Location
- func (m *Mux) Registry() *Registry
- func (m *Mux) Route(prefix string, fn func(Router)) Router
- func (m *Mux) ServeHTTP(w http.ResponseWriter, r *http.Request)
- func (m *Mux) Use(middlewares ...func(http.Handler) http.Handler)
- type Registry
- type Route
- type RouteOption
- type Router
- type Stack
Constants ¶
This section is empty.
Variables ¶
var Logger *slog.Logger
Logger is the global logger.
Functions ¶
func GetRequestID ¶
GetRequestID returns the request ID from the context. Use with RequestID middleware.
func MustGetLanguage ¶
MustGetLanguage returns the language from the context. If the language is not set, it panics. Use with Language middleware.
func MustGetLogger ¶
MustGetLogger returns the logger from the context. If the logger is not set, it panics. Use with Logger middleware.
func SetLanguage ¶
SetLanguage sets the language in the context.
func SetLoggerLevelByText ¶
func SetLoggerLevelByText(s string)
SetLoggerLevelByText sets the logger level to the given text level. The text level must be one of the following: debug, info, warn, error.
func SetRegistry ¶
SetRegistry sets the registry in the context.
Types ¶
type BufferedResponseWriter ¶
type BufferedResponseWriter struct {
http.ResponseWriter
// contains filtered or unexported fields
}
BufferedResponseWriter is a response writer that buffers the response.
func NewBufferedResponseWriter ¶
func NewBufferedResponseWriter(w http.ResponseWriter) *BufferedResponseWriter
NewBufferedResponseWriter returns a new BufferedResponseWriter.
func (*BufferedResponseWriter) Flush ¶
func (w *BufferedResponseWriter) Flush() (int, error)
Flush writes the buffered response to the underlying response writer.
func (*BufferedResponseWriter) Header ¶
func (w *BufferedResponseWriter) Header() http.Header
Header returns the header of the response.
func (*BufferedResponseWriter) Size ¶
func (w *BufferedResponseWriter) Size() int
Size returns the size of the buffered response.
func (*BufferedResponseWriter) StatusCode ¶
func (w *BufferedResponseWriter) StatusCode() int
StatusCode returns the status code of the response.
func (*BufferedResponseWriter) Write ¶
func (w *BufferedResponseWriter) Write(b []byte) (int, error)
Write writes the given bytes to the buffer if the response has not been flushed or the response has already otherwise.
func (*BufferedResponseWriter) WriteHeader ¶
func (w *BufferedResponseWriter) WriteHeader(statusCode int)
WriteHeader sets the status code of the response.
type Location ¶
type Location struct {
// contains filtered or unexported fields
}
Location represents a resolved route.
func GetLocation ¶
GetLocation returns the location for the given key from the registry in the context. Use with Locator middleware.
func NewLocation ¶
NewLocation returns a new Location.
func (Location) WithPathParams ¶
WithPathParams returns a new Location with the path parameters.
func (Location) WithPrefix ¶
WithPrefix returns a new Location with the prefix.
func (Location) WithQueryParam ¶
WithQueryParam returns a new Location with the query parameter.
type Mux ¶
type Mux struct {
// contains filtered or unexported fields
}
Mux is a router that uses a ServeMux.
func (*Mux) Delete ¶
func (m *Mux) Delete(pattern string, handler http.HandlerFunc, options ...RouteOption) Location
Delete adds a route for the DELETE verb.
func (*Mux) Get ¶
func (m *Mux) Get(pattern string, handler http.HandlerFunc, options ...RouteOption) Location
Get adds a route for the GET verb.
func (*Mux) Method ¶
func (m *Mux) Method(method, pattern string, handler http.HandlerFunc, options ...RouteOption) Location
Method adds a route for the given verb.
func (*Mux) Patch ¶
func (m *Mux) Patch(pattern string, handler http.HandlerFunc, options ...RouteOption) Location
Patch adds a route for the PATCH verb.
func (*Mux) Post ¶
func (m *Mux) Post(pattern string, handler http.HandlerFunc, options ...RouteOption) Location
Post adds a route for the POST verb.
func (*Mux) Put ¶
func (m *Mux) Put(pattern string, handler http.HandlerFunc, options ...RouteOption) Location
Put adds a route for the PUT verb.
type Registry ¶
type Registry struct {
// contains filtered or unexported fields
}
Registry is a registry of routes.
func (*Registry) Get ¶
Get returns the location for the given key. It panics if the location does not exist.
type Route ¶
type Route struct {
// contains filtered or unexported fields
}
Route represents a route.
func NewRoute ¶
func NewRoute(method, path string, handler http.Handler, options ...RouteOption) Route
NewRoute returns a new Route.
type RouteOption ¶
type RouteOption func(*Route)
RouteOption is a function that configures a Route.
func WithMiddleware ¶
func WithMiddleware(middlewares ...func(http.Handler) http.Handler) RouteOption
WithMiddleware returns a new RouteOption that sets the middlewares for the route.
func WithName ¶
func WithName(name string) RouteOption
WithName returns a new RouteOption that sets the name of the route.
type Router ¶
type Router interface {
ServeHTTP(w http.ResponseWriter, req *http.Request)
// Mount mounts the given handler at the given prefix.
Mount(prefix string, handler http.Handler)
// Route creates a new router with the given prefix.
Route(prefix string, fn func(Router)) Router
// Group creates a new router without any prefix.
// It is useful for adding middlewares to a group of routes.
Group(fn func(Router)) Router
// Use adds the given middlewares to the router.
Use(middlewares ...func(http.Handler) http.Handler)
// Method adds a route for the given verb.
Method(method, pattern string, handler http.HandlerFunc, options ...RouteOption) Location
// HTTP routing methods.
Get(pattern string, handler http.HandlerFunc, options ...RouteOption) Location
Post(pattern string, handler http.HandlerFunc, options ...RouteOption) Location
Put(pattern string, handler http.HandlerFunc, options ...RouteOption) Location
Patch(pattern string, handler http.HandlerFunc, options ...RouteOption) Location
Delete(pattern string, handler http.HandlerFunc, options ...RouteOption) Location
// Registry returns the registry of the router.
Registry() *Registry
}
Router is a router. It provides a way to mount handlers, define routes, and use middlewares.
Source Files
¶
Directories
¶
| Path | Synopsis |
|---|---|
|
examples
|
|
|
basic
command
|
|
|
named_route
command
|
|
|
routing
command
|
|
|
Package middlewares provides middlewares for the ki router.
|
Package middlewares provides middlewares for the ki router. |