gpt4 book ai didi

web-applications - 跨多个包的全局 session 管理的命名空间/范围问题

转载 作者:数据小太阳 更新时间:2023-10-29 03:37:39 26 4
gpt4 key购买 nike

首先让我说我是 Golang 的新手。现在使用它几个星期了。真的很喜欢这种语言,但是...

我在使用 Golang 进行全局 session 管理时遇到了一些问题。我看到它是如何工作的,如果范围全部在一个包中,我可以让它工作,但是我最近刚刚为我的每个 go 文件创建了新包。我这样做是因为我读到这是最佳实践并且有利于可重用性。

自从我将 go 文件移动到它们自己的包而不是一个包中后, session 管理就崩溃了。它看起来每次都创建一个新 session ,而不是重复使用现有 session 。这里有一些代码可以让您了解我在做什么:

package main

import (
"net/http"
"api/login"
"api/globalsessionkeeper"
"github.com/astaxie/beego/session"
)

func main() {
http.HandleFunc("/login", login.DoLogin)

og.Fatal(http.ListenAndServe(":8000", nil))
}

func Index(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Hello, %q", html.EscapeString(r.URL.Path))
}

func init() {
var err error
fmt.Println("Session init")
globalsessionkeeper.GlobalSessions, err = session.NewManager("mysql", `{"enableSetCookie":true, "SessionOn":true, "cookieName":"globalsession_id","gclifetime":120,"ProviderConfig":"root@tcp(172.16.0.23:3306)/databse"}`)
if err != nil {
fmt.Printf("Error")
}
globalsessionkeeper.GlobalSessions.SetSecure(true)
go globalsessionkeeper.GlobalSessions.GC()
}

package globalsessionkeeper(我创建这个只是为了我可以在所有其他包中重用全局变量..i.e“包登录”..等)

package globalsessionkeeper

import (
"github.com/astaxie/beego/session"
_ "github.com/astaxie/beego/session/mysql"
)

//Global Variable
var GlobalSessions *session.Manager

这是登录函数/包中的一个片段。这一切都在一个包中,工作得很好:

if validated == true {
//create session using the request data which includes the cookie/sessionid
sessionStore, err := globalsessionkeeper.GlobalSessions.SessionStart(w, r)
if err != nil {
//need logging here instead of print
fmt.Printf("Error, could not start session %v\n", err)
break
}
defer sessionStore.SessionRelease(w) //update db upon completion for request

if sessionStore.Get("uniquevalue") == nil {
//need logging here instead of print
fmt.Printf("uniquevalue not found, Saving Session, Get has %v\n", sessionStore)
fmt.Printf("uniquevalue not found, Saving Session, Get has %v\n", sessionStore.Get("uniquevalue"))
err = sessionStore.Set("uniquevalue", input.uniquevalue)
if err != nil {
//need logging here instead of print
fmt.Printf("Error while writing to DB, %v\n", err)
break
}
} else {
//need logging here instead of print
fmt.Printf("Found Session! Session uniquevalue = %v\n", sessionStore.Get("uniquevalue"))
}
//Send back 204 no content (with cookie)
w.WriteHeader(http.StatusNoContent)
} else {
fmt.Printf("Login Failed")
w.WriteHeader(http.StatusUnauthorized)
}

数据库有正确的条目。它为它创建的每个 session 存储了唯一的值。这里还有一些输出:

answer = true
uniquvalue not found, Saving Session, Get has &{0xc208046b80 f08u0489804984988494994 {{0 0} 0 0 0 0} map[]}
uniquevalue not found, Saving Session, Get has <nil>
2015/06/06 17:32:26 http: multiple response.WriteHeader calls

当然,multiple response.WriteHeader 是我必须更正的其他内容。 go 例程最初发送 200OK,但我想将其更改为 204 无内容,但是一旦我这样做,它就开始给我这个错误。

如有任何帮助,我们将不胜感激。谢谢!

最佳答案

事实证明这一直有效。我将错误的 cookie 值传递回 api。愚蠢的错误会导致各种头痛。

关于web-applications - 跨多个包的全局 session 管理的命名空间/范围问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30686076/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com