middleware

package
v0.0.0-...-11c8b56 Latest Latest
Warning

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

Go to latest
Published: Jan 1, 2018 License: AGPL-3.0 Imports: 10 Imported by: 0

Documentation

Overview

Package middleware contains the HTTP middleware used in the api as well as utility functions for interacting with them

Index

Constants

This section is empty.

Variables

View Source
var Default = Chain{
	ContentHeaders,
	Logger,
}

Default is the default middleware stack for Praelatus

Functions

func CORS

func CORS(next http.Handler) http.Handler

CORS allows cross origin requests to the server. Note: By default it allows all origins so can be insecure. TODO: Make the origins configurable

func ContentHeaders

func ContentHeaders(next http.Handler) http.Handler

ContentHeaders will set the content-type header for the API to application/json

func GetUserSession

func GetUserSession(r *http.Request) *models.User

GetUserSession will check the given http.Request for a session token and if found it will return the corresponding user.

func Logger

func Logger(next http.Handler) http.Handler

Logger will log a request and any information about the request, it should be the first middleware in any chain.

func RefreshSession

func RefreshSession(r *http.Request) error

RefreshSession will reset the expiry on the session for the given request

func SetUserSession

func SetUserSession(u models.User, w http.ResponseWriter) error

SetUserSession will generate a secure cookie for user u, will set the cookie on the response w and will add the user session to the session store

Types

type Chain

type Chain []Middleware

Chain is a middleware chain

func (Chain) Load

func (c Chain) Load(next http.Handler) http.Handler

Load the middleware in this chain for the given handler

type LoggedResponseWriter

type LoggedResponseWriter struct {
	http.ResponseWriter
	// contains filtered or unexported fields
}

LoggedResponseWriter wraps http.ResponseWriter so we can capture the status code for logging

func (*LoggedResponseWriter) Status

func (w *LoggedResponseWriter) Status() int

Status will return the status code used in this request.

func (*LoggedResponseWriter) Write

func (w *LoggedResponseWriter) Write(b []byte) (int, error)

func (*LoggedResponseWriter) WriteHeader

func (w *LoggedResponseWriter) WriteHeader(code int)

WriteHeader implements http.ResponseWriter adding our custom functionality to it

type Middleware

type Middleware func(http.Handler) http.Handler

Middleware is any function which modifies the behavior of a http.Handler

func (Middleware) Load

func (m Middleware) Load(next http.Handler) http.Handler

Load the middleware for the given handler

Jump to

Keyboard shortcuts

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