40

The problem I'm seeing is that I'm trying to use the http.FileServer with the Gorilla mux Router.Handle function.

This doesn't work (the image returns a 404)..

myRouter := mux.NewRouter()
myRouter.Handle("/images/", http.StripPrefix("/images/", http.FileServer(http.Dir(HomeFolder + "images/"))))

this works (the image is shown ok)..

http.Handle("/images/", http.StripPrefix("/images/", http.FileServer(http.Dir(HomeFolder + "images/"))))

Simple go web server program below, showing the problem...

package main

import (
    "fmt"
    "net/http"
    "io"
    "log"
    "github.com/gorilla/mux"
)

const (
    HomeFolder = "/root/test/"
)

func HomeHandler(w http.ResponseWriter, req *http.Request) {
    io.WriteString(w, htmlContents)
}

func main() {

    myRouter := mux.NewRouter()
    myRouter.HandleFunc("/", HomeHandler)
    //
    // The next line, the image route handler results in 
    // the test.png image returning a 404.
    // myRouter.Handle("/images/", http.StripPrefix("/images/", http.FileServer(http.Dir(HomeFolder + "images/"))))
    //
    myRouter.Host("mydomain.com")
    http.Handle("/", myRouter)

    // This method of setting the image route handler works fine.
    // test.png is shown ok.
    http.Handle("/images/", http.StripPrefix("/images/", http.FileServer(http.Dir(HomeFolder + "images/"))))

    // HTTP - port 80
    err := http.ListenAndServe(":80", nil)

    if err != nil {
        log.Fatal("ListenAndServe: ", err)
        fmt.Printf("ListenAndServe:%s\n", err.Error())
    }
}

const htmlContents = `<!DOCTYPE HTML>
<html lang="en">
  <head>
    <title>Test page</title>
    <meta charset = "UTF-8" />
  </head>
  <body>
    <p align="center">
        <img src="/images/test.png" height="640" width="480">
    </p>
  </body>
</html>
`
dodgy_coder
  • 12,407
  • 10
  • 54
  • 67

2 Answers2

68

I posted this on golang-nuts discussion group and got this solution from Toni Cárdenas ...

The standard net/http ServeMux (which is the standard handler you are using when you use http.Handle) and the mux Router have different ways of matching an address.

See the differences between http://golang.org/pkg/net/http/#ServeMux and http://godoc.org/github.com/gorilla/mux.

So basically, http.Handle('/images/', ...) matches '/images/whatever', while myRouter.Handle('/images/', ...) only matches '/images/', and if you want to handle '/images/whatever', you have to ...

Option 1 - Use a regular expression match in your router

myRouter.Handle("/images/{rest}",
     http.StripPrefix("/images/", http.FileServer(http.Dir(HomeFolder + "images/"))))

Option 2 - Use the PathPrefix method on your router:

myRouter.PathPrefix("/images/").Handler(http.StripPrefix("/images/", 
     http.FileServer(http.Dir(HomeFolder + "images/"))))
Rob Bell
  • 3,542
  • 5
  • 28
  • 49
dodgy_coder
  • 12,407
  • 10
  • 54
  • 67
  • 4
    +1 #2 has been successful for me in a current project (before I stumbled upon this answer. just reassuring the reader(s) that #2 is what I am using and works). – eduncan911 Jun 04 '14 at 16:59
  • Just a note to add, please remember if you are a golang newb like me, golang does not like statements as formatted by in #2. I needed combine all three lines into one line (no changes in syntax, only changes in whitespace) before golang would stop complaining about expecting commas. #2 is the right answer, it is just formated in such a way that will cause problems for newbies and novices like myself. – Owen Ivory Dec 21 '17 at 19:24
  • You're right, I had to put the make the statement a single line. – farhany Mar 02 '18 at 17:28
  • 1
    Thank you so much for putting me out of hours of misery, the PathPrefix and StripPrefix thing worked for me, I went from `r.Handle("/", fs)` to a `r.PathPrefix("/").Handler(http.StripPrefix("/", fs))` – SamGamgee May 17 '19 at 11:24
  • @SamGamgee - you can go one step further and drop the `http.StripPrefix()` so that you end up with `r.PathPrefix("/").Handler(fs)` – Rob Bell Apr 10 '20 at 21:21
0

As of May 2015 gorilla/mux package still have no version releases. But problem is different now. It is not that myRouter.Handle does not match url and needs regexp, it does! But http.FileServer requires prefix to be removed from url. Below example works fine.

ui := http.FileServer(http.Dir("ui"))
myRouter.Handle("/ui/", http.StripPrefix("/ui/", ui))

Note, there is no /ui/{rest} in abowe example. You may also wrap http.FileServer into logger gorilla/handler and see request to coming to FileServer and response 404 going out.

ui := handlers.CombinedLoggingHandler(os.Stderr,http.FileServer(http.Dir("ui"))
myRouter.Handle("/ui/", ui) // getting 404
// works with strip: myRouter.Handle("/ui/", http.StripPrefix("/ui/", ui))
smile-on
  • 2,073
  • 1
  • 20
  • 20
  • 1
    The default `Handle` method of `router` still creates a `route` the same way as before. I tried the method you stated above and `"/ui/"` will not match all paths with the `"/ui/"` prefix. @dodgy_coder's solution is correct. Either include the regex with the default `Handler` method or use `PathPrefix` – thecalvinchan May 11 '15 at 01:14