Documentation
¶
Index ¶
- func BasicHttp(msg string) http.Handler
- func BasicJson(msg string) http.Handler
- func CacheControl(next http.Handler) http.Handler
- func CacheControlWithAge(age int) func(http.Handler) http.Handler
- func FileServer(fsys fs.FS) http.Handler
- func FileServerDir(root, prefix string) http.Handler
- func Http200(w http.ResponseWriter, msg string)
- func Http301(w http.ResponseWriter, msg string)
- func Http400(w http.ResponseWriter, msg string)
- func Http401(w http.ResponseWriter, msg string)
- func Http403(w http.ResponseWriter, msg string)
- func Http404(w http.ResponseWriter, msg string)
- func Http500(w http.ResponseWriter)
- func Json200(w http.ResponseWriter, data any) error
- func Json201(w http.ResponseWriter, data any) error
- func Json204(w http.ResponseWriter, data any) error
- func Json301(w http.ResponseWriter, data any) error
- func Json400(w http.ResponseWriter, data any) error
- func Json401(w http.ResponseWriter, data any) error
- func Json403(w http.ResponseWriter, data any) error
- func Json404(w http.ResponseWriter, data any) error
- func Json500(w http.ResponseWriter) error
- func LogDebug(msg string, attrs ...slog.Attr)
- func LogError(err error, attrs ...slog.Attr)
- func LogInfo(msg string, attrs ...slog.Attr)
- func LogRequest(next http.Handler) http.Handler
- func LogWarn(msg string, attrs ...slog.Attr)
- func Logger(l *slog.Logger)
- func MakeHandler(next HandlerFunc) http.Handler
- func NewLogger(w io.Writer) *slog.Logger
- func NotFound(next http.Handler) http.Handler
- func NotFoundJson(next http.Handler) http.Handler
- func RecoverPanic(next http.Handler) http.Handler
- func Render(w http.ResponseWriter, page string, status int, data any) error
- func SecureHeaders(next http.Handler) http.Handler
- func Serve(addr string, handler http.Handler) error
- func Templates(templatesFS fs.FS, opts ...ViewOpt)
- func WrapItem[T any](val T) func() T
- func WrapMap[T any](val map[string]T) func() map[string]T
- func WrapSlice[T any](val []T) func() []T
- type AppError
- type HandlerFunc
- type Server
- func (s *Server) Handle(pattern string, handler http.Handler)
- func (s *Server) HandleFunc(pattern string, handler func(http.ResponseWriter, *http.Request))
- func (s *Server) Handler() http.Handler
- func (s *Server) Serve(addr string) error
- func (s *Server) Use(middleware ...func(http.Handler) http.Handler)
- type View
- type ViewOpt
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func BasicHttp ¶
BasicHttp is an http handler which serves a custom message with a status of 200 OK.
mux.Handle("/", BasicHttp("hi"))
.
func BasicJson ¶
BasicJson is an http handler which serves a custom json message with a status of 200 OK.
mux.Handle("/", BasicJson("hi"))
.
func CacheControl ¶
CacheControl is a middleware which sets the caching policy for assets. Defaults to 2 days.
func CacheControlWithAge ¶
CacheControlWithAge is a middleware which sets the caching policy for assets.
func FileServer ¶
FileServer is an http handler which serves files from the given file system.
mux.Handle("/static/", FileServer(fsys))
.
func FileServerDir ¶
FileServerDir is an http handler which serves files from the file system. Root is the filesystem root. Prefix is the prefix of the request path to strip off before searching the filesystem for the given file.
mux.Handle("/static/", FileServerDir("./staticfiles", "/static/"))
.
func Http200 ¶
func Http200(w http.ResponseWriter, msg string)
func Http301 ¶
func Http301(w http.ResponseWriter, msg string)
func Http400 ¶
func Http400(w http.ResponseWriter, msg string)
func Http401 ¶
func Http401(w http.ResponseWriter, msg string)
func Http403 ¶
func Http403(w http.ResponseWriter, msg string)
func Http404 ¶
func Http404(w http.ResponseWriter, msg string)
func Http500 ¶
func Http500(w http.ResponseWriter)
func Json500 ¶
func Json500(w http.ResponseWriter) error
func LogRequest ¶
Logger is a middleware which logs the http request and response status.
func MakeHandler ¶
func MakeHandler(next HandlerFunc) http.Handler
MakeHandler is a middleware which converts a rio.HandlerFunc to an http.Handler. It centralizes the error handling with the custom AppError error type.
func NotFound ¶
NotFound is a middleware which returns a 404 Not Found error if the request path is not "/".
func NotFoundJson ¶
NotFound is a middleware which returns a 404 Not Found json error if the request path is not "/".
func RecoverPanic ¶
RecoverPanic is a middleware which recovers from panics and logs a HTTP 500 (Internal Server Error) if possible.
func SecureHeaders ¶
SecureHeaders is a middleware which adds HTTP security headers to every response, inline with current OWASP guidance.
func Serve ¶
Serve starts an http server.
The addr is the address to listen to. The addr assumes the format "host:port". The handler is the http.Handler to serve.
func Templates ¶
Templates configures the default view with the given templateFS filesystem and the opts functional options.
func WrapItem ¶ added in v0.0.3
func WrapItem[T any](val T) func() T
WrapItem wraps a value of any type in a func. This is used to inject values into the template.FuncMap.
Types ¶
type AppError ¶
AppError is custom error type for Http/Json errors.
type HandlerFunc ¶
type HandlerFunc func(http.ResponseWriter, *http.Request) error
HandlerFunc is a custom http handler signature which accepts an http.ResponseWriter, *http.Request and returns an error. HandlerFuncs must be converted into an http.Handler with the MakeHandler middleware.
type Server ¶
type Server struct {
// contains filtered or unexported fields
}
Server is a wrapper around the standard http.ServeMux.
It can register routes for Handlers and HandlerFuncs. It can also register middleware for the entire ServeMux.
func NewServer ¶
func NewServer() *Server
NewServer constructs and returns a new *Server.
The underlying mux is the standard http.ServeMux. The LogRequest, RecoverPanic and SecureHeaders middleware are automatically registered on construction.
func (*Server) Handle ¶
Handler registers the handler for the given pattern. It is a proxy for http.ServeMux.Handle().
func (*Server) HandleFunc ¶
HandleFunc registers the handler function for the given pattern. It is a proxy for http.ServeMux.HandleFunc().
func (*Server) Handler ¶
Handler returns the Server as an http.Handler.
It wraps the ServeMux with the middleware handlers, and returns the result as an http.Handler.
The middleware is reversed, so that the earliest registered middleware is wrapped last.
type View ¶
type View struct {
// contains filtered or unexported fields
}
View is a collection of html templates for rendering.
type ViewOpt ¶
type ViewOpt func(*View)
ViewOpt is a function to configure a View.
func WithFuncMap ¶
WithFuncMap adds the functions in the given funcMap to the View.
If the View contains a funcMap function with the same name, then it will not be added.
Source Files
¶
Directories
¶
Path | Synopsis |
---|---|
Package format implements functions to format numbers, dates, times and strings.
|
Package format implements functions to format numbers, dates, times and strings. |
Package forms implements utilities to parse and validate data.
|
Package forms implements utilities to parse and validate data. |
internal
|
|