Documentation ¶
Index ¶
- func Logger() func(next http.Handler) http.Handler
- type Error
- type Etager
- type FlashMessage
- type LastModer
- type Message
- type PageLink
- type Pagination
- type PaginationForm
- type Server
- func (s *Server) AbsoluteURL(r *http.Request, parts ...string) *url.URL
- func (s *Server) AddFlash(w http.ResponseWriter, r *http.Request, typ, msg string) error
- func (s *Server) AddRoute(pattern string, handler http.Handler)
- func (s *Server) AssetURL(r *http.Request, name string) string
- func (s *Server) AuthenticatedRouter() chi.Router
- func (s *Server) BindQueryString(r *http.Request, dst interface{}) *Message
- func (s *Server) Csrf() func(next http.Handler) http.Handler
- func (s *Server) CurrentPath(r *http.Request) string
- func (s *Server) Error(w http.ResponseWriter, r *http.Request, err error)
- func (s *Server) ErrorPages(next http.Handler) http.Handler
- func (s *Server) Flashes(r *http.Request) []FlashMessage
- func (s *Server) GetPageParams(r *http.Request) (*PaginationForm, *form.Form)
- func (s *Server) GetReqID(r *http.Request) string
- func (s *Server) GetSession(r *http.Request) *sessions.Session
- func (s *Server) InitRequest(next http.Handler) http.Handler
- func (s *Server) InitSession() error
- func (s *Server) IsTurboRequest(r *http.Request) bool
- func (s *Server) ListenAndServe() error
- func (s *Server) LoadJSON(r *http.Request, dst interface{}) *Message
- func (s *Server) Log(r *http.Request) *log.Entry
- func (s *Server) Message(w http.ResponseWriter, r *http.Request, message *Message)
- func (s *Server) NewPagination(r *http.Request, count, limit, offset int) Pagination
- func (s *Server) Redirect(w http.ResponseWriter, r *http.Request, ref ...string)
- func (s *Server) Render(w http.ResponseWriter, r *http.Request, status int, value interface{})
- func (s *Server) RenderTemplate(w http.ResponseWriter, r *http.Request, status int, name string, ctx TC)
- func (s *Server) RenderTurboStream(w http.ResponseWriter, r *http.Request, name, action, target string, ...)
- func (s *Server) SendPaginationHeaders(w http.ResponseWriter, r *http.Request, count, limit, offset int)
- func (s *Server) SetSecurityHeaders() func(next http.Handler) http.Handler
- func (s *Server) Status(w http.ResponseWriter, _ *http.Request, status int)
- func (s *Server) TextMessage(w http.ResponseWriter, r *http.Request, status int, msg string)
- func (s *Server) Validate(data interface{}) *Message
- func (s *Server) WithCaching(next http.Handler) http.Handler
- func (s *Server) WithPermission(act string) func(next http.Handler) http.Handler
- func (s *Server) WithSession() func(next http.Handler) http.Handler
- func (s *Server) WriteEtag(w http.ResponseWriter, i Etager)
- func (s *Server) WriteLastModified(w http.ResponseWriter, i LastModer)
- type TC
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Etager ¶
type Etager interface {
GetSumStrings() []string
}
Etager must provides a function that returns a list of strings used to build an etag header.
type FlashMessage ¶
FlashMessage contains a message type and content.
type LastModer ¶
LastModer must provides a function that returns a list of times used to build a Last-Modified header.
type Message ¶
type Message struct { Status int `json:"status"` Message string `json:"message"` Errors []Error `json:"errors,omitempty"` }
Message is used by the server's Message() method.
type Pagination ¶
type Pagination struct { URL *url.URL Limit int Offset int TotalCount int TotalPages int CurrentPage int First int Last int Next int Previous int FirstPage string LastPage string NextPage string PreviousPage string PageLinks []PageLink }
Pagination holds all the information regarding pagination
func (Pagination) GetLink ¶
func (p Pagination) GetLink(offset int) string
GetLink returns a new url string with limit and offset values
func (Pagination) GetPageLinks ¶
func (p Pagination) GetPageLinks() []PageLink
GetPageLinks returns the links that can be used in a template.
type PaginationForm ¶
PaginationForm is a default form for pagination
func (*PaginationForm) Validate ¶
func (pf *PaginationForm) Validate(f *form.Form)
Validate validates the PaginationForm values
type Server ¶
type Server struct { Router *chi.Mux BasePath string }
Server is a wrapper around chi router.
func (*Server) AbsoluteURL ¶
AbsoluteURL resolve the absolute URL for the given ref path parts. If the ref starts with "./", it will resolve relative to the current URL.
func (*Server) AuthenticatedRouter ¶
func (s *Server) AuthenticatedRouter() chi.Router
AuthenticatedRouter returns a chi.Router instance with middlewares to force authentication.
func (*Server) BindQueryString ¶
BindQueryString binds the request's query string with the given pointer to a struct of data. If the destination implements Validate(), it runs the validation as well.
func (*Server) CurrentPath ¶
CurrentPath returns the path of the current request after striping the server's base path. This value can later be used in the AbsoluteURL or Redirect functions.
func (*Server) ErrorPages ¶
ErrorPages is a middleware that overrides the response writer so that, under some conditions, it can send a response matching the "accept" request header.
Conditions are: response status must be >= 400, its content-type is text/plain and it has some content.
func (*Server) Flashes ¶
func (s *Server) Flashes(r *http.Request) []FlashMessage
Flashes returns the flash messages retrieved from the session store in the session middleware.
func (*Server) GetPageParams ¶
GetPageParams returns the pagination parameters from the query string
func (*Server) GetSession ¶
GetSession returns the session currently stored in context. It will panic (on purpose) if the route is not using the WithSession() middleware.
func (*Server) InitRequest ¶
InitRequest update the scheme and host on the incoming HTTP request URL (r.URL), based on provided headers and/or current environnement.
It also checks the validity of the host header when the server is not running in dev mode.
func (*Server) InitSession ¶
InitSession creates the session store.
func (*Server) IsTurboRequest ¶
IsTurboRequest returns true when the request was made with an x-turbo header.
func (*Server) ListenAndServe ¶
ListenAndServe starts the HTTP server
func (*Server) LoadJSON ¶
LoadJSON loads the JSON payload from the request body to the destination variable. If the destination implements Validate(), it runs the validation as well.
func (*Server) NewPagination ¶
func (s *Server) NewPagination(r *http.Request, count, limit, offset int) Pagination
NewPagination creates a new Pagination instance base on the current request.
func (*Server) Redirect ¶
Redirect yields a 303 redirection with a location header. The given "ref" values are joined togegher with the server's base path to provide a full absolute URL.
func (*Server) RenderTemplate ¶
func (s *Server) RenderTemplate(w http.ResponseWriter, r *http.Request, status int, name string, ctx TC)
RenderTemplate yields an HTML response using the given template and context.
func (*Server) RenderTurboStream ¶
func (s *Server) RenderTurboStream( w http.ResponseWriter, r *http.Request, name, action, target string, ctx interface{}, )
RenderTurboStream yields an HTML response with turbo-stream content-type using the given template and context. The template result is enclosed in a turbo-stream tag with action and target as specified. You can call this method as many times as needed to output several turbo-stream tags in the same HTTP response.
func (*Server) SendPaginationHeaders ¶
func (s *Server) SendPaginationHeaders( w http.ResponseWriter, r *http.Request, count, limit, offset int, )
SendPaginationHeaders compute and set the pagination headers
func (*Server) SetSecurityHeaders ¶
SetSecurityHeaders adds some headers to improve client side security.
func (*Server) TextMessage ¶
TextMessage sends a JSON formatted message response with a status and a message.
func (*Server) Validate ¶
Validate runs the validation on a given destination data and returns a formatted message with the encountered errors, if any.
func (*Server) WithCaching ¶
WithCaching is a middleware that checks if an Etag and/or a Last-Modified headers are sent with the response. If the request has the correspondign cache header and theys match the request stops with a 304.
func (*Server) WithPermission ¶
WithPermission enforce a permission check on the request's path for the given action.
In the RBAC configuration, the user's group is the subject, the request's path is the object and "act" is the action.
func (*Server) WithSession ¶
WithSession initialize a session store that will be available on the included routes.
func (*Server) WriteEtag ¶
func (s *Server) WriteEtag(w http.ResponseWriter, i Etager)
WriteEtag adds an Etag header to the response, based on the values sent by GetSumStrings. The build date is always included.
func (*Server) WriteLastModified ¶
func (s *Server) WriteLastModified(w http.ResponseWriter, i LastModer)
WriteLastModified adds a Last-Modified headers using the most recent date of GetLastModified and the build date.