Skip to content

Latest commit

History

6 Commits

Folders and files

NameName
Last commit message
Last commit date

Repository files navigation

GORM store for Session

BuildCodecovReportCardGoDocLicense

Quick Start

Download and install

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

Create file server.go

package main
import (
"context""fmt""net/http"
gormstore "github.com/go-session/gorm""github.com/go-session/session"
_ "github.com/jinzhu/gorm/dialects/sqlite"
)
funcmain() {
store:=gormstore.MustStore(gormstore.Config{}, "sqlite3", "session.db")
deferstore.Close()
session.InitManager(
session.SetStore(store),
)
http.HandleFunc("/", func(w http.ResponseWriter, r*http.Request) {
store, err:=session.Start(context.Background(), w, r)
iferr!=nil {
fmt.Fprint(w, err)
return
}
store.Set("foo", "bar")
err=store.Save()
iferr!=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)
iferr!=nil {
fmt.Fprint(w, err)
return
}
foo, ok:=store.Get("foo")
ifok {
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) 2019 Lyric

About

GORM store for Session.

Topics

Resources

Stars

4 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages