From ecd9d6999609fa5bec1a0d959436579833ecfb28 Mon Sep 17 00:00:00 2001 From: amutake Date: Tue, 30 Dec 2014 05:30:15 +0900 Subject: [PATCH] Initial --- main.go | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 main.go diff --git a/main.go b/main.go new file mode 100644 index 0000000..c7d8619 --- /dev/null +++ b/main.go @@ -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) + } +}