unable to handle static files with gorilla mux

3,055 views
Skip to first unread message

Vasiliy Tolstov

unread,
Apr 10, 2012, 6:39:04 AM4/10/12
to goril...@googlegroups.com
Hello. I'm try to use gorilla in my project and have one issue:

Dinamic path works fine, but if i try to serve static file with
FileHandler func, i'm always get 404 error.

func main() {
r := mux.NewRouter()
r.HandleFunc("/", HomeHandler)
r.HandleFunc("/favicon.ico", FileHandler)
r.HandleFunc("/sitemap.txt", FileHandler)
r.HandleFunc("/robots.txt", FileHandler)
r.HandleFunc("/images", FileHandler)
r.HandleFunc("/css", FileHandler)
r.HandleFunc("/projects/{category}", ProjectsHandler)
r.HandleFunc("/projects/{category}/{project}", ProjectHandler)
e := http.ListenAndServe(":8080", r)
if e != nil {
println(e.Error())
}
}

When i get /images i get text/html, then i get /images/logo.png i get 404 error.
What i'm missing?

--
Vasiliy Tolstov,
Clodo.ru
e-mail: v.to...@selfip.ru
jabber: va...@selfip.ru

flackez

unread,
Apr 10, 2012, 8:15:53 AM4/10/12
to goril...@googlegroups.com
On Tuesday, April 10, 2012 5:39:04 PM UTC+7, Vasiliy Tolstov wrote:

When i get /images i get text/html, then i get /images/logo.png i get 404 error.
What i'm missing?

I'm not sure, but you might be use "/images/" instead of "/images".

Vasiliy Tolstov

unread,
Apr 10, 2012, 8:23:47 AM4/10/12
to goril...@googlegroups.com
2012/4/10 flackez <fla...@gmail.com>:

No, in this case when get /images/ all works, but if /images/logo.png not.

Marcel Hauf

unread,
Apr 10, 2012, 9:20:13 AM4/10/12
to goril...@googlegroups.com
I handle static files like this:
http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("static/"))))
which serves every file inside "workdir/static/filename" as "localhost:8080/static/filename".
Works with folders, too. workdir/static/js/jquery.js would be served as "localhost:8080/static/js/jquery.js".

2012/4/10 Vasiliy Tolstov <v.to...@selfip.ru>



--
~ Marcel Hauf

Rodrigo Moraes

unread,
Apr 10, 2012, 11:51:31 AM4/10/12
to Gorilla web toolkit
On Apr 10, 10:20 am, Marcel Hauf wrote:
> I handle static files like this:
> http.Handle("/static/", http.StripPrefix("/static/",
> http.FileServer(http.Dir("static/"))))

If is fine to register paths in the default mux, like above. You can
mix those with gorilla routes etc. A pure gorilla approach would
involve path prefix, something like:

r := new(mux.Router)
r.PathPrefix("/static/").Handler(http.StripPrefix("/static/",
http.FileServer(http.Dir("static/"))))

I'm not exactly sure what you "FileHandler" does, but I suspect that
for the "/images" and "/css" paths you want to use prefix matching and
not exact matching:

r.PathPrefix("/images/").Handler(...)

-- rodrigo

Vasiliy Tolstov

unread,
Apr 10, 2012, 2:26:38 PM4/10/12
to goril...@googlegroups.com
2012/4/10 Rodrigo Moraes <rodrigo...@gmail.com>:

Thanks!!!

Vasiliy Tolstov

unread,
Apr 10, 2012, 3:32:40 PM4/10/12
to goril...@googlegroups.com
2012/4/10 Rodrigo Moraes <rodrigo...@gmail.com>:

Hmm. How can i deal with this and with r.StrictSlash(true) ?
Or i need new router?

--
Vasiliy Tolstov,
Clodo.ru
e-mail: v.to...@selfip.ru

jbv abber: va...@selfip.ru
v

Rodrigo Moraes

unread,
Apr 10, 2012, 4:11:38 PM4/10/12
to Gorilla web toolkit
On Apr 10, 4:32 pm, Vasiliy Tolstov wrote:
> Hmm. How can i deal with this and with r.StrictSlash(true) ?
> Or i need new router?

Good question. Did you have problems with it? The StrictSlash option
must be ignored for path prefixes. I thought it was, but it was not.
So I made this change:

http://code.google.com/p/gorilla/source/detail?r=7a5c1deed356c864722265a2cf625fd764327186

-- rodrigo



Vasiliy Tolstov

unread,
Apr 10, 2012, 4:33:43 PM4/10/12
to goril...@googlegroups.com
2012/4/11 Rodrigo Moraes <rodrigo...@gmail.com>:

Thanks for quick fix!
>

--
Vasiliy Tolstov,
Clodo.ru
e-mail: v.to...@selfip.ru

jabber: va...@selfip.ru

Vasiliy Tolstov

unread,
Apr 10, 2012, 4:43:43 PM4/10/12
to goril...@googlegroups.com
2012/4/11 Rodrigo Moraes <rodrigo...@gmail.com>:

Hmm , some error not solved.
When StrictSlah(true) i get net::ERR_TOO_MANY_REDIRECTS in chromium
and to path static/logo.png in first reload add slash
(/static/logo.png/) and after second refresh slash removed
(/static/logo.png) but error stil exists.

--
Vasiliy Tolstov,
Clodo.ru
e-mail: v.to...@selfip.ru

jabber: va...@selfip.ru

Rodrigo Moraes

