Skip to content

Commit

Permalink
Add README
Browse files Browse the repository at this point in the history
  • Loading branch information
jinzhu committed Jun 28, 2017
1 parent 09ae3de commit d738c8a
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 8 deletions.
8 changes: 8 additions & 0 deletions LICENSE.txt
@@ -0,0 +1,8 @@
The MIT License (MIT)
Copyright (c) The Plant https://theplant.jp

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
76 changes: 69 additions & 7 deletions README.md
Expand Up @@ -2,14 +2,76 @@

Session Management for QOR

It wrap other session management into a common interface, allow you to use https://github.com/alexedwards/scs, http://www.gorillatoolkit.org/pkg/sessions as the backend
It wrapped other libs like [SCS](https://github.com/alexedwards/scs), [Gorilla Session](http://www.gorillatoolkit.org/pkg/sessions) into a common interface, which will be used for QOR libs and your application.

## Basic Usage

Add(request, name, value string/interface{})
Pop(request, name) string
Get(request, name) string
```go
import (
"github.com/gorilla/sessions"
"github.com/qor/session/gorilla"
// "github.com/alexedwards/scs/engine/memstore"
)

Flash(request, session.Message{Message: message, Type: "warning"}) // alias of Add
Load(request, name, struct{}) // alias of Get
var SessionManager = session.ManagerInterface

Save(r, w) // in middleware
func main() {
// Use gorilla session as the backend
engine := sessions.NewCookieStore([]byte("something-very-secret"))
SessionManager = gorilla.New("_session", engine)
// Use SCS as the backend
// engine := memstore.New(0)
// SessionManager := scs.New(engine)

mux := http.NewServeMux()
mux.HandleFunc("/put", putHandler)
mux.HandleFunc("/get", getHandler)
// Your routes

// Wrap your application's handlers or router with manager's Middleware
http.ListenAndServe(":7000", manager.Middleware(mux))
}

func putHandler(w http.ResponseWriter, req *http.Request) {
SessionManager.Add(req, "key", "value")
}

func getHandler(w http.ResponseWriter, req *http.Request) {
value := SessionManager.Get(req, "key")
io.WriteString(w, value)
}
```

## Common Interface

```go
type ManagerInterface interface {
// Add value to session data, if value is not string, will marshal it into JSON encoding and save it into session data.
Add(req *http.Request, key string, value interface{}) error

// Get value from session data
Get(req *http.Request, key string) string

// Pop value from session data
Pop(req *http.Request, key string) string

// Flash add flash message to session data
Flash(req *http.Request, message Message) error

// Flashes returns a slice of flash messages from session data
Flashes(req *http.Request) []Message

// Load get value from session data and unmarshal it into result
Load(req *http.Request, key string, result interface{}) error

// PopLoad pop value from session data and unmarshal it into result
PopLoad(req *http.Request, key string, result interface{}) error

// Middleware returns a new session manager middleware instance.
Middleware(http.Handler) http.Handler
}
```

## License

Released under the [MIT License](http://opensource.org/licenses/MIT).
11 changes: 10 additions & 1 deletion session_manager.go
Expand Up @@ -4,15 +4,24 @@ import "net/http"

// ManagerInterface session manager interface
type ManagerInterface interface {
// Add value to session data, if value is not string, will marshal it into JSON encoding and save it into session data.
Add(req *http.Request, key string, value interface{}) error
Pop(req *http.Request, key string) string
// Get value from session data
Get(req *http.Request, key string) string
// Pop value from session data
Pop(req *http.Request, key string) string

// Flash add flash message to session data
Flash(req *http.Request, message Message) error
// Flashes returns a slice of flash messages from session data
Flashes(req *http.Request) []Message

// Load get value from session data and unmarshal it into result
Load(req *http.Request, key string, result interface{}) error
// PopLoad pop value from session data and unmarshal it into result
PopLoad(req *http.Request, key string, result interface{}) error

// Middleware returns a new session manager middleware instance.
Middleware(http.Handler) http.Handler
}

Expand Down

0 comments on commit d738c8a

Please sign in to comment.