2017-06-10 21:38:09 +02: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)
|
|
|
|
err := http.ListenAndServe(address, nil)
|
|
|
|
if err != nil {
|
|
|
|
// TODO pick another port
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func handler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
path := r.URL.Path
|
|
|
|
|
2017-06-11 17:51:25 +02:00
|
|
|
// w.Header().Set("Content-Type", "text/plain; charset=utf-8")
|
2017-06-10 21:38:09 +02:00
|
|
|
|
|
|
|
p, found := siteMap[path]
|
|
|
|
if !found {
|
|
|
|
w.WriteHeader(http.StatusNotFound)
|
|
|
|
p, found = siteMap["404.html"]
|
|
|
|
}
|
|
|
|
if !found {
|
|
|
|
fmt.Fprintf(w, "404 page not found: %s", path)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-06-10 23:51:46 +02:00
|
|
|
p, err := readFile(p.Path, siteData, true)
|
2017-06-10 21:38:09 +02:00
|
|
|
if err != nil {
|
2017-06-11 17:51:25 +02:00
|
|
|
fmt.Printf("Error rendering %s: %s", p.Path, err)
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
2017-06-10 21:38:09 +02:00
|
|
|
}
|
|
|
|
w.Write(p.Body)
|
|
|
|
}
|