web

package
v1.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 25, 2021 License: Apache-2.0 Imports: 22 Imported by: 0

Documentation

Overview

Package web provides utility for working with HTTP requests, responses, and middleware.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Alert

func Alert(sess *sessions.Session) template.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

func CanAccessResource(db *sqlx.DB, name string, r *http.Request, get databaseFunc) (bool, error)

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(p database.Paginator, r *http.Request) string

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.

func JSONError

func JSONError(w http.ResponseWriter, message string, status int)

JSONError takes the given message, and encodes it to a JSON object with the given status code.

func PageURL

func PageURL(url *url.URL, addr string, page int64) string

PageURL appends the given page value to the given URL as the page query parameter and returns the string.

Types

type Gate

type Gate func(u *user.User, r *http.Request) (*http.Request, bool, error)

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

func (h *Handler) Redirect(w http.ResponseWriter, r *http.Request, url string)

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

func (h *Handler) SaveMiddleware(handler http.Handler) http.Handler

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 (h Handler) UserFromCookie(r *http.Request) (*user.User, bool, error)

func (Handler) UserFromRequest

func (h Handler) UserFromRequest(w http.ResponseWriter, r *http.Request) (*user.User, bool, error)

Get the currently authenticated user from the request. Check for token auth first, then fallback to cookie.

func (Handler) UserFromToken

func (h Handler) UserFromToken(r *http.Request) (*user.User, bool, error)

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.

func (Middleware) Guest

func (h Middleware) Guest(next http.Handler) http.Handler

Guest redirects the user back to the homepage if they're already authenticated. Otherwise it let's them continue with the request.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL