Skip to content

go-session/gear-session

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Session middleware for Gear

Build Codecov ReportCard GoDoc License

Quick Start

Download and install

$ go get -u -v github.com/go-session/gear-session

Create file server.go

package main

import (
	"fmt"
	"net/http"

	"github.com/go-session/gear-session"
	"github.com/teambition/gear"
)

func main() {
	app := gear.New()
	app.Use(gearsession.New())

	router := gear.NewRouter()

	router.Get("/", func(ctx *gear.Context) error {
		store := gearsession.FromContext(ctx)
		store.Set("foo", "bar")
		err := store.Save()
		if err != nil {
			return gear.ErrInternalServerError.From(err)
		}
		return ctx.Redirect("/foo")
	})

	router.Get("/foo", func(ctx *gear.Context) error {
		store := gearsession.FromContext(ctx)
		foo, ok := store.Get("foo")
		if !ok {
			return gear.ErrNotFound
		}
		return ctx.End(http.StatusOK, []byte(fmt.Sprintf("foo:%s", foo)))
	})
	app.UseHandler(router)

	app.Listen(":8080")
}

Build and run

$ go build server.go
$ ./server

Open in your web browser

http://localhost:8080

foo:bar

MIT License

Copyright (c) 2018 Lyric