Skip to content

Latest commit

 

History

History
23 lines (17 loc) · 604 Bytes

README.md

File metadata and controls

23 lines (17 loc) · 604 Bytes

Static File Server

By using this package you can serve static files with 404 Error fallback to custom http.handler.

For example if you want to serve static contents of a Single Page Application, you can always serve index.html when 404 error occurs.

Example:

func main() {
	port := os.Getenv("PORT")
	if port == "" {
		log.Panic("$PORT should not be empty")
	}

	h := sfs.New(http.Dir("client/build"), IndexHandler)

	log.Fatal(http.ListenAndServe(":"+port, h))
}

func IndexHandler(w http.ResponseWriter, r *http.Request) {
	http.ServeFile(w, r, "client/build/index.html")
}