Skip to content

Instantly share code, notes, and snippets.

@nicolasparada
Created June 1, 2018 00:32
Show Gist options
  • Save nicolasparada/879ed2c6c45760c144667c3780f28b4b to your computer and use it in GitHub Desktop.
Save nicolasparada/879ed2c6c45760c144667c3780f28b4b to your computer and use it in GitHub Desktop.
To serve single page applications
package main
import (
"log"
"net/http"
)
func main() {
h := http.FileServer(spaFileSystem{http.Dir("static")})
log.Fatalln(http.ListenAndServe(":3000", h))
}
type spaFileSystem struct {
fs http.FileSystem
}
// Open a file in the dir, falling back to index.html.
func (fs spaFileSystem) Open(name string) (http.File, error) {
f, err := fs.fs.Open(name)
if err != nil {
return fs.fs.Open("index.html")
}
return f, nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment