From b2056fbf48849e321389977e3fdbb0f5841822c4 Mon Sep 17 00:00:00 2001 From: Mitch Riedstra Date: Sat, 14 Dec 2019 09:51:15 -0500 Subject: Tidy up the server command, pull the handlers into the page library. Convert to go modules. Add a build script and version to the command --- page/http.go | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 page/http.go (limited to 'page/http.go') diff --git a/page/http.go b/page/http.go new file mode 100644 index 0000000..acf9147 --- /dev/null +++ b/page/http.go @@ -0,0 +1,34 @@ +package page + +import ( + "log" + "net/http" + "path/filepath" +) + +func SetupHandlers() { + http.Handle("/static/", StaticHandler()) + http.HandleFunc("/", PageHandler) +} + +func PageHandler(w http.ResponseWriter, r *http.Request) { + u := r.URL.Path + if u == "/" { + u = "/index" + } + u = filepath.Join(".", u) + log.Println(u) + + p := &Page{Name: u} + err := p.Render(w) + if err != nil { + log.Println(err) + http.Error(w, "Internal server error", 500) + return + } + +} + +func StaticHandler() (h http.Handler) { + return http.StripPrefix("/static/", http.FileServer(http.Dir("static"))) +} -- cgit v1.2.3