beegosession

package module
v3.0.1+incompatible Latest Latest
Warning

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

Go to latest
Published: Jun 13, 2018 License: MIT Imports: 4 Imported by: 0

README

Session middleware for Beego

Build Codecov ReportCard GoDoc License

Quick Start

Download and install
$ go get -u -v github.com/go-session/beego-session
Create file server.go
package main

import (
	"fmt"

	"github.com/astaxie/beego"
	"github.com/astaxie/beego/context"
	"github.com/go-session/beego-session"
	"github.com/go-session/session"
)

func main() {
	app := beego.NewApp()

	app.Handlers.InsertFilter("*", beego.BeforeRouter, beegosession.New())

	app.Handlers.Get("/", func(ctx *context.Context) {
		store := beegosession.FromContext(ctx)
		store.Set("foo", "bar")
		err := store.Save()
		if err != nil {
			ctx.Abort(500, err.Error())
			return
		}
		ctx.Redirect(302, "/foo")
	})

	app.Handlers.Get("/foo", func(ctx *context.Context) {
		store := beegosession.FromContext(ctx)
		foo, ok := store.Get("foo")
		if !ok {
			ctx.Abort(404, "not found")
			return
		}
		ctx.WriteString(fmt.Sprintf("foo:%s", foo))
	})

	beego.BConfig.Listen.HTTPPort = 8080
	app.Run()
}
Build and run
$ go build server.go
$ ./server
Open in your web browser

http://localhost:8080

foo:bar

MIT License

Copyright (c) 2018 Lyric

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (

	// DefaultConfig is the default Recover middleware config.
	DefaultConfig = Config{
		ErrorHandleFunc: func(ctx *context.Context, err error) {
			ctx.Abort(500, err.Error())
		},
	}
)

Functions

func Destroy

func Destroy(ctx *context.Context) error

Destroy a session

func FromContext

func FromContext(ctx *context.Context) session.Store

FromContext Get session storage from context

func New

func New(opt ...session.Option) beego.FilterFunc

New create a session middleware

func NewWithConfig

func NewWithConfig(config Config, opt ...session.Option) beego.FilterFunc

NewWithConfig create a session middleware

func Refresh

func Refresh(ctx *context.Context) (session.Store, error)

Refresh a session and return to session storage

Types

type Config

type Config struct {
	// error handling when starting the session
	ErrorHandleFunc ErrorHandleFunc
}

Config defines the config for Session middleware

type ErrorHandleFunc

type ErrorHandleFunc func(*context.Context, error)

ErrorHandleFunc error handling function

Jump to

Keyboard shortcuts

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