Reputation: 2173
I'm trying to write an app in Go with Angular. I'm not sure if I got the concept right, but basically I should serve a simple html that loads angular and the app (js) itself and then the rest is handled by ajax requests. What I don't know is how to serve the html file on every non-ajax request on every path? I would like to use Gorilla mux but I couldn't find out how to do that.
Is this even the right direction?
Upvotes: 2
Views: 159
Reputation: 246
//serve static files from a folder 'public' which should be in the same dir as the executable.
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Cache-Control", "no-cache")
http.ServeFile(w, r, "public"+r.URL.Path)
})
This will try to serve every non-matching URL from the public directory. Hope this helps.
Upvotes: 0
Reputation: 3586
On every request that is not any known url You should send index.html - or whatever is Your base angular app file.
Gorilla/mux has a NotFoundHandler, which is handler for everyting that is not matched by any other routes. You can assignt Your own handler for it like that:
solution with gorilla/mux is:
func main() {
r := mux.NewRouter()
r.HandleFunc("/foo", fooHandler)
r.NotFoundHandler = http.HandlerFunc(notFound)
http.Handle("/", r)
}
while notFound is:
func notFound(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, "static/index.html")
}
assuming the Your base file is in static/index.html :).
Now all Your requests that are not any other requests (so, in that setup - not an ajax call defined in routes) will serve index file with url that can be handled by ngRoute or ui-router.
Upvotes: 1