engine

package
v2.30.3+incompatible Latest Latest
Warning

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

Go to latest
Published: Nov 9, 2022 License: Apache-2.0 Imports: 9 Imported by: 20

README

Session

Middleware support for echo, utilizing by admpub/sessions.

Installation

go get github.com/webx-top/echo

Usage

package main

import (
    "github.com/webx-top/echo"
    "github.com/webx-top/echo/engine/standard"
    "github.com/webx-top/echo/middleware/session"
    cookieStore "github.com/webx-top/echo/middleware/session/engine/cookie"
)

func index(c echo.Context) error {
    session := c.Session()

    var count int
    v := session.Get("count")

    if v == nil {
        count = 0
    } else {
        count = v.(int)
        count += 1
    }

    session.Set("count", count)

    data := struct {
        Visit int
    }{
        Visit: count,
    }

    return c.JSON(http.StatusOK, data)
}

func main() {
    sessionOptions:=&echo.SessionOptions{
        Name:   `GOSESSIONID`,
        Engine: `cookie`,
        CookieOptions: &echo.CookieOptions{
            Path:     `/`,
            HttpOnly: true,
        },
    }
    cookieStore.RegWithOptions(&cookieStore.CookieOptions{
        KeyPairs: [][]byte{
            []byte("secret-key"),
        },
        SessionOptions: sessionOptions,
    })

    e := echo.New()

    // Attach middleware
    e.Use(session.Middleware(sessionOptions))

    // Routes
    e.Get("/", index)

    e.Run(standard.New(":8080"))
}

Documentation

Index

Constants

View Source
const (
	DefaultMaxAge = 86400 * 30 // 30days
)

Defaults for sessions.Options

Variables

View Source
var ErrInvalidSessionID = errors.New("invalid session ID")

Functions

func Del

func Del(name string)

func GenerateSessionID added in v1.6.0

func GenerateSessionID(prefix ...string) string

func Get

func Get(name string) sessions.Store

func NewMySession

func NewMySession(store sessions.Store, name string, ctx echo.Context) echo.Sessioner

func NewSession

func NewSession(ctx echo.Context) echo.Sessioner

func Reg

func Reg(name string, store sessions.Store)

func StoreEngine

func StoreEngine(options *echo.SessionOptions) (store sessions.Store)

Types

type Closer added in v1.6.0

type Closer interface {
	Close() error
}

type Session

type Session struct {
	// contains filtered or unexported fields
}

func (*Session) AddFlash

func (s *Session) AddFlash(value interface{}, vars ...string) echo.Sessioner

func (*Session) AddPreSaveHook added in v1.6.0

func (s *Session) AddPreSaveHook(hook func(echo.Context) error)

func (*Session) Clear

func (s *Session) Clear() echo.Sessioner

func (*Session) Delete

func (s *Session) Delete(key string) echo.Sessioner

func (*Session) Flashes

func (s *Session) Flashes(vars ...string) []interface{}

func (*Session) Get

func (s *Session) Get(key string) interface{}

func (*Session) ID added in v1.3.0

func (s *Session) ID() string

func (*Session) MustID added in v1.6.0

func (s *Session) MustID() string

func (*Session) RemoveID added in v1.6.0

func (s *Session) RemoveID(sessionID string) error

func (*Session) Save

func (s *Session) Save() error

func (*Session) Session

func (s *Session) Session() *sessions.Session

func (*Session) Set

func (s *Session) Set(key string, val interface{}) echo.Sessioner

func (*Session) SetID added in v1.3.0

func (s *Session) SetID(id string, notReload ...bool) error

func (*Session) SetPreSaveHook added in v1.6.0

func (s *Session) SetPreSaveHook(hooks ...func(echo.Context) error)

func (*Session) Written

func (s *Session) Written() bool

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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