Documentation ¶
Overview ¶
Package base contains middleware that has no external dependencies except Goji
Index ¶
- Variables
- func AWSHTTPRedirect(host string) func(c *web.C, h http.Handler) http.Handler
- func BuildEnvSet(key string, value interface{}) func(c *web.C, h http.Handler) http.Handler
- func BuildSessionMiddleware(sh SessionHolder) func(c *web.C, h http.Handler) http.Handler
- func BuildStripPrefix(pattern interface{}) func(c *web.C, h http.Handler) http.Handler
- func BuildUpdateableSet(key string, value *interface{}) func(c *web.C, h http.Handler) http.Handler
- func CacheAWhileMiddleWare(cacheFor time.Duration) func(c *web.C, h http.Handler) http.Handler
- func GzipMiddleWare(c *web.C, h http.Handler) http.Handler
- func LoggingMiddleWare(c *web.C, h http.Handler) http.Handler
- func LoggingMiddleWareJSON(c *web.C, h http.Handler) http.Handler
- func Logout(c *web.C)
- func StripRangeMiddleWare(c *web.C, h http.Handler) http.Handler
- type BaseSessionHolder
- func (sh *BaseSessionHolder) AddToResponse(c web.C, session *Session, w http.ResponseWriter)
- func (sh *BaseSessionHolder) Create(c web.C) *Session
- func (sh *BaseSessionHolder) GenerateSessionId() string
- func (sh *BaseSessionHolder) GetSessionId(r *http.Request) string
- func (sh *BaseSessionHolder) GetTimeout() int
- func (sh *BaseSessionHolder) SetHttpOnly(httpOnly bool)
- func (sh *BaseSessionHolder) SetPersistentCookies(persistent bool)
- func (sh *BaseSessionHolder) SetSameSite(ss http.SameSite)
- func (sh *BaseSessionHolder) SetSecure(secure bool)
- func (sh *BaseSessionHolder) SetTimeout(timeout int)
- type MemorySessionHolder
- func (sh *MemorySessionHolder) Destroy(c web.C, session *Session) error
- func (sh *MemorySessionHolder) Get(c web.C, r *http.Request) (*Session, error)
- func (sh *MemorySessionHolder) RegenerateId(_ web.C, session *Session) (string, error)
- func (sh *MemorySessionHolder) ResetTTL(_ web.C, session *Session) error
- func (sh *MemorySessionHolder) Save(c web.C, session *Session) error
- type Session
- type SessionHolder
- type StatusTrackingResponseWriter
Constants ¶
This section is empty.
Variables ¶
var (
ErrorSessionNotFound error = errors.New("No session found matching that Id")
)
Functions ¶
func AWSHTTPRedirect ¶
AWSHTTPRedirect redirects traffic on port 80 to https. It does this by looking at the X-Forwarded-Proto header, which is set by AWS load balancers
func BuildEnvSet ¶
Build a middleware that always sets c.Env[key] = value
func BuildSessionMiddleware ¶
BuildSessionMiddleware builds middleware with the provided SessionHolder. The middleware
- adds the SessionHolder to c.Env["sessionholder"] so application code can create and delete sessions
- finds the session associated with the request (if any) and puts it in c.Env["session"]
- saves the session if dirty and the request is processed without a panic
Add the middleware as follows
mux := web.New() mux.Use(redis.BuildRedis(":6379")) sh := redis.NewSessionHolder() mux.Use(redis.BuildSessionMiddleware(sh)) // Add handlers that use sessions
func BuildStripPrefix ¶
BuildStripPrefix builds Goji middleware that strips a prefix from the request URL path.
pattern can be either a string or a *regexp.Regexp. If it is a string a prefix of the same length as the string is removed from the path. If it is a Regexp then everything up to the end of the first match is removed
func BuildUpdateableSet ¶
Build a middleware that always sets c.Env[key] = *value
func CacheAWhileMiddleWare ¶
CacheAWhileMiddleware builds a middleware that sets the Expires header for cacheFor into the future
func GzipMiddleWare ¶
Simple GZIP middleware
GZIPs GET 200 OK responses if the request Accept-Encoding includes gzip. Sets Content-Encoding to "gzip".
func LoggingMiddleWare ¶
Middleware that logs responses.
The output format is:
<remote addr> - <method> <url> <status code> <response time ms>
Remote address is taken from X-Forwarded-For & X-Forwarded-Port if present
Types ¶
type BaseSessionHolder ¶
type BaseSessionHolder struct { Timeout int RandSource rand.Source HttpOnly bool Secure bool SameSite http.SameSite PersistentCookie bool }
BaseSessionHolder is a building block you can use to build a SessionHolder implementation
func NewBaseSessionHolder ¶
func NewBaseSessionHolder(timeout int) BaseSessionHolder
func (*BaseSessionHolder) AddToResponse ¶
func (sh *BaseSessionHolder) AddToResponse(c web.C, session *Session, w http.ResponseWriter)
Add the Session to the response
Basically this means setting a cookie
func (*BaseSessionHolder) Create ¶
func (sh *BaseSessionHolder) Create(c web.C) *Session
Create builds a session object, adds it to c.Env["session"] and marks it as dirty
func (*BaseSessionHolder) GenerateSessionId ¶
func (sh *BaseSessionHolder) GenerateSessionId() string
func (*BaseSessionHolder) GetSessionId ¶
func (sh *BaseSessionHolder) GetSessionId(r *http.Request) string
GetSessionId extracts the session ID from a request if one is present.
Note this is not part of the SessionHolder interface - it is intended to be used as a building block by SessionHolder implementations
func (*BaseSessionHolder) GetTimeout ¶
func (sh *BaseSessionHolder) GetTimeout() int
GetTimeout retrieves the currently set TTL for session objects
func (*BaseSessionHolder) SetHttpOnly ¶
func (sh *BaseSessionHolder) SetHttpOnly(httpOnly bool)
SetHTTPOnly allows setting/unsetting of HTTP only secured cookies
func (*BaseSessionHolder) SetPersistentCookies ¶
func (sh *BaseSessionHolder) SetPersistentCookies(persistent bool)
SetPersistentCookies allows switching between persistent time out based cookies (MaxAge) and session lifetime cookies (no MaxAge)
func (*BaseSessionHolder) SetSameSite ¶
func (sh *BaseSessionHolder) SetSameSite(ss http.SameSite)
func (*BaseSessionHolder) SetSecure ¶
func (sh *BaseSessionHolder) SetSecure(secure bool)
SetSecure allows setting/unsetting of HTTPS only / secure cookies
func (*BaseSessionHolder) SetTimeout ¶
func (sh *BaseSessionHolder) SetTimeout(timeout int)
SetTimeout updates the TTL for session objects
Note that the TTL will only be updated for existing sessions when the session is requested again, it might timeout if this request takes too long to occur
type MemorySessionHolder ¶
type MemorySessionHolder struct { BaseSessionHolder // contains filtered or unexported fields }
MemorySessionHolder is an in-memory session holder. Only really useful for testing
func (*MemorySessionHolder) Destroy ¶
func (sh *MemorySessionHolder) Destroy(c web.C, session *Session) error
func (*MemorySessionHolder) RegenerateId ¶
Regenerate the session id of an existing session.
type Session ¶
type Session struct { Values map[string]interface{} // contains filtered or unexported fields }
Session represents a web session
func SessionFromEnv ¶
SessionFromEnv() retrieves the session from the Goji context
type SessionHolder ¶
type SessionHolder interface { /* Get the session associated with the current request, if there is one. Return ErrorSessionNotFound if there is no matching session */ Get(c web.C, r *http.Request) (*Session, error) /* Create a new session Note the implementation should create the session dirty so it will be saved when the response is processed. It should also add the session to c.Env["session"] for easy access */ Create(c web.C) *Session /* Destroy a session so that it can no longer be retrieved */ Destroy(c web.C, session *Session) error /* Save a session */ Save(c web.C, session *Session) error /* Regenerate the sessionId for an existing session. This can be used against session fixation attacks */ RegenerateId(c web.C, session *Session) (string, error) /* AddToResponse writes the session cookie into the http response */ AddToResponse(c web.C, session *Session, w http.ResponseWriter) /* SetTimeout sets the timeout for session data */ SetTimeout(timeout int) /* GetTimeout retrieves the currently set timeout for session data */ GetTimeout() int /* Set HttpOnly cookie on/off */ SetHttpOnly(httpOnly bool) /* Set Secure cookie on/off */ SetSecure(secure bool) SetSameSite(ss http.SameSite) /* ResetTTL can be implemented to reset the TTL for a session object if not dirty */ ResetTTL(c web.C, session *Session) error }
SessionHolder is an interface for a session repository.
func NewMemorySessionHolder ¶
func NewMemorySessionHolder(timeout int) SessionHolder
func SessionHolderFromEnv ¶
func SessionHolderFromEnv(c *web.C) (SessionHolder, bool)
SessionHolderFromEnv() retrieves the session holder from the Goji context
type StatusTrackingResponseWriter ¶
type StatusTrackingResponseWriter struct { http.ResponseWriter // http status code written Status int }
func (*StatusTrackingResponseWriter) WriteHeader ¶
func (w *StatusTrackingResponseWriter) WriteHeader(status int)