unread,
Apr 10, 2012, 5:19:45 PM4/10/12
to Gorilla web toolkit
On Apr 10, 5:43 pm, Vasiliy Tolstov wrote:
> Hmm , some error not solved.
> When StrictSlah(true) i get net::ERR_TOO_MANY_REDIRECTS in chromium
> and to path static/logo.png in first reload add slash
> (/static/logo.png/) and after second refresh slash removed
> (/static/logo.png) but error stil exists.

Did you update gorilla using the go tool? The change made in the repo
wasn't tagged, so maybe you still have the old version?

Also, can you post your route definition? Are you using PathPrefix?
When path prefix is used, redirect is not set even with
StrictSlash(true), as this test tries to demonstrate:

http://code.google.com/p/gorilla/source/detail?r=c9756d5f20f4bb59b9cb226520a7f50dd8be7a8f

-- rodrigo

Vasiliy Tolstov

unread,
Apr 10, 2012, 5:32:32 PM4/10/12
to goril...@googlegroups.com
2012/4/11 Rodrigo Moraes <rodrigo...@gmail.com>:

func main() {
r := mux.NewRouter()
// r.StrictSlash(true)


r.HandleFunc("/", HomeHandler)
r.HandleFunc("/favicon.ico", FileHandler)
r.HandleFunc("/sitemap.txt", FileHandler)
r.HandleFunc("/robots.txt", FileHandler)

r.PathPrefix("/static/").Handler(http.StripPrefix("/static/",
http.FileServer(http.Dir("static/"))))

r.HandleFunc("/projects/{category}", ProjectsHandler)
r.HandleFunc("/projects/{category}/{project}", ProjectHandler)

r.HandleFunc("/login/{method}", LoginHandler)
http.Handle("/", r)


e := http.ListenAndServe(":8080", r)
if e != nil {
println(e.Error())
}
}

Vasiliy Tolstov

unread,
Apr 10, 2012, 5:32:59 PM4/10/12
to goril...@googlegroups.com
2012/4/11 Vasiliy Tolstov <v.to...@selfip.ru>:

> 2012/4/11 Rodrigo Moraes <rodrigo...@gmail.com>:
>> On Apr 10, 5:43 pm, Vasiliy Tolstov wrote:
>> Also, can you post your route definition? Are you using PathPrefix?
>> When path prefix is used, redirect is not set even with
>> StrictSlash(true), as this test tries to demonstrate:
>>


i'm update package via go get -u

Rodrigo Moraes

unread,
Apr 10, 2012, 5:42:44 PM4/10/12
to Gorilla web toolkit
On Apr 10, 6:32 pm, Vasiliy Tolstov wrote:
> i'm update package via go get -u

That won't work, sorry. It will only work when the tag go1 is set to
the repo, but I'll hold a bit before tagging it.

You need to update manually -- fetch the repo, add it to GOPATH/src
and then run go install.

-- rodrigo

Vasiliy Tolstov

unread,
Apr 10, 2012, 5:55:33 PM4/10/12
to goril...@googlegroups.com
2012/4/11 Rodrigo Moraes <rodrigo...@gmail.com>:

Nothing. I'm go to src/ hg pull && gh update
go install -v inside mux dir
after that i get mux.a pkg.
after that i go to my project and do
go clean
go build

but i still have error.. (hg log says that changes to mux apllied)

Rodrigo Moraes

unread,
Apr 10, 2012, 6:04:14 PM4/10/12
to Gorilla web toolkit
On Apr 10, 6:55 pm, Vasiliy Tolstov wrote:
> Nothing. I'm go to src/ hg pull && gh update
> go install -v inside mux dir
> after that i get mux.a pkg.
> after that i go to my project and do
> go clean
> go build

Hm, I'm not familiar with this. Is the updated code.google.com/p/
gorilla dir inside GOPATH/src?

-- rodrigo

Vasiliy Tolstov

unread,
Apr 10, 2012, 6:06:04 PM4/10/12
to goril...@googlegroups.com
2012/4/11 Rodrigo Moraes <rodrigo...@gmail.com>:

Yes.

Rodrigo Moraes

unread,
Apr 10, 2012, 6:24:56 PM4/10/12
to Gorilla web toolkit
On Apr 10, 7:06 pm, Vasiliy Tolstov wrote:
> Yes.

Ok. Can you send me a simple test case or example that reproduces the
problem? I'll take a better look.

-- rodrigo

Rodrigo Moraes

unread,
Apr 13, 2012, 10:25:27 AM4/13/12
to Gorilla web toolkit
After playing with the example Vasiliy sent me (thanks!), I found the
issue. There is a bug when PathPrefix is used -- a possible
redirection is still calculated when the route matches. I fixed it by
setting the strictSlash option to false when PathPrefix is used:

// PathPrefix adds a matcher for the URL path prefix.
func (r *Route) PathPrefix(tpl string) *Route {
r.strictSlash = false
r.err = r.addRegexpMatcher(tpl, false, true)
return r
}

This must affect particularly file serving, when you have the
strictSlash option turned on and have a PathPrefix ending in a slash
but the served file obviously should not end in a slash.

Sorry for this issue and thanks for bringing it up. I'll push this fix
and tag a new release later today (going to lunch now :).

-- rodrigo

Rodrigo Moraes

unread,
Apr 13, 2012, 1:44:27 PM4/13/12
to Gorilla web toolkit
On Apr 13, 11:25 am, Rodrigo Moraes wrote:
> I'll push this fix and tag a new release later today.

This is now fixed and a proper test is in place.

-- rodrigo
Reply all
Reply to author
Forward
0 new messages