Created
June 1, 2018 00:32
-
-
Save nicolasparada/879ed2c6c45760c144667c3780f28b4b to your computer and use it in GitHub Desktop.
To serve single page applications
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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