Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/moonrhythm/session
Session Manager and Middleware for Go net/http
https://github.com/moonrhythm/session
golang http middleware session session-management
Last synced: 8 days ago
JSON representation
Session Manager and Middleware for Go net/http
- Host: GitHub
- URL: https://github.com/moonrhythm/session
- Owner: moonrhythm
- License: mit
- Created: 2017-04-17T07:34:05.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2023-03-07T05:11:19.000Z (over 1 year ago)
- Last Synced: 2024-06-21T15:42:08.244Z (5 months ago)
- Topics: golang, http, middleware, session, session-management
- Language: Go
- Homepage:
- Size: 210 KB
- Stars: 11
- Watchers: 3
- Forks: 1
- Open Issues: 7
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# session
[![codecov](https://codecov.io/gh/moonrhythm/session/branch/master/graph/badge.svg)](https://codecov.io/gh/moonrhythm/session)
[![Go Report Card](https://goreportcard.com/badge/github.com/moonrhythm/session)](https://goreportcard.com/report/github.com/moonrhythm/session)
[![GoDoc](https://godoc.org/github.com/moonrhythm/session?status.svg)](https://godoc.org/github.com/moonrhythm/session)Session Middleware for Golang
## Example with Middleware
```go
package mainimport (
"fmt"
"log"
"net/http"
"time""github.com/moonrhythm/session"
"github.com/moonrhythm/session/store"
)func main() {
mux := http.NewServeMux()
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/" {
http.NotFound(w, r)
return
}s, _ := session.Get(r.Context(), "sess")
cnt := s.GetInt("counter")
cnt++
s.Set("counter", cnt)
w.Header().Set("Content-Type", "text/html")
fmt.Fprintf(w, "Couter: %d
Reset", cnt)
})
mux.HandleFunc("/reset", func(w http.ResponseWriter, r *http.Request) {
s, _ := session.Get(r.Context(), "sess")
s.Del("counter")
http.Redirect(w, r, "/", http.StatusFound)
})h := session.Middleware(session.Config{
Domain: "localhost",
HTTPOnly: true,
Secret: []byte("testsecret1234"),
MaxAge: time.Minute,
Path: "/",
Secure: session.PreferSecure,
Store: new(store.Memory),
})(mux)
// equals to
// h := session.New(session.Config{...}).Middleware()(mux)log.Fatal(http.ListenAndServe(":8080", h))
}```
## Example with Manager
```go
package mainimport (
"fmt"
"log"
"net/http"
"time""github.com/moonrhythm/session"
"github.com/moonrhythm/session/store"
)func main() {
mux := http.NewServeMux()m := session.New(session.Config{
Domain: "localhost",
HTTPOnly: true,
Secret: []byte("testsecret1234"),
MaxAge: time.Minute,
Path: "/",
Secure: session.PreferSecure,
Store: new(store.Memory),
})mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/" {
http.NotFound(w, r)
return
}s, _ := m.Get(r, "sess")
cnt := s.GetInt("counter")
cnt++
s.Set("counter", cnt)
m.Save(r.Context(), w, s)
w.Header().Set("Content-Type", "text/html")
fmt.Fprintf(w, "Couter: %d
Reset", cnt)
})
mux.HandleFunc("/reset", func(w http.ResponseWriter, r *http.Request) {
s, _ := m.Get(r, "sess")
s.Del("counter")
m.Save(r.Context(), w, s)
http.Redirect(w, r, "/", http.StatusFound)
})log.Fatal(http.ListenAndServe(":8080", mux))
}
```[See more examples](https://github.com/moonrhythm/session/tree/master/example)
## License
MIT