flash

package
v0.0.0-...-baf4ea5 Latest Latest
Warning

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

Go to latest
Published: Oct 25, 2021 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package flash provides flash message utilities

Index

Constants

View Source
const (
	Success = "success"
	Error   = "error"
	Warning = "warning"
)

Variables

This section is empty.

Functions

Types

type Context

type Context string
const (
	ContextFlashMsgs Context = "ContextFlashMsgs" // map[string][]string
)

type Encoder

type Encoder struct {
	Key string
}

func NewEncoder

func NewEncoder(key string) Encoder

func (Encoder) GetFlashMsgs

func (fe Encoder) GetFlashMsgs(w http.ResponseWriter, r *http.Request) (map[string][]FlashMsg, error)

GetFlashMsgs will get all flash messages from both the cookie and request context, after which it will delete the cookie (hence a 'flash' message).

Make sure you use an Encoder with the same seceret key as the encoder that you set the flash messages with:

flashmsgs, _ := flash.NewEncoder("secret_key").GetFlashMsgs(w, r)

Usually you won't call this function directly in your handlers, instead you will include the "helpers/flash/flash.html" template in your html file which will automatically call this function and display the flash messages, if any.

func (Encoder) SetFlashMsgs

func (fe Encoder) SetFlashMsgs(w http.ResponseWriter, r *http.Request, msgs map[string][]string) (*http.Request, error)

SetFlashMsgs will set a bunch of flash messages into a cookie that can be read later by GetFlashMsgs.

NOTE: This returns a new request with the flash messages in the context updated, so make sure to re-assign the request:

r, _ = flash.NewEncoder("secret_key").SetFlashMsgs(w, r, msgs)

func (Encoder) UnsetFlashMsgs

func (fe Encoder) UnsetFlashMsgs(w http.ResponseWriter, r *http.Request, keys ...string) (*http.Request, error)

UnsetFlashMsgs will unset all flash messages under the specified keys. This is useful if you wish to suppress particular flash messages that you know were set in a previous middleware handler.

NOTE: This returns a new request with the flash messages in the context updated, so make sure to re-assign the request:

r, _ = flash.NewEncoder("secret_key").UnsetFlashMsgs(flash.Success)

func (Encoder) UnsetFlashMsgsHandler

func (fe Encoder) UnsetFlashMsgsHandler(keys ...string) func(http.Handler) http.Handler

UnsetFlashMsgsHandler is a http.Handler wrapper around UnsetFlashMsgs.

type FlashMsg

type FlashMsg struct {
	Valid bool   `json:"Valid"`
	Value string `json:"Value"`
}

FlashMsg represents a flash message

Jump to

Keyboard shortcuts

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