Skip to content

go-session/buntdb

Folders and files

NameName
Last commit message
Last commit date

Latest commit

19ded39 Â· Oct 29, 2018

History

7 Commits
Jun 6, 2018
Jun 13, 2018
Jun 6, 2018
Jun 13, 2018
Oct 29, 2018
Jun 13, 2018

Repository files navigation

BuntDB store for Session

Build Codecov ReportCard GoDoc License

Quick Start

Download and install

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

Create file server.go

package main

import (
	"context"
	"fmt"
	"net/http"

	"github.com/go-session/buntdb"
	"github.com/go-session/session"
)

func main() {
	session.InitManager(
		session.SetStore(buntdb.NewFileStore("session.db")),
	)

	http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
		store, err := session.Start(context.Background(), w, r)
		if err != nil {
			fmt.Fprint(w, err)
			return
		}

		store.Set("foo", "bar")
		err = store.Save()
		if err != nil {
			fmt.Fprint(w, err)
			return
		}

		http.Redirect(w, r, "/foo", 302)
	})

	http.HandleFunc("/foo", func(w http.ResponseWriter, r *http.Request) {
		store, err := session.Start(context.Background(), w, r)
		if err != nil {
			fmt.Fprint(w, err)
			return
		}

		foo, ok := store.Get("foo")
		if ok {
			fmt.Fprintf(w, "foo:%s", foo)
			return
		}
		fmt.Fprint(w, "does not exist")
	})

	http.ListenAndServe(":8080", nil)
}

Build and run

$ go build server.go
$ ./server

Open in your web browser

http://localhost:8080

foo:bar

MIT License

Copyright (c) 2018 Lyric