spa-server/main.go

47 lines
772 B
Go
Raw Normal View History

2014-12-29 20:30:15 +00:00
package main
import (
2014-12-29 20:41:51 +00:00
"flag"
2014-12-29 20:30:15 +00:00
"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() {
2014-12-29 20:41:51 +00:00
2014-12-29 20:30:15 +00:00
port := ":5050"
2014-12-29 20:41:51 +00:00
flag.Parse()
if flag.NArg() != 0 {
port = ":" + flag.Arg(0)
}
2014-12-29 20:48:44 +00:00
fmt.Println("spa-server starting on localhost" + port)
2014-12-29 20:41:51 +00:00
2014-12-29 20:30:15 +00:00
http.HandleFunc("/", handler)
err := http.ListenAndServe(port, nil)
if err != nil {
log.Fatal("spa-server: ", err)
}
}