使用Gorilla工具包提供静态内容和处理404未找到错误。

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

Serving static content and handling 404 not found with Gorilla toolkit

问题

最近我问过关于使用Gorilla mux来提供静态内容和处理404错误的问题;当使用Handle而不是PathPrefix时,应用程序可以提供根页面(http://localhost:8888):

func main() {
    r := mux.NewRouter()
    r.HandleFunc("/myService", ServiceHandler)
    r.Handle("/", http.FileServer(http.Dir("./static")))
    r.NotFoundHandler = http.HandlerFunc(notFound)
    l, _ := net.Listen("tcp", "8888")
    http.Serve(l, r)
}

然而,对于根页面内的页面请求(例如http://localhost:8888/demo/page1.html),会被404处理程序拦截。有没有办法在捕获不存在的页面或服务请求的同时防止这种情况发生?这是目录结构:

...
main.go
static\
  | index.html
  demo\
    page1.html
    demo.js
    demo.css
    | jquery\
       | <js files>
    | images\
       | <png files>

之前的问题:

我正在使用Gorilla mux工具包来处理Web服务器应用程序中的HTTP请求:

func main() {
    r := mux.NewRouter()
    r.HandleFunc("/myService", ServiceHandler)
    r.PathPrefix("/").Handler(http.FileServer(http.Dir("./static")))
    l, _ := net.Listen("tcp", "8888")
    http.Serve(l, r)
}

我想为无效的URL添加一个处理程序,但它从未被调用:

func main() {
    r := mux.NewRouter()
    r.HandleFunc("/myService", ServiceHandler)
    r.NotFoundHandler = http.HandlerFunc(notFound)
    r.PathPrefix("/").Handler(http.FileServer(http.Dir("static")))
    l, _ := net.Listen("tcp", "8888")
    http.Serve(l, r)
}

如果我移除静态处理程序,未找到处理程序会被调用。然而,应用程序需要从非绝对路径提供静态内容。有没有办法将其与404处理结合起来?

英文:

I recently asked about serving static content and handling 404 with Gorilla mux; when using Handle instead of PathPrefix, the app can serve the root page (http://localhost:8888):

func main() {
    r := mux.NewRouter()
    r.HandleFunc(&quot;/myService&quot;, ServiceHandler)
    r.Handle(&quot;/&quot;, http.FileServer(http.Dir(&quot;./static&quot;)))
    r.NotFoundHandler = http.HandlerFunc(notFound)
    l, _ := net.Listen(&quot;tcp&quot;, &quot;8888&quot;)
    http.Serve(l, r)
}

However requests for pages within the root page (e.g. http://localhost:8888/demo/page1.html) get intercepted by the 404 handler. Is there any way to prevent this, while catching requests for non-existent pages or services? This is the directory structure:

...
main.go
static\
  | index.html
  demo\
    page1.html
    demo.js
    demo.css
    | jquery\
       | &lt;js files&gt;
    | images\
       | &lt;png files&gt;

Previous Question:

I am using the Gorilla mux toolkit to handle http requests in a web server app:

func main() {
    r := mux.NewRouter()
    r.HandleFunc(&quot;/myService&quot;, ServiceHandler)
    r.PathPrefix(&quot;/&quot;).Handler(http.FileServer(http.Dir(&quot;./static&quot;)))
    l, _ := net.Listen(&quot;tcp&quot;, &quot;8888&quot;)
    http.Serve(l, r)
}

I want to add a handler for invalid URLS, but it is never called:

func main() {
    r := mux.NewRouter()
    r.HandleFunc(&quot;/myService&quot;, ServiceHandler)
    r.NotFoundHandler = http.HandlerFunc(notFound)
    r.PathPrefix(&quot;/&quot;).Handler(http.FileServer(http.Dir(&quot;static&quot;)))
    l, _ := net.Listen(&quot;tcp&quot;, &quot;8888&quot;)
    http.Serve(l, r)
}

If I remove the static handler, the not found handler is called. However, the app needs to serve the static content, from a non-absolute path. Is there a way to combine that with 404 handling?

答案1

得分: 1

我怀疑r.PathPrefix("/") .Handler()会匹配任何路径,使得notfound处理程序无效。

route.go中所述:

//请注意,它不会特殊处理斜杠(“/foobar/”将与前缀“/foo”匹配),因此您可能希望在此处使用尾部斜杠。

如果您正在使用PathPrefix(如这些测试中所示),请将其用于特定路径,而不是通用的“/”。

英文:

I suspect r.PathPrefix(&quot;/&quot;).Handler() would match any path, making the notfound handler useless.

As mentioned in "route.go":

// Note that it does not treat slashes specially 
// (&quot;`/foobar/`&quot; will be matched by the prefix &quot;`/foo`&quot;) 
// so you may want to use a trailing slash here.

If you are using PathPrefix (as in those tests), use it for a specific path, not for the generic "/".

huangapple
  • 本文由 发表于 2015年5月5日 19:41:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/30052002.html
匿名

发表评论

匿名网友

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

确定