Documentation
¶
Overview ¶
Package server is the main Readeck HTTP server. It defines common middlewares, guards, permission handlers, etc.
Index ¶
- func GetCSPHeader(r *http.Request) csp.Policy
- func GetTemplate(name string) (*jet.Template, error)
- func Logger() func(next http.Handler) http.Handler
- type Error
- type Etager
- type LastModer
- type Link
- 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(middlewares ...func(http.Handler) http.Handler) chi.Router
- func (s *Server) CannonicalPaths(next http.Handler) http.Handler
- func (s *Server) CompressResponse(next http.Handler) http.Handler
- func (s *Server) Csrf(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) []sessions.FlashMessage
- func (s *Server) GetPageParams(r *http.Request, defaultLimit int) *PaginationForm
- func (s *Server) GetPaginationLinks(r *http.Request, p Pagination) []Link
- func (s *Server) GetReqID(r *http.Request) string
- func (s *Server) GetSession(r *http.Request) *sessions.Session
- func (s *Server) Init()
- func (s *Server) InitRequest(next http.Handler) http.Handler
- func (s *Server) InitSession() (err error)
- func (s *Server) IsTurboRequest(r *http.Request) bool
- func (s *Server) LoadLocale(next http.Handler) http.Handler
- func (s *Server) Locale(r *http.Request) *locales.Locale
- func (s *Server) Log(r *http.Request) *slog.Logger
- 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) RenewCsrf(w http.ResponseWriter, r *http.Request)
- func (s *Server) SendPaginationHeaders(w http.ResponseWriter, r *http.Request, p Pagination)
- func (s *Server) SetSecurityHeaders(next http.Handler) http.Handler
- func (s *Server) Status(w http.ResponseWriter, _ *http.Request, status int)
- func (s *Server) TemplateVars(r *http.Request) jet.VarMap
- func (s *Server) TextMessage(w http.ResponseWriter, r *http.Request, status int, msg string)
- func (s *Server) WithCacheControl(next http.Handler) http.Handler
- func (s *Server) WithCaching(next http.Handler) http.Handler
- func (s *Server) WithCustomErrorTemplate(status int, template string) func(next http.Handler) http.Handler
- func (s *Server) WithPermission(obj, act string) func(next http.Handler) http.Handler
- func (s *Server) WithRedirectLogin(next http.Handler) http.Handler
- func (s *Server) WithSession() func(next http.Handler) http.Handler
- func (s *Server) WriteEtag(w http.ResponseWriter, r *http.Request, taggers ...Etager)
- func (s *Server) WriteLastModified(w http.ResponseWriter, r *http.Request, moders ...LastModer)
- type TC
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func GetCSPHeader ¶
GetCSPHeader extracts the current CSPHeader from the request's context.
func GetTemplate ¶
GetTemplate returns a template from the current views.
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 LastModer ¶
LastModer must provides a function that returns a list of times used to build a Last-Modified header.
type Link ¶
Link contains a "Link" header information.
func (Link) Write ¶
func (l Link) Write(w http.ResponseWriter)
Write adds the header to a ResponseWriter.
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) Limit ¶
func (f *PaginationForm) Limit() int
Limit returns the current limit or zero if none was given.
func (*PaginationForm) Offset ¶
func (f *PaginationForm) Offset() int
Offset returns the current offset or 0 if none was given.
func (*PaginationForm) SetLimit ¶
func (f *PaginationForm) SetLimit(v int)
SetLimit sets the limit's value. It's used to set a default limit before binding the form.
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 ¶
AuthenticatedRouter returns a chi.Router instance with middlewares to force authentication.
func (*Server) CannonicalPaths ¶
CannonicalPaths cleans the URL path and removes trailing slashes. It returns a 308 redirection so any form will pass through.
func (*Server) CompressResponse ¶
CompressResponse returns a gzipped response for some content types. It uses gzhttp that provides a BREACH mittigation.
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) []sessions.FlashMessage
Flashes returns the flash messages retrieved from the session in the session middleware.
func (*Server) GetPageParams ¶
func (s *Server) GetPageParams(r *http.Request, defaultLimit int) *PaginationForm
GetPageParams returns the pagination parameters from the query string.
func (*Server) GetPaginationLinks ¶
func (s *Server) GetPaginationLinks(r *http.Request, p Pagination) []Link
GetPaginationLinks returns a list of Link instances suitable for pagination.
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 handler.
func (*Server) IsTurboRequest ¶
IsTurboRequest returns true when the request was made with an x-turbo header.
func (*Server) LoadLocale ¶
LoadLocale is a middleware that loads the correct locale for the current user. It defaults to English if no user is connected or no language is set.
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{}, attrs map[string]string, )
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) RenewCsrf ¶
func (s *Server) RenewCsrf(w http.ResponseWriter, r *http.Request)
RenewCsrf generate a new CSRF protection token.
func (*Server) SendPaginationHeaders ¶
func (s *Server) SendPaginationHeaders( w http.ResponseWriter, r *http.Request, p Pagination, )
SendPaginationHeaders compute and set the pagination headers.
func (*Server) SetSecurityHeaders ¶
SetSecurityHeaders adds some headers to improve client side security.
func (*Server) TemplateVars ¶
TemplateVars returns the default variables set for a template in the request's context.
func (*Server) TextMessage ¶
TextMessage sends a JSON formatted message response with a status and a message.
func (*Server) WithCacheControl ¶
WithCacheControl sends the global caching headers.
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) WithCustomErrorTemplate ¶
func (s *Server) WithCustomErrorTemplate(status int, template string) func(next http.Handler) http.Handler
WithCustomErrorTemplate registers a custom template for an error rendered as HTML. It must be set before any middleware that would trigger an HTTP error.
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 given "obj" is the object and "act" is the action.
func (*Server) WithRedirectLogin ¶
WithRedirectLogin sets the unauthorized handler to redirect to the login page.
func (*Server) WithSession ¶
WithSession initialize a session handler that will be available on the included routes.
func (*Server) WriteEtag ¶
WriteEtag adds an Etag header to the response, based on the values sent by GetSumStrings. The build date is always included.
func (*Server) WriteLastModified ¶
WriteLastModified adds a Last-Modified headers using the most recent date of GetLastModified and the build date.