mixing static and dynamic handler for http

2,029 views
Skip to first unread message

Σπύρος

unread,
Aug 4, 2011, 6:17:09 PM8/4/11
to golang-nuts
Hi

I try to create an application that serves both static and dynamic
content. The idea is that when the user points to a url a template is
rendered that has relative links to images. In code it resembles sth
like that:

func main() {
http.HandleFunc("/fen", FenHandler)
http.Handle("/pieces", http.FileServer(http.Dir("/home/spyros/src/
sandbox/board/pieces")))
http.ListenAndServe(":8080", nil)
}

This way it seems to ignore /pieces. If i point with a browser to
http://localhost:8080/pieces/board.jpg i get a 404 and the text
response "404 page not found". Using /pieces i get the same response.
If i hit http://localhost:8080/ i get the directory listing but the
links still return 404. The first url /fen works correctly but it does
not display the images but broken links. The urls in the template are
relative like "../pieces/wbb.jpg"

So to understand the api, it is permissible to mix static and dynamic
content or i should revert to url parsing and http.ServeFile for each
image?

I tried to get a hint by looking at the app engine demos but there the
static content directory is declared in app.yaml so i guess it is
handled by GAE and the url does not go to a http.Handler

Thanks
Spyros

Brad Fitzpatrick

unread,
Aug 4, 2011, 7:04:00 PM8/4/11
to Σπύρος, golang-nuts
See http://golang.org/pkg/http/#ServeMux

Note the comment about the trailing slash.


2011/8/4 Σπύρος <winw...@gmail.com>

Σπύρος

unread,
Aug 5, 2011, 4:36:10 AM8/5/11
to golang-nuts
Still not working. I added the trailing slash

http.Handle("/pieces/", http.FileServer(http.Dir("/home/spyros/src/
sandbox/board/pieces")))

and the difference is that if i point to http://localhost/pieces i get
a 301 and a redirection to /pieces/

http://localhost:8080/pieces/ still returns 404 and not the directory
listing
http://localhost:8080/pieces/wbb.jpg still returns 404

The version i use is
8g version weekly.2011-07-29 9342

As a workaround i use a second go server in a different port

http.ListenAndServe(":9090", http.FileServer(http.Dir("/home/spyros/
src/sandbox/board/pieces")))

which works as expected


On Aug 5, 2:04 am, Brad Fitzpatrick <bradf...@golang.org> wrote:
> Seehttp://golang.org/pkg/http/#ServeMux
>
> Note the comment about the trailing slash.
>
> 2011/8/4 Σπύρος <winwas...@gmail.com>
>
>
>
>
>
>
>
> > Hi
>
> > I try to create an application that serves both static and dynamic
> > content. The idea is that when the user points to a url a template is
> > rendered that has relative links to images. In code it resembles sth
> > like that:
>
> > func main() {
> >        http.HandleFunc("/fen", FenHandler)
> >        http.Handle("/pieces", http.FileServer(http.Dir("/home/spyros/src/
> > sandbox/board/pieces")))
> >        http.ListenAndServe(":8080", nil)
> > }
>
> > This way it seems to ignore /pieces. If i point with a browser to
> >http://localhost:8080/pieces/board.jpgi get a 404 and the text
> > response "404 page not found". Using /pieces i get the same response.
> > If i hithttp://localhost:8080/i get the directory listing but the

Martin Capitanio

unread,
Aug 5, 2011, 6:01:00 AM8/5/11
to golan...@googlegroups.com
On Friday, August 5, 2011 12:17:09 AM UTC+2, Σπύρος wrote:
Hi

I try to create an application that serves both static and dynamic
content. The idea is that when the user points to a url a template is
rendered that has relative links to images. In code it resembles sth
like that:

func main() {
http.HandleFunc("/fen", FenHandler)
http.Handle("/pieces", http.FileServer(http.Dir("/home/spyros/src/
sandbox/board/pieces")))

somewhat mind blowing, but this should work:
pfx := "/whatever/"
h := http.StripPrefix(pfx, http.FileServer(http.Dir("/home/spyros/src/ 
sandbox/board/pieces")))
http.Handle(pfx, h)

Σπύρος

unread,
Aug 5, 2011, 7:01:54 AM8/5/11
to golang-nuts
Yes this works. Looks like http.FileServer currently works only with
"/" as prefix

Thanks

On Aug 5, 1:01 pm, Martin Capitanio <m...@capitanio.org> wrote:
> On Friday, August 5, 2011 12:17:09 AM UTC+2, Σπύρος wrote:
>
> > Hi
>
> > I try to create an application that serves both static and dynamic
> > content. The idea is that when the user points to a url a template is
> > rendered that has relative links to images. In code it resembles sth
> > like that:
>
> > func main() {
> > http.HandleFunc("/fen", FenHandler)
> > http.Handle("/pieces", http.FileServer(http.Dir("/home/spyros/src/
> > sandbox/board/pieces")))
>
> somewhat mind blowing, but this should work:
> pfx := "/whatever/"
> h := http.StripPrefix(pfx, http.FileServer(http.Dir("/home/spyros/src/
> sandbox/board/pieces")))
> http.Handle(pfx, h)
>
> http.ListenAndServe(":8080", nil)
>
>
>
>
>
>
>
> > }
>
> > This way it seems to ignore /pieces. If i point with a browser to
> >http://localhost:8080/pieces/board.jpgi get a 404 and the text
> > response "404 page not found". Using /pieces i get the same response.
> > If i hithttp://localhost:8080/i get the directory listing but the
Reply all
Reply to author
Forward
0 new messages