Skip to content
master
Switch branches/tags
Code

Latest commit

 

Git stats

Files

Permalink
Failed to load latest commit information.
Type
Name
Latest commit message
Commit time
 
 
 
 

Redirect Back

A Golang HTTP Handler that redirect back to last URL saved in session

Usage

package main

import (
	"net/http"

	"github.com/qor/redirect_back"
	"github.com/qor/session/manager"
)

var RedirectBack = redirect_back.New(&redirect_back.Config{
	IgnoredPaths:      []string{"/login"},     // Will ignore requests that has those paths when set return path
	IgnoredPrefixes:   []string{"/auth"},      // Will ignore requests that has those prefixes when set return path
	AllowedExtensions: []string{"", ".html"}   // Only save requests w/o extension or extension `.html` (default setting)
	IgnoreFunc: func(req *http.Request) bool { // Will ignore request if `IgnoreFunc` returns true
		return false
	},
	SessionManager: manager.SessionManager,
	FallbackPath:   "/",
})

func main() {
	mux := http.NewServeMux()
	mux.HandleFunc("/", homeHandler)
	mux.HandleFunc("/page", pageHandler)

	// Wrap your application's handlers or router with redirect back's middleware
	http.ListenAndServe(":8000", manager.SessionManager.Middleware(RedirectBack.Middleware(mux)))
}

func homeHandler(w http.ResponseWriter, req *http.Request) {
	w.Write([]byte("home"))
}

func pageHandler(w http.ResponseWriter, req *http.Request) {
	// Redirect to return path or the default one
	RedirectBack.RedirectBack(w, req)
}

About

A Golang HTTP Handler that redirect back to last URL saved in session

Resources

License

Releases

No releases published

Packages

No packages published

Languages