This commit is contained in:
amutake 2014-12-30 05:30:15 +09:00
commit ecd9d69996
1 changed files with 39 additions and 0 deletions

39
main.go Normal file
View File

@ -0,0 +1,39 @@
package main
import (
"fmt"
"log"
"net/http"
"os"
)
func handler(w http.ResponseWriter, r *http.Request) {
path := "." + r.URL.Path
fmt.Println(path)
_, err := os.Stat(path)
if err == nil {
// file exists
http.ServeFile(w, r, path)
} else {
// file does not exists
index := "index.html"
_, err := os.Stat(index)
if err == nil {
// index.html exists
http.ServeFile(w, r, index)
} else {
// index.html does not exists
http.NotFound(w, r)
}
}
}
func main() {
port := ":5050"
http.HandleFunc("/", handler)
fmt.Println("spa-server starting on localhost" + port)
err := http.ListenAndServe(port, nil)
if err != nil {
log.Fatal("spa-server: ", err)
}
}