session

package
v0.0.0-...-800783e Latest Latest
Warning

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

Go to latest
Published: Feb 20, 2016 License: Apache-2.0 Imports: 11 Imported by: 0

README

Session

Middleware support for echo, utilizing by gorilla/sessions.

Installation

go get github.com/syntaqx/echo-middleware/session

Usage

package main

import (
    "net/http"

    "github.com/gorilla/context"
    "github.com/labstack/echo"
    "github.com/syntaqx/echo-middleware/session"
)

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

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

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

    session.Set("count", count)
    session.Save()

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

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

func main() {
    store := session.NewCookieStore([]byte("secret-key"))
    // store := session.NewFilesystemStore("", []byte("secret-key"))
    // store, err := session.NewRedisStore(32, "tcp", "localhost:6379", "", []byte("secret-key"))
    // if err != nil {
    //     panic(err)
    // }

    e := echo.New()

    // Attach middleware
    e.Use(session.Sessions("ESESSION", store))

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

    // Wrap echo with a context.ClearHandler
    http.ListenAndServe(":8080", context.ClearHandler(e))
}

Documentation

Overview

Copyright 2016 Wenhui Shen <www.webx.top>

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

Session implements middleware for easily using github.com/gorilla/sessions within echo. This package was originally inspired from the https://github.com/ipfans/echo-session package, and modified to provide more functionality

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CloseBolt

func CloseBolt()

func NewMySession

func NewMySession(store Store, name string, ctx echo.Context) I.Session

func NewSession

func NewSession(options *I.Options, setting interface{}, ctx echo.Context) I.Session

Types

type BoltStore

type BoltStore interface {
	Store
}

func NewBoltStore

func NewBoltStore(dbFile string, options I.Options, bucketName []byte, keyPairs ...[]byte) (BoltStore, error)

./sessions.db

type CookieStore

type CookieStore interface {
	Store
}

func NewCookieStore

func NewCookieStore(keyPairs ...[]byte) CookieStore

Keys are defined in pairs to allow key rotation, but the common case is to set a single authentication key and optionally an encryption key.

The first key in a pair is used for authentication and the second for encryption. The encryption key can be set to nil or omitted in the last pair, but the authentication key is required in all pairs.

It is recommended to use an authentication key with 32 or 64 bytes. The encryption key, if set, must be either 16, 24, or 32 bytes to select AES-128, AES-192, or AES-256 modes.

type FilesystemStore

type FilesystemStore interface {
	Store
}

func NewFilesystemStore

func NewFilesystemStore(path string, keyPairs ...[]byte) FilesystemStore

NewFilesystemStore returns a new FilesystemStore.

The path argument is the directory where sessions will be saved. If empty it will use os.TempDir().

See NewCookieStore() for a description of the other parameters.

type RedisStore

type RedisStore interface {
	Store
}

func NewRedisStore

func NewRedisStore(size int, network, address, password string, keyPairs ...[]byte) (RedisStore, error)

size: maximum number of idle connections. network: tcp or udp address: host:port password: redis-password Keys are defined in pairs to allow key rotation, but the common case is to set a single authentication key and optionally an encryption key.

The first key in a pair is used for authentication and the second for encryption. The encryption key can be set to nil or omitted in the last pair, but the authentication key is required in all pairs.

It is recommended to use an authentication key with 32 or 64 bytes. The encryption key, if set, must be either 16, 24, or 32 bytes to select AES-128, AES-192, or AES-256 modes.

type Session

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

func (*Session) AddFlash

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

func (*Session) Clear

func (s *Session) Clear() I.Session

func (*Session) Delete

func (s *Session) Delete(key string) I.Session

func (*Session) Flashes

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

func (*Session) Get

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

func (*Session) Options

func (s *Session) Options(options I.Options) I.Session

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{}) I.Session

func (*Session) SetID

func (s *Session) SetID(id string) I.Session

func (*Session) Written

func (s *Session) Written() bool

type Store

type Store interface {
	sessions.Store
	Options(I.Options)
}

func StoreEngine

func StoreEngine(options *I.Options, setting interface{}) (store Store)

Jump to

Keyboard shortcuts

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