husobee/vestigo

Cannot Serve Static Files

KamiQuasi opened this issue · 3 comments

Hoping I'm just doing something wrong. I wasn't sure where else to get help, but I've tried all sorts of variants of the following to serve static files under a path in the same way I would with http.Handle but can't seem to get it to work:

router := vestigo.NewRouter()
bc := http.FileServer(http.Dir("bower_components"))
router.Handle("/bower_components/", http.StripPrefix("/bower_components/", bc))
err = http.ListenAndServe(bind, router)
if err != nil {
	log.Fatal(fmt.Errorf("Listener Error: %s", err))
}

#48 might be helpful. Let me know if that doesn't work for you.

Sometime I wonder if I'm making things too simple or others a bit too complicated. Anyways, here's how I handle static files. (Partly extracted from my project, this version is not tested!)

// Create a new Router
router := vestigo.NewRouter()

// Handle all endpoints ...
router.Get("/", HandleIndexPage) // Just an example

// Handle static files
router.Get("/*", func(w http.ResponseWriter, r *http.Request) {
    http.ServeFile(w, r, "./static/" + vestigo.Param(r, "_name"))
})

http.ListenAndServe("0.0.0.0:8080", router)

This way you can simply route everything like you would with the standard HTTP Muxer. This way there is no need for a special route for static files. Hope this is actually helping a bit. ;-)

@husobee that was just what I needed, thanks!