Documentation ¶
Overview ¶
Package web provides utility for working with HTTP requests, responses, and middleware.
Index ¶
- func Alert(sess *sessions.Session) template.Alert
- func CanAccessResource(db *sqlx.DB, name string, r *http.Request, get databaseFunc) (bool, error)
- func EncodeToLink(p database.Paginator, r *http.Request) string
- func HTMLError(w http.ResponseWriter, message string, status int)
- func JSONError(w http.ResponseWriter, message string, status int)
- func PageURL(url *url.URL, addr string, page int64) string
- type Gate
- type Handler
- func (h *Handler) Redirect(w http.ResponseWriter, r *http.Request, url string)
- func (h *Handler) RedirectBack(w http.ResponseWriter, r *http.Request)
- func (h *Handler) SaveMiddleware(handler http.Handler) http.Handler
- func (h *Handler) Session(r *http.Request) (*sessions.Session, func(*http.Request, http.ResponseWriter))
- func (h Handler) UserFromCookie(r *http.Request) (*user.User, bool, error)
- func (h Handler) UserFromRequest(w http.ResponseWriter, r *http.Request) (*user.User, bool, error)
- func (h Handler) UserFromToken(r *http.Request) (*user.User, bool, error)
- type Middleware
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Alert ¶
Alert returns the first alert that was flashed to the session. If no alert exists, then an empty alert is returned instead.
func CanAccessResource ¶
CanAccessResource returns whether the current user has access to the given resource. The resource's ID will be taken from the request based on the name, this is passed back to the databaseFunc which will return the underlying model for that resource. The name of the resource is also used to check against the permissions of that user.
func EncodeToLink ¶
EncodeToLink returns the given Paginator encoded to a Link header value using the given URL.
func HTMLError ¶
func HTMLError(w http.ResponseWriter, message string, status int)
HTMLError renders an HTML error page with the given message, and the given status code.
Types ¶
type Gate ¶
Gate serves as a stripped down middleware handler function that will be passed the current user in the request, if any, along with the request itself. This will determine whether the given user can access whatever is on the other end of the current endpoint, hence the bool return value.
type Handler ¶
type Handler struct { // DB is the current connection to the database. DB *sqlx.DB SMTP struct { Client *mail.Client From string } // Log is the log file that the HTTP server is writing to. Log *log.Logger // Queues is a set of different queues to dispatch background jobs to such // as webhooks, and downloading of remote images. Queues *queue.Set // Store is the current store being used for storing session data, such // as form fields, form errors, and general error messages. Store sessions.Store // Users is a pointer to the user.Store. This would typically be used for // getting the currently authenticated user from the database. Users *user.Store // Tokens is a pointer to the oauth2.TokenStore. This is used for retrieving // the current user from the authorization token in the request, if any. Tokens *oauth2.TokenStore // SecureCookie is what is used to encrypt the data we store inside the // request cookies. SecureCookie *securecookie.SecureCookie }
Handler is the main type for embedding into request handlers.
func (*Handler) Redirect ¶
Redirect redirects to the given URL, and saves the session in the process.
func (*Handler) RedirectBack ¶
func (h *Handler) RedirectBack(w http.ResponseWriter, r *http.Request)
RedirectBack redirects to the Referer of the given request.
func (*Handler) SaveMiddleware ¶
SaveMiddleware will save any data put in the session before serving the next request. Useful if any session data is flashed from within a gate.
func (*Handler) Session ¶
func (h *Handler) Session(r *http.Request) (*sessions.Session, func(*http.Request, http.ResponseWriter))
Session returns the session for the current request, and a callback for saving the returned session.
func (Handler) UserFromCookie ¶
func (Handler) UserFromRequest ¶
Get the currently authenticated user from the request. Check for token auth first, then fallback to cookie.
type Middleware ¶
type Middleware struct {
Handler
}
func (Middleware) Auth ¶
func (h Middleware) Auth(next http.Handler) http.Handler
Auth redirects the user back to /login if they're not authenticated, however it let's them continue if they are, and set's the user in the request context.
func (Middleware) Gate ¶
func (h Middleware) Gate(gates ...Gate) mux.MiddlewareFunc
Gate returns a mux.MiddlewareFunc that when called will iterate over the given gates to determine if the user can access the next request.