user776942
user776942

Reputation:

How do I run this image proxy under a different path?

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?

Upvotes: 1

Views: 183

Answers (1)

Thundercat
Thundercat

Reputation: 120999

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".

Upvotes: 2

Related Questions