6

I had a look a this question Serving static content with a root URL with the Gorilla toolkit and used the answer from there successfully.

I, though, want to declare explicitly which file I want to serve as static, for development purposes, like this:

router.PathPrefix("/style.css").Handler(http.FileServer(http.Dir("/usr/local/myproject/style.css")))

Which worked as expected too.

Now, I wonder, if it's a correct way to serve a single explicitly defined file or should I do it differently?

Community
  • 1
  • 1
Sergei Basharov
  • 51,276
  • 73
  • 200
  • 335
  • What response you get when you access something like `/style.css/abc` ? – Ankur Jan 19 '17 at 07:49
  • 1
    I guess rather using `PathPrefix` you should use `Path` router – Ankur Jan 19 '17 at 07:51
  • 6
    To serve a single file, a lighter / faster alternative is `http.ServeFile()`. See this answer for details: [Include js file in Go template](http://stackoverflow.com/questions/28899675/include-js-file-in-go-template/28899786#28899786). – icza Jan 19 '17 at 08:19

1 Answers1

0

You can use the http.ServeFile() function directly:

http.ServeFile(w, r, "/usr/local/myproject/style.css")
dom1
  • 425
  • 1
  • 2
  • 19