httpserver

package
v0.0.0-...-7bf004b Latest Latest
Warning

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

Go to latest
Published: Dec 19, 2023 License: MIT Imports: 15 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrInternal = merry.
				New("internal server error").
				WithUserMessage("Sorry, but the server encountered an error.")
	ErrBadRequest = merry.
					New("bad request").
					WithUserMessage("Your request was bad and/or invalid.")
	ErrPermissionDenied = merry.
						New("permission denied").
						WithUserMessage("You do not have permission to view this page.")
	ErrGettingUser = merry.
					New("failed to retrieve user").
					WithUserMessage("Failed to retrieve user record.")
	ErrGetSecureSession = merry.
						WithMessage(ErrInternal, "secure session exists, but could not be decoded")
	ErrRequestArgument = merry.
						New("invalid HTTP request argument").
						WithUserMessage("One or more of your request arguments was invalid.")
)
View Source
var (
	// DB is our shared database connection (handles connection pooling, and is
	// goroutine-safe)
	DB *sqlx.DB
)

Functions

func Error

func Error(w http.ResponseWriter, code int, header string, message string,
	user *user.User)

Error replies to the request with the specified error message and HTTP code. It does not otherwise end the request; the caller should ensure no further writes are done to w.

func ErrorInternal

func ErrorInternal(w http.ResponseWriter)

ErrorInternal is a helper that response with a generic error 500 message.

func ErrorUnauthorized

func ErrorUnauthorized(w http.ResponseWriter, message string, user *user.User)

ErrorUnauthorized is a helper that responds with the a 403 error code and a custom message (e.g. Sorry, but you don't have permission to view that.)"

See this Stack Overflow question for why 403 is used instead of 401:

https://stackoverflow.com/a/6937030

func GroupListGet

func GroupListGet(c *Context, w http.ResponseWriter, r *http.Request) error

GroupListGet shows the user a list of all current zauth groups.

func Listen

func Listen(database *sqlx.DB, listenTo string, isProduction bool)

Listen performs setup and runs the Web server (blocking)

func LoginGetPost

func LoginGetPost(c *Context, w http.ResponseWriter, r *http.Request) error

LoginGetPost handles a user's request to view the login page (GET and POST).

func LoginOrUserPageGet

func LoginOrUserPageGet(c *Context, w http.ResponseWriter, r *http.Request) error

LoginOrUserPageGet asks the user to login if they aren't already. If they are, it will redirect them to their user details page.

func LogoutGet

func LogoutGet(c *Context, w http.ResponseWriter, r *http.Request) error

LogoutGet handles a user's request to logout of zauth.

func NewGroupGet

func NewGroupGet(c *Context, w http.ResponseWriter, r *http.Request) error

NewGroupGet is a sub-handler that shows the Group creation page.

func NewGroupPost

func NewGroupPost(c *Context, w http.ResponseWriter, r *http.Request) error

NewGroupPost is a sub-handler that processes the Group creation form.

func NewUserGet

func NewUserGet(c *Context, w http.ResponseWriter, r *http.Request) error

NewUserGet is a sub-handler that shows the User creation page.

func NewUserPost

func NewUserPost(c *Context, w http.ResponseWriter, r *http.Request) error

NewUserPost is a sub-handler that processes the User creation form.

func PasswordResetGetPost

func PasswordResetGetPost(c *Context, w http.ResponseWriter, r *http.Request) error

PasswordResetGetPost is a sub-handler that processes password resets via secure tokens. These tokens expire after a certain time, and are only valid for the username they are created for. This handler does both GET AND POST.

func Render

func Render(w http.ResponseWriter, name string, data interface{})

Render loads the HTML template 'name' using the provided 'data' struct and buffers the output.

func UserDetailGet

func UserDetailGet(c *Context, w http.ResponseWriter, r *http.Request) error

UserDetailGet is a sub-handler that shows the details for a specific user.

func UserListGet

func UserListGet(c *Context, w http.ResponseWriter, r *http.Request) error

UserListGet shows the user a list of all current zauth users.

func Wrap

func Wrap(router *mux.Router, subHandler Handler, requireLogin bool) http.Handler

Wrap provides a wrapper to page-specific handlers. It handles logging, getting and checking user authentication, flash messages and error rendering. The sub handler MUST implement Handler.

Order of operations:

  • Get the user object if logged in
  • Log request (before auth req redirection)
  • Redirect to login IFF auth is required
  • Get flash messages if any -- run page-specific sub-handler
  • If there is an error, STOP continuing and render a proper error
  • If there are new flash messages, SAVE them
  • Render the page OR render the error

TODO: Defer logging the request until we have the result, so we can log them

on the same line like so: josh POST /group/new -> error: duplicate name

Types

type Context

type Context struct {
	// Router is the Gorilla-based router. It's included here so we can route
	// names can be reversed (e.g. what is the URI for a user's details page?).
	Router *mux.Router
	// Tx is the database transaction that is started for you.
	Tx *sqlx.Tx
	// User is the person making this HTTP request.
	User               *user.User
	NormalFlashMessage string
	ErrorFlashMessage  string
	RouteVariables     map[string]string
	Response           http.ResponseWriter
	Request            *http.Request
}

Context is a struct passed to Handlers with additional information not in the standard HTTP handlers, such as User object.

func (*Context) AddErrorFlash

func (c *Context) AddErrorFlash(message string)

AddErrorFlash adds an error flash message to the store to be viewed by the user upon next page request.

If you need to add a flash message, you should do so before writing to the response(Writer)! This is due to gorilla's session.Save().

func (*Context) AddNormalFlash

func (c *Context) AddNormalFlash(message string)

AddNormalFlash adds a flash message to the store to be viewed by the user upon next page request.

If you need to add a flash message, you should do so before writing to the response(Writer)! This is due to gorilla's session.Save().

Normal flash messages are not errors, and are typically informing the user that an operation was successful such as logging out, or creating a new user.

func (*Context) GetRouteVarTrim

func (c *Context) GetRouteVarTrim(varName string) string

GetRouteVarTrim returns the whitespace trimmed Gorilla Mux Route Variable.

func (*Context) GetUser

func (c *Context) GetUser(username string) (user.User, error)

GetUser returns the specified User. This potentially avoids a second DB call if the HTTP request is being made by this user, and is thus already loaded into memory.

type Handler

type Handler = func(c *Context, w http.ResponseWriter, r *http.Request) error

Handler adds a context argument and is used with handleWrap

type LoginPageData

type LoginPageData struct {
	Message   string
	Error     string
	Username  string
	CSRFField template.HTML
}

Jump to

Keyboard shortcuts

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