Skip to content

This is session middleware for Echo, provided as an alternative implementation inspired by labstack/echo-contrib/session.

License

Notifications You must be signed in to change notification settings

kohkimakimoto/echo-session

Repository files navigation

Echo Session

test MIT License Go Reference

This is session middleware for Echo, provided as an alternative implementation inspired by labstack/echo-contrib/session.

Usage

package main

import (
	"fmt"
	"net/http"

	session "github.com/kohkimakimoto/echo-session"
	"github.com/labstack/echo/v4"
)

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

	e.Use(session.Middleware(session.NewCookieStore([]byte("12345678901234567890123456789012"))))

	e.GET("/", func(c echo.Context) error {
		s := session.MustGet(c)
		counter := 0
		if val := s.Get("counter"); val != nil {
			if count, ok := val.(int); ok {
				counter = count
			}
		}
		counter++

		s.Set("counter", counter)
		if err := s.Save(); err != nil {
			return err
		}
		return c.HTML(http.StatusOK, fmt.Sprintf("Counter: %d", counter))
	})

	e.GET("/refresh", func(c echo.Context) error {
		s := session.MustGet(c)
		s.Clear()
		if err := s.Save(); err != nil {
			return err
		}
		return c.Redirect(http.StatusFound, "/")
	})

	e.Logger.Fatal(e.Start(":8080"))
}

Author

Kohki Makimoto kohki.makimoto@gmail.com

License

The MIT License (MIT)

About

This is session middleware for Echo, provided as an alternative implementation inspired by labstack/echo-contrib/session.

Topics

Resources

License

Stars

Watchers

Forks

Languages