Skip to content

go-session/echo-session

Folders and files

NameName
Last commit message
Last commit date

Latest commit

b37294e · Oct 19, 2018

History

6 Commits
May 16, 2018
Jun 13, 2018
May 16, 2018
Aug 31, 2018
Oct 19, 2018
Jun 13, 2018

Repository files navigation

Session middleware for Echo

Build Codecov ReportCard GoDoc License

Quick Start

Download and install

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

Create file server.go

package main

import (
	"fmt"
	"net/http"

	"github.com/go-session/echo-session"
	"github.com/labstack/echo"
)

func main() {
	e := echo.New()

	e.Use(echosession.New())

	e.GET("/", func(ctx echo.Context) error {
		store := echosession.FromContext(ctx)
		store.Set("foo", "bar")
		err := store.Save()
		if err != nil {
			return err
		}
		return ctx.Redirect(302, "/foo")
	})

	e.GET("/foo", func(ctx echo.Context) error {
		store := echosession.FromContext(ctx)
		foo, ok := store.Get("foo")
		if !ok {
			return ctx.String(http.StatusNotFound, "not found")
		}
		return ctx.String(http.StatusOK, fmt.Sprintf("foo:%s", foo))
	})

	e.Logger.Fatal(e.Start(":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