How to properly serve a single static file with Gorilla mux?
Asked Answered
R

1

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?

Randeerandel answered 19/1, 2017 at 7:37 Comment(3)
What response you get when you access something like <url>/style.css/abc ?Jesuitism
I guess rather using PathPrefix you should use Path routerJesuitism
To serve a single file, a lighter / faster alternative is http.ServeFile(). See this answer for details: Include js file in Go template.Nealneala
C
1

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

http.ServeFile(w, r, "/usr/local/myproject/style.css")
Coulson answered 1/6, 2023 at 10:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.