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

2018-06-13 23:15:32 +08:00
 lyrictian

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)
}
2950 次点击
所在节点    Go 编程语言
2 条回复
FrankAdler
2018-06-14 13:05:49 +08:00
mark 一下, 现在都是在写无状态 api, 暂时用不到 session
my3157
2018-06-14 13:15:41 +08:00
也是 rest api , 把 go-macaron/cache 魔改了 处理 access token

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/462921

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX