The Go Programming Language
http://golang.org/
Go Playground
Go Projects
Revel Web Framework
lyrictian

又造了一个 Golang 的轮子 Session 库!使用简单!支持多存储!

  •  
  •   lyrictian ·
    LyricTian · Jun 13, 2018 · 3809 views
    This topic created in 2915 days ago, the information mentioned may be changed or developed.

    https://github.com/go-session/session

    基于简单使用,易扩展的目的!提供了多存储的支持,内存存储、文件存储、redis 存储、cookie 存储,后续后增加 mysql、mongodb 的支持。以及多种中间件的支持,有 gin、echo、beego、gear。欢迎来踩!!!

    下面给出一个使用示例:

    package main
    
    import (
    	"context"
    	"fmt"
    	"net/http"
    
    	"github.com/go-session/session"
    )
    
    func main() {
    	http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
    		store, err := session.Start(context.Background(), w, r)
    		if err != nil {
    			fmt.Fprint(w, err)
    			return
    		}
    
    		store.Set("foo", "bar")
    		err = store.Save()
    		if err != 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)
    		if err != nil {
    			fmt.Fprint(w, err)
    			return
    		}
    
    		foo, ok := store.Get("foo")
    		if ok {
    			fmt.Fprintf(w, "foo:%s", foo)
    			return
    		}
    		fmt.Fprint(w, "does not exist")
    	})
    
    	http.ListenAndServe(":8080", nil)
    }
    
    2 replies    2018-06-14 13:15:41 +08:00
    FrankAdler
        1
    FrankAdler  
       Jun 14, 2018
    mark 一下, 现在都是在写无状态 api, 暂时用不到 session
    my3157
        2
    my3157  
       Jun 14, 2018
    也是 rest api , 把 go-macaron/cache 魔改了 处理 access token
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   1352 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 37ms · UTC 17:11 · PVG 01:11 · LAX 10:11 · JFK 13:11
    ♥ Do have faith in what you're doing.