wrapsession

package
v2.7.1+incompatible Latest Latest
Warning

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

Go to latest
Published: Aug 15, 2018 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

Package wrapsession provides wrappers based on the github.com/gorilla/sessions. It assumes one session per request. If you need multiple sessions pre request, define your own wrapper or use gorilla/sessions directly.

wrapsession expects the ResponseWriter to a wrap.Contexter supporting sessions.Session and error

Example
package main

import (
	"fmt"
	"net/http"
	"net/http/httptest"

	"github.com/go-on/sessions"
	"github.com/go-on/wrap"
	"github.com/go-on/wrap-contrib/stack"
	"github.com/go-on/wrap-contrib/third-party/wrapsession"
	"github.com/go-on/wrap-contrib/wraps"
)

var store = sessions.NewCookieStore([]byte("something-very-secret"))

func errorHandler(w http.ResponseWriter, r *http.Request) {
	var err error
	w.WriteHeader(http.StatusInternalServerError)
	fmt.Fprintf(w, "An error happened: %s\n", err.Error())
	fmt.Printf("An error happened: %s\n", err.Error())
	return
}

type writeToSession struct{}

var _ wrap.ContextWrapper = writeToSession{}

func (writeToSession) ValidateContext(ctx wrap.Contexter) {
	var session sessions.Session
	ctx.SetContext(&session)
	ctx.Context(&session)
}

func (writeToSession) Wrap(next http.Handler) http.Handler {
	var f http.HandlerFunc
	f = func(w http.ResponseWriter, r *http.Request) {
		var session sessions.Session
		w.(wrap.Contexter).Context(&session)
		session.Values["name"] = r.URL.Query().Get("name")
		session.AddFlash("Hello, flash messages world!")
		next.ServeHTTP(w, r)
	}
	return f
}

type readFromSession struct{}

var _ wrap.ContextWrapper = readFromSession{}

func (readFromSession) ValidateContext(ctx wrap.Contexter) {
	var session sessions.Session
	ctx.SetContext(&session)
	ctx.Context(&session)
}

func (readFromSession) Wrap(next http.Handler) http.Handler {
	var f http.HandlerFunc
	f = func(w http.ResponseWriter, r *http.Request) {
		var session sessions.Session
		w.(wrap.Contexter).Context(&session)
		fmt.Printf("Name: %s\n", session.Values["name"])
		for _, fl := range session.Flashes() {
			fmt.Printf("Flash: %v\n", fl)
		}
	}
	return f
}

func main() {
	// stack.New() sets and checks global Contexter
	stack.New(&context{})

	// stack.Use() checks if global Contexter supports the context data needed by the wrappers
	// before adding them to the global Contexter
	stack.Use(
		wraps.ErrorHandlerFunc(errorHandler),
		wrapsession.SaveAndClear,
		wrapsession.Session(store, "my-session-name"),
		writeToSession{},
		readFromSession{},
	)

	// put the stack together and return a handler
	h := stack.Handler()

	req, _ := http.NewRequest("GET", "/?name=Peter", nil)
	rec := httptest.NewRecorder()
	h.ServeHTTP(rec, req)

}
Output:

Name: Peter
Flash: Hello, flash messages world!

Index

Examples

Constants

This section is empty.

Variables

View Source
var SaveAndClear = saveAndClear{}

SaveAndClear saves the session and clears up any references to the request. it should be used at the beginning of the middleware chain (but after the Contexter)

Functions

func Session

func Session(store sessions.Store, name string) wrap.Wrapper

Types

This section is empty.

Jump to

Keyboard shortcuts

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