问题标签 [gorilla]

For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.

0 投票
1 回答
463 浏览

mgo - gorilla/schema and mgo/bson.ObjectId

Here's a bit of a chicken and egg problem.

In a HTML template, a form, bson.ObjectId needs to be rendered with {{mytype.Id.Hex()}}

e.g.

In Go when defining a struct that is supposed to be parsed by gorilla/schema

when you call (from schema) decoder.Decode(instance_of_mytype, r.PostForm) it "throws" an error: schema: invalid path "id" since the format is just the string respresentation of bson.ObjectId and not an actual bson.ObjectId.

I wonder what I could do except filling the fields manually (r.FormValue()) to make it work.

Should I create an issue with gorilla/schema or mgo or should I just do it manually?

0 投票
1 回答
4469 浏览

security - Golang WebSocket 应用程序中的身份验证

我正在尝试在主要使用 WebSockets 的应用程序中实现用户身份验证,但我不确定如何开始。

我正在使用 Gorilla mux 和 websocket 包。

我曾考虑过使用此处描述的方法(文件 main.go 和 auth.go),但是这种方法是否可以防止经过身份验证的用户以某种方式劫持彼此的套接字,就像本文中描述的那样?

有人可以在 Go 中推荐一个好的方法或包吗?

0 投票
1 回答
1905 浏览

rest - Go Gorilla Mux 未按预期路由

我在让 Go 的 Gorilla Mux 库正常工作时遇到问题。从我阅读的文档和我所做的所有调试中,我似乎无法弄清楚问题所在。这是我的路由:

文件夹结构:

main.go:

路线/路线.go

路线/user.go

每当我向服务器的根路径(即提供静态内容的路径)发出请求时,服务器都会按预期响应主页。但是,任何其他调用都会导致 404 响应(我使用 cURL 测试请求)。例如,对http://localhost:8000/user/new的格式错误的请求应该返回 409,但会返回 404。如果我期望得到 201 响应,则相同。

一切看起来都正确,我已经检查了三次,但我无法弄清楚这里的问题是什么。

0 投票
2 回答
2150 浏览

session - Gorilla Sessions - 如何根据请求自动更新 Cookie 过期时间?

我知道许多其他语言和 Web 框架会在每次通过后端访问会话(或类似的操作)时自动将 cookie 的过期时间更新为会话超时。我不相信 Gorilla 提供了这个实用程序。

我正在考虑编写一些请求中间件,如果它检测到有效会话,将延长 cookie 的生命周期,但我想知道是否有更好的方法来做到这一点。

更新 cookie 过期的最佳实践是什么,尤其是与 Gorilla/Go 相关的?

0 投票
2 回答
20939 浏览

go - golang http 恐慌的全局恢复处理程序

我想创建全局错误处理程序以通过电子邮件发送。

如何使其全球化。如果我知道错误会发生在哪里会很容易

但是,如果我不确切知道错误会发生在哪里,该怎么办?所以我应该添加一个全局recoverish 处理程序。但是具体怎么做我不知道。

更新

我添加defer recover到开头,main但它永远不会在请求时执行http://localhost:3001。所以恐慌不会通过电子邮件发送。

0 投票
1 回答
2555 浏览

go - 如何使用 golang Gorilla mux 找到 CSS 文件

我正在将 Go 与 Gorilla Mux 一起使用。

这是我的 webserver.go 文件

在 webserver.go 文件所在的同一文件夹中是 index.html 文件。

/ - 这里是 index.html

/css - 所有 CSS 文件

/images - 所有图片、资源文件

我设法使用上面的代码加载 index.html 文件,但它似乎没有加载 CSS 文件和图像。

在我拥有的 index.html 文件中。

所以它应该找到css文件还是我必须确保“Go”可以找到css和图像文件夹?如何?

0 投票
1 回答
1236 浏览

session - Go Gorilla Mux 会话名称

我很难理解 Gorillamux的会话名称。

http://www.gorillatoolkit.org/pkg/sessions#CookieStore.Get

我想使用 session 来避免在两个处理程序之间使用全局变量。所以我将键值保存在共享会话中并从会话中检索值。

我想知道我是否希望每个用户都有自己唯一的会话和它Values,我是否需要分配唯一的会话名称(会话 ID)?还是大猩猩会话自己处理每个用户都有自己的会话和值?

我想知道是否需要生成具有唯一标识符的会话名称。

谢谢

0 投票
1 回答
502 浏览

go - 带有单独处理程序的 PathPrefixed 子路由器

给定以下内容(Go playground 的完整示例):

我本来希望以下 URL 触发相应的处理程序,但我似乎无法使其工作:

不幸的是,最后两个显然都不是可路由的,它们都触发h.Show了,谁能指出我做错了什么?我可能已经预料到有一个无界的{uuid}(没有尾部斜杠)可能会继续运行,忽略/但似乎并非如此。

我什至不知道这是否与在 Github (#31) 上仍然开放的 Subrouter strict-slash 问题有关,但据我所知,我确实在那里尝试了替代方案。(即object.Methods("GET").Path("/").Handler(h.Show)

安装在object根目录上的处理程序是否可以Methods()防止任何进一步的路由匹配?

0 投票
1 回答
9207 浏览

nginx - Gorilla WebSocket 一分钟后断开连接

我在 nginx 1.4.6 反向代理后面使用 Go (Golang) 1.4.2 和 Gorilla WebSockets。打开页面大约一分钟后,我的 WebSocket 断开连接。在 Chrome 和 Firefox 上也会发生相同的行为。

起初,我在使用 WebSocket 连接服务器和客户端时遇到了问题。然后,我读到我需要调整我的 nginx 配置。这就是我所拥有的。

我的 Go 代码基本上是在回显客户的信息。(为简洁起见,省略了错误)。这是我的HandleFunc

JavaScript 也很简单。

Go 正在报告websocket: close 1006 unexpected EOF。我知道当我离开或刷新页面ReadJSON时返回EOF,但这似乎是一个不同的错误。此外,在打开页面大约一分钟后,意外的 EOF 会自行发生。

onerror在 JavaScript 中有一个函数。该事件不会触发,而是onclose触发。

0 投票
2 回答
7915 浏览

regex - Gorilla Mux Regex

I'm using the Mux package from the Golang Gorilla Toolkit for my routes.

Consider the following routes:

The problem is with the regex of the middle route - it is not interpreted, so the route will not work!

With the {} curly brackets doesn't work either. It is just ignored, and treated as /admin/

Neither does:

In short, what I'm trying to achieve here is to first match the /admin/install route, and that exact route I then want to exclude from the route below, using the regex, but it doesn't work.

Is there some way to use regex with the gorilla mux package?