2017-06-10 15:38:09 -04:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
)
|
|
|
|
|
|
|
|
func server() error {
|
|
|
|
address := "localhost:4000"
|
|
|
|
printSetting("Server address:", "http://"+address+"/")
|
|
|
|
printSetting("Server running...", "press ctrl-c to stop.")
|
|
|
|
http.HandleFunc("/", handler)
|
2017-06-11 20:05:17 -04:00
|
|
|
return http.ListenAndServe(address, nil)
|
2017-06-10 15:38:09 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func handler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
path := r.URL.Path
|
|
|
|
|
2017-06-11 11:51:25 -04:00
|
|
|
// w.Header().Set("Content-Type", "text/plain; charset=utf-8")
|
2017-06-10 15:38:09 -04:00
|
|
|
|
2017-06-13 11:00:24 -04:00
|
|
|
p, found := site.Paths[path]
|
2017-06-10 15:38:09 -04:00
|
|
|
if !found {
|
|
|
|
w.WriteHeader(http.StatusNotFound)
|
2017-06-13 11:00:24 -04:00
|
|
|
p, found = site.Paths["404.html"]
|
2017-06-10 15:38:09 -04:00
|
|
|
}
|
|
|
|
if !found {
|
|
|
|
fmt.Fprintf(w, "404 page not found: %s", path)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-06-14 20:44:22 -04:00
|
|
|
err := p.Write(w)
|
2017-06-10 15:38:09 -04:00
|
|
|
if err != nil {
|
2017-06-11 20:05:17 -04:00
|
|
|
fmt.Printf("Error rendering %s: %s", path, err)
|
|
|
|
}
|
2017-06-10 15:38:09 -04:00
|
|
|
}
|