1
0
mirror of https://github.com/danog/gojekyll.git synced 2025-01-23 03:31:22 +01:00
gojekyll/server.go
2017-06-11 20:51:01 -04:00

36 lines
709 B
Go
Executable File

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)
return http.ListenAndServe(address, nil)
}
func handler(w http.ResponseWriter, r *http.Request) {
path := r.URL.Path
// w.Header().Set("Content-Type", "text/plain; charset=utf-8")
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
}
err := p.Render(w)
if err != nil {
fmt.Printf("Error rendering %s: %s", path, err)
}
}