Gorilla Mux Regex(大猩猩Mux正则表达式)

huangapple go评论94阅读模式
英文:

Gorilla Mux Regex

问题

我正在使用Golang Gorilla Toolkit中的Mux包来处理我的路由。

考虑以下路由:

m.HandleFunc("/admin/install", installHandler).Methods("GET")
m.HandleFunc("/admin/^((?!install).)*$", adminHandler).Methods("GET")
m.HandleFunc("/admin", adminHandler).Methods("GET")

问题出在中间路由的正则表达式上 - 它没有被解释,所以这个路由将无法工作!

m.HandleFunc("/admin/{^((?!install).)*$}", adminHandler).Methods("GET")

使用{}花括号也不起作用。它被忽略,并被视为/admin/。

以下方式也不行:

m.HandleFunc("/admin/{_dummy:^((?!install).)*$}", adminHandler).Methods("GET")

简而言之,我在这里尝试的是首先匹配/admin/install路由,然后使用正则表达式从下面的路由中排除该精确路由,但它不起作用。

有没有办法在gorilla mux包中使用正则表达式?

英文:

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

Consider the following routes:

m.HandleFunc("/admin/install", installHandler).Methods("GET")
m.HandleFunc("/admin/^((?!install).)*$", adminHandler).Methods("GET")
m.HandleFunc("/admin", adminHandler).Methods("GET")

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

m.HandleFunc("/admin/{^((?!install).)*$}", adminHandler).Methods("GET")

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

Neither does:

m.HandleFunc("/admin/{_dummy:^((?!install).)*$}", adminHandler).Methods("GET")

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?

答案1

得分: 5

实际上可以这样做:

m.HandleFunc(`/{_dummy:admin\/([^install]*).*}`, adminHandler).Methods("GET")

编辑:

作为对VonC评论的回答,这里有一个示例go应用程序:https://play.golang.org/p/nYWNADK7Sr

在本地计算机上运行它。尝试以下路由:

  • http://localhost:8080/admin/ - (返回"adminHandler")
  • http://localhost:8080/admin/something - (返回"adminHandler")
  • http://localhost:8080/admin/install - (返回"installHandler")

所以是的,VonC,它确实解决了特定的问题:

"首先匹配/admin/install路由,然后我想从下面的路由中排除该确切路由"

但是正确的是,它并不意味着“不跟随单词install”-这只是一种可能的替代方法,它在re2语法的范围内是可行的。如果在URL中出现该单词,只是简单地“忽略”或排除install。

英文:

It is actually possible to do this:

m.HandleFunc(`/{_dummy:admin\/([^install]*).*}`, adminHandler).Methods("GET")

Edit:

As my answer to VonC's comment above, here a sample go app: https://play.golang.org/p/nYWNADK7Sr

Run it on your local pc. Try the following routes:

http://localhost:8080/admin/ - (returns "adminHandler")
http://localhost:8080/admin/something - (returns "adminHandler")
http://localhost:8080/admin/install - (returns "installHandler")

So yes VonC, it does solve the specific problem:

> "to first match the /admin/install route, and that exact route I then
> want to exclude from the route below"

But it is correct that it doesn't mean "not followed by the word, install" - this is just an alternative approach which is possible within the bounds of the re2 syntax. To simply "ignore" or exclude the word install, if it happens to show up in the url.

答案2

得分: 1

它不起作用是因为golang的正则表达式遵循re2语法,该语法不支持前瞻或后顾。

你可能需要先为/admin/install定义一个处理程序。
然后,所有其他的/admin/xxx将用于其他路由(即非/admin/install)。

实际上,OP SK84评论中补充说:

> 即使首先定义了admin/install,/admin/也会被执行。这就是我想要避免的。
>
> 我想我需要在admin处理程序中编写代码来解决这个问题-很简单,但不如本来可以工作的那么漂亮。

英文:

It doesn't work because golang regexp follows the re2 syntax, which doesn't support lookahead or lookbehind.

You might need to define an handler for /admin/install first.
Then all others /admin/xxx would be for other routes (ie not /admin/install)

Actually, the OP SK84 adds in the comments:

> Even if admin/install is defined first, /admin/ will also be executed. That's what I wanted to avoid.
>
> I guess I need to code around it inside the admin handler then - easy, but not as pretty as this would have been if it had worked.

huangapple
  • 本文由 发表于 2015年3月10日 03:50:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/28950606.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定