我如何在不同的路径下运行这个图像代理? [英] How do I run this image proxy under a different path?
问题描述
我使用 https://github.com/willnorris/imageproxy 来获取图片并调整图片大小代表用户。默认的应用程序工作,但我想这与现有的服务器,并将路径更改为/代理/,因为/将用于我的主应用程序。我也不想单独运行它,因为它实际上只是我需要的几行。我有:
pre $ p:= imageproxy.NewProxy(nil,nil)
p.SignatureKey = [] byte( secret key)
p.Timeout = 10 * time.Second
router:= mux.NewRouter()。StrictSlash(true)
router.NewRoute()。Name (代理)。方法(GET)。路径(/代理/)。处理程序(p)
服务器:=& http.Server {
地址: 127.0.0.1:8000,
Handler:router,
}
I每个图像都会收到404页面未找到。将其更改为:
server:=& http.Server {
Addr:localhost:8000,
处理程序:p,
}
log.Fatal(server.ListenAndServe())
有效。是否有可能改变路径?
使用 http.StripPrefix 在调用图片代理处理程序之前从请求路径中删除/ proxy:
<$ p (/ proxy),p>
router.NewRoute()。Name(proxy)。 ))
另外,使用 PathPrefix ,而不是/ proxy下的所有路径上匹配的路径。
I am using https://github.com/willnorris/imageproxy to fetch and resize images on behalf of users. The default app works but I'd like to integrate this with an existing server and change the path to "/proxy/" since "/" will be used for my main app. I also don't want to have to run this separately since it's literally just a few lines I need. I have:
p := imageproxy.NewProxy(nil, nil)
p.SignatureKey = []byte("secret key")
p.Timeout = 10 * time.Second
router := mux.NewRouter().StrictSlash(true)
router.NewRoute().Name("proxy").Methods("GET").Path("/proxy/").Handler(p)
server := &http.Server{
Addr: "127.0.0.1:8000",
Handler: router,
}
I receive "404 page not found" for every image. Changing it to:
server := &http.Server{
Addr: "localhost:8000",
Handler: p,
}
log.Fatal(server.ListenAndServe())
works. Is it possible to do change the path?
Use http.StripPrefix to remove "/proxy" from the request path before invoking the image proxy handler:
router.NewRoute().Name("proxy").Methods("GET").PathPrefix("/proxy/").Handler(http.StripPrefix("/proxy", p))
Also, use PathPrefix instead of Path for match on all paths below "/proxy".
这篇关于我如何在不同的路径下运行这个图像代理?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!