redirect_back

package module
Version: v0.0.0-...-9d21605 Latest Latest
Warning

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

Go to latest
Published: Sep 2, 2020 License: MIT Imports: 8 Imported by: 2

README

Redirect Back

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

Usage

package main

import (
	"net/http"

	"github.com/conku/redirect_back"
	"github.com/conku/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)
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	SessionManager    session.ManagerInterface
	FallbackPath      string
	IgnoredPaths      []string
	IgnoredPrefixes   []string
	AllowedExtensions []string
	IgnoreFunc        func(*http.Request) bool
}

Config redirect back config

type RedirectBack

type RedirectBack struct {
	Ignore     func(req *http.Request) bool
	IgnorePath func(pth string) bool
	// contains filtered or unexported fields
}

RedirectBack redirect back struct

func New

func New(config *Config) *RedirectBack

New initialize redirect back instance

func (*RedirectBack) Middleware

func (redirectBack *RedirectBack) Middleware(handler http.Handler) http.Handler

Middleware returns a RedirectBack middleware instance that record return_to path

func (*RedirectBack) RedirectBack

func (redirectBack *RedirectBack) RedirectBack(w http.ResponseWriter, req *http.Request)

RedirectBack redirect back to last visited page

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
t or T : Toggle theme light dark auto
y or Y : Canonical URL