首先让我说我是 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/
Rails2.3可以选择随时使用RouteSet#add_configuration_file添加更多路由。是否可以在Rails3项目中做同样的事情? 最佳答案 在config/application.rb中:config.paths.config.routes在Rails3.2(也可能是Rails3.1)中,使用:config.paths["config/routes"] 关于ruby-on-rails-Rails3中的多个路由文件,我们在StackOverflow上找到一个类似的问题
我有多个ActiveRecord子类Item的实例数组,我需要根据最早的事件循环打印。在这种情况下,我需要打印付款和维护日期,如下所示:ItemAmaintenancerequiredin5daysItemBpaymentrequiredin6daysItemApaymentrequiredin7daysItemBmaintenancerequiredin8days我目前有两个查询,用于查找maintenance和payment项目(非排他性查询),并输出如下内容:paymentrequiredin...maintenancerequiredin...有什么方法可以改善上述(丑陋的)代
我需要从一个View访问多个模型。以前,我的links_controller仅用于提供以不同方式排序的链接资源。现在我想包括一个部分(我假设)显示按分数排序的顶级用户(@users=User.all.sort_by(&:score))我知道我可以将此代码插入每个链接操作并从View访问它,但这似乎不是“ruby方式”,我将需要在不久的将来访问更多模型。这可能会变得很脏,是否有针对这种情况的任何技术?注意事项:我认为我的应用程序正朝着单一格式和动态页面内容的方向发展,本质上是一个典型的网络应用程序。我知道before_filter但考虑到我希望应用程序进入的方向,这似乎很麻烦。最终从任何
刚入门rails,开始慢慢理解。有人可以解释或给我一些关于在application_controller中编码的好处或时间和原因的想法吗?有哪些用例。您如何为Rails应用程序使用应用程序Controller?我不想在那里放太多代码,因为据我了解,每个请求都会调用此Controller。这是真的? 最佳答案 ApplicationController实际上是您应用程序中的每个其他Controller都将从中继承的类(尽管这不是强制性的)。我同意不要用太多代码弄乱它并保持干净整洁的态度,尽管在某些情况下ApplicationContr
我有一个具有一些属性的模型:attr1、attr2和attr3。我需要在不执行回调和验证的情况下更新此属性。我找到了update_column方法,但我想同时更新三个属性。我需要这样的东西:update_columns({attr1:val1,attr2:val2,attr3:val3})代替update_column(attr1,val1)update_column(attr2,val2)update_column(attr3,val3) 最佳答案 您可以使用update_columns(attr1:val1,attr2:val2
我正在尝试修改当前依赖于定义为activeresource的gem:s.add_dependency"activeresource","~>3.0"为了让gem与Rails4一起工作,我需要扩展依赖关系以与activeresource的版本3或4一起工作。我不想简单地添加以下内容,因为它可能会在以后引起问题:s.add_dependency"activeresource",">=3.0"有没有办法指定可接受版本的列表?~>3.0还是~>4.0? 最佳答案 根据thedocumentation,如果你想要3到4之间的所有版本,你可以这
当我在我的Rails应用程序根目录中运行rakedoc:app时,API文档是使用/doc/README_FOR_APP作为主页生成的。我想向该文件添加.rdoc扩展名,以便它在GitHub上正确呈现。更好的是,我想将它移动到应用程序根目录(/README.rdoc)。有没有办法通过修改包含的rake/rdoctask任务在我的Rakefile中执行此操作?是否有某个地方可以查找可以修改的主页文件的名称?还是我必须编写一个新的Rake任务?额外的问题:Rails应用程序的两个单独文件/README和/doc/README_FOR_APP背后的逻辑是什么?为什么不只有一个?
我没有找到太多关于如何执行此操作的信息,尽管有很多关于如何使用像这样的redirect_to将参数传递给重定向的建议:action=>'something',:controller=>'something'在我的应用程序中,我在路由文件中有以下内容match'profile'=>'User#show'我的表演Action是这样的defshow@user=User.find(params[:user])@title=@user.first_nameend重定向发生在同一个用户Controller中,就像这样defregister@title="Registration"@user=Use
我正在尝试按0-9和a-z的顺序创建数字和字母列表。我有一组值value_array=['0','1','2','3','4','5','6','7','8','9','a','b','光盘','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','','u','v','w','x','y','z']和一个组合列表的数组,按顺序,这些数字可以产生x个字符,比方说三个list_array=[]和一个当前字母和数字组合的数组(在将它插入列表数组之前我会把它变成一个字符串,]current_combo['0','0','0']
是否有可能:before_filter:authenticate_user!||:authenticate_admin! 最佳答案 before_filter:do_authenticationdefdo_authenticationauthenticate_user!||authenticate_admin!end 关于ruby-on-rails-before_filter运行多个方法,我们在StackOverflow上找到一个类似的问题: https://