package main import ( "net/http" "os" "path/filepath" ) // https://github.com/gorilla/mux#serving-single-page-applications type spaHandler struct { staticPath string } func (h spaHandler) ServeHTTP(writer http.ResponseWriter, request *http.Request) { path, err := filepath.Abs(request.URL.Path) if err != nil { http.Error(writer, err.Error(), http.StatusBadRequest) } path = filepath.Join(h.staticPath, path) if _, err = os.Stat(path); os.IsNotExist(err) { serve_path := filepath.Base(path) if len(filepath.Ext(path)) == 0 { serve_path = "index.html" } serve_path = filepath.Join(h.staticPath, serve_path) http.ServeFile(writer, request, serve_path) return } else if err != nil { http.Error(writer, err.Error(), http.StatusInternalServerError) return } http.FileServer(http.Dir(h.staticPath)).ServeHTTP(writer, request) }