From d83f4bca3f7026696a41225caac11807ed06fc2f Mon Sep 17 00:00:00 2001 From: Mitch Riedstra Date: Tue, 24 Nov 2020 23:20:54 -0500 Subject: Add more comments. Expand the interface. Allow templates to more easily be rendered with external variables. --- http/main.go | 27 ++------------------------- 1 file changed, 2 insertions(+), 25 deletions(-) (limited to 'http/main.go') diff --git a/http/main.go b/http/main.go index e09f610..c4726d8 100644 --- a/http/main.go +++ b/http/main.go @@ -9,7 +9,6 @@ import ( "net/http" "os" "path/filepath" - "strings" "riedstra.dev/mitch/go-website/local" "riedstra.dev/mitch/go-website/page" @@ -21,7 +20,7 @@ import ( // since an interface is used, you can swap this out with a different // library to load pages from different sources var NewPage func(string) page.Page = func(u string) page.Page { - return &local.Page{Path: u} + return local.NewPage(u) } // ReindexRedirectTo is the path that we'll redirect to when a call to rebuild @@ -54,29 +53,7 @@ func PageHandler(w http.ResponseWriter, r *http.Request) { } u = filepath.Join(".", u) - p := NewPage(u) - err := p.Render(w) - if err != nil { - if strings.HasSuffix(err.Error(), "no such file or directory") { - Logger.Printf("%s %s %d %s", r.RemoteAddr, r.Method, 404, u) - p = NewPage("404") - w.WriteHeader(404) - err := p.Render(w) - if err != nil { - Logger.Printf("%s %s path: %s while trying 404: %s", r.RemoteAddr, r.Method, u, err) - http.Error(w, "Internal server error", 500) - return - } - return - } else { - Logger.Printf("%s %s path: %s encountered: %s", r.RemoteAddr, r.Method, u, err) - http.Error(w, "Internal server error", 500) - return - } - } - - Logger.Printf("%s %s %d %s", r.RemoteAddr, r.Method, 200, u) - + RenderForPath(w, r, u) } func RebuildIndexHandler(w http.ResponseWriter, r *http.Request) { -- cgit v1.2.3