add session's readme
Showing
1 changed file
with
98 additions
and
0 deletions
session/README.md
0 → 100644
| 1 | sessionmanager | ||
| 2 | ============== | ||
| 3 | |||
| 4 | sessionmanager is a golang session manager. It can use session for many providers.Just like the `database/sql` and `database/sql/driver`. | ||
| 5 | |||
| 6 | ##How to install | ||
| 7 | |||
| 8 | go get github.com/astaxie/beego/session | ||
| 9 | |||
| 10 | |||
| 11 | ##how many providers support | ||
| 12 | Now this sessionmanager support memory/file/redis/mysql | ||
| 13 | |||
| 14 | |||
| 15 | |||
| 16 | ##How do we use it? | ||
| 17 | |||
| 18 | first you must import it | ||
| 19 | |||
| 20 | |||
| 21 | import ( | ||
| 22 | "github.com/astaxie/beego/session" | ||
| 23 | ) | ||
| 24 | |||
| 25 | then in you web app init the globalsession manager | ||
| 26 | |||
| 27 | var globalSessions *session.Manager | ||
| 28 | |||
| 29 | use memory as providers: | ||
| 30 | |||
| 31 | func init() { | ||
| 32 | globalSessions, _ = session.NewManager("memory", "gosessionid", 3600,"") | ||
| 33 | go globalSessions.GC() | ||
| 34 | } | ||
| 35 | |||
| 36 | use mysql as providers,the last param is the DNS, learn more from [mysql](https://github.com/Go-SQL-Driver/MySQL#dsn-data-source-name): | ||
| 37 | |||
| 38 | func init() { | ||
| 39 | globalSessions, _ = session.NewManager("mysql", "gosessionid", 3600,"username:password@protocol(address)/dbname?param=value") | ||
| 40 | go globalSessions.GC() | ||
| 41 | } | ||
| 42 | |||
| 43 | use file as providers,the last param is the path where to store the file: | ||
| 44 | |||
| 45 | func init() { | ||
| 46 | globalSessions, _ = session.NewManager("file", "gosessionid", 3600,"./tmp") | ||
| 47 | go globalSessions.GC() | ||
| 48 | } | ||
| 49 | |||
| 50 | use redis as providers,the last param is the redis's conn address: | ||
| 51 | |||
| 52 | func init() { | ||
| 53 | globalSessions, _ = session.NewManager("redis", "gosessionid", 3600,"127.0.0.1:6379") | ||
| 54 | go globalSessions.GC() | ||
| 55 | } | ||
| 56 | |||
| 57 | at last in the handlerfunc you can use it like this | ||
| 58 | |||
| 59 | func login(w http.ResponseWriter, r *http.Request) { | ||
| 60 | sess := globalSessions.SessionStart(w, r) | ||
| 61 | defer sess.SessionRelease() | ||
| 62 | username := sess.Get("username") | ||
| 63 | fmt.Println(username) | ||
| 64 | if r.Method == "GET" { | ||
| 65 | t, _ := template.ParseFiles("login.gtpl") | ||
| 66 | t.Execute(w, nil) | ||
| 67 | } else { | ||
| 68 | fmt.Println("username:", r.Form["username"]) | ||
| 69 | sess.Set("username", r.Form["username"]) | ||
| 70 | fmt.Println("password:", r.Form["password"]) | ||
| 71 | } | ||
| 72 | } | ||
| 73 | |||
| 74 | |||
| 75 | |||
| 76 | ##How to write own provider | ||
| 77 | When we develop a web app, maybe you want to write a provider because you must meet the requirements. | ||
| 78 | |||
| 79 | Write a provider is so easy. You only define two struct type(Session and Provider),which satisfy the interface definition.Maybe The memory provider is a good example for you. | ||
| 80 | |||
| 81 | type SessionStore interface { | ||
| 82 | Set(key, value interface{}) error //set session value | ||
| 83 | Get(key interface{}) interface{} //get session value | ||
| 84 | Delete(key interface{}) error //delete session value | ||
| 85 | SessionID() string //back current sessionID | ||
| 86 | SessionRelease() // release the resource | ||
| 87 | } | ||
| 88 | |||
| 89 | type Provider interface { | ||
| 90 | SessionInit(maxlifetime int64, savePath string) error | ||
| 91 | SessionRead(sid string) (SessionStore, error) | ||
| 92 | SessionDestroy(sid string) error | ||
| 93 | SessionGC() | ||
| 94 | } | ||
| 95 | |||
| 96 | ##LICENSE | ||
| 97 | |||
| 98 | BSD License http://creativecommons.org/licenses/BSD/ | ||
| ... | \ No newline at end of file | ... | \ No newline at end of file |
-
Please register or sign in to post a comment