Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/eucalytus/session
https://github.com/eucalytus/session
gin session web
Last synced: 2 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/eucalytus/session
- Owner: eucalytus
- License: apache-2.0
- Created: 2019-05-29T13:01:11.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2023-02-09T20:32:12.000Z (almost 2 years ago)
- Last Synced: 2024-09-30T07:21:33.678Z (3 months ago)
- Topics: gin, session, web
- Language: Go
- Size: 28.3 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# session
![GitHub](https://img.shields.io/github/license/eucalytus/session.svg)
[![Language](https://img.shields.io/badge/Language-Go-blue.svg)](https://golang.org/)
[![Go Report Card](https://goreportcard.com/badge/github.com/eucalytus/session)](https://goreportcard.com/report/github.com/eucalytus/session)
[![Build Status](https://travis-ci.org/eucalytus/session.svg?branch=master)](https://travis-ci.org/eucalytus/session)
![CircleCI](https://img.shields.io/circleci/build/github/eucalytus/session/master.svg)
![Codecov](https://img.shields.io/codecov/c/github/eucalytus/session.svg)# How to use it
```go
package mainimport (
"log"
"net/http""github.com/eucalytus/session"
)func main() {
manager := session.NewManager(session.Options{
MaxInactiveInterval: 1800, MaxAge: 84000, HttpOnly: true,
},
session.CreateMemSession,
//listen session event
func(s session.Session, event int) {
if event == session.Created {
log.Printf("new session is created, sessionId=%s\n", s.GetMaskedSessionId())
} else if event == session.Destroyed {
log.Printf("session is destroyed, sessionId=%s\n", s.GetMaskedSessionId())
} else {
log.Printf("session is updated, sessionId=%s\n", s.GetMaskedSessionId())
}
},
)//private resource
http.HandleFunc("/private", func(response http.ResponseWriter, request *http.Request) {
s := manager.GetSession(request)
if s != nil {
if _, found := s.Get("key"); found {
response.Write([]byte("OK"))
return
}
}
response.WriteHeader(http.StatusUnauthorized)
response.Write([]byte("StatusUnauthorized"))
})//login
http.HandleFunc("/login", func(response http.ResponseWriter, request *http.Request) {
s := manager.GetSession(request)
if s == nil {
temp, err := manager.CreateSession(request, response)
if err != nil {
log.Printf("create session failed: %v\n", err)
}
s = temp
}
s.Set("key", "login")
response.Write([]byte("OK"))
})//logout
http.HandleFunc("/logout", func(response http.ResponseWriter, request *http.Request) {
s := manager.GetSession(request)
if s != nil {
s.Set("key", nil)
s.Invalidate()
}
response.Write([]byte("OK"))
})http.ListenAndServe("0.0.0.0:8000", nil)
}```