diff options
| author | Mitchell Riedstra <mitch@riedstra.dev> | 2023-01-07 13:31:23 -0500 |
|---|---|---|
| committer | Mitchell Riedstra <mitch@riedstra.dev> | 2023-01-07 13:31:23 -0500 |
| commit | ca33a035c779ae14fb6330c8801c75f49dd1bb79 (patch) | |
| tree | deaabaf15d6d91079a68f247e46070399e4343ee /page/render.go | |
| parent | 97dd660925434be537cd9a49a1d0c893b223e357 (diff) | |
| download | go-website-ca33a035c779ae14fb6330c8801c75f49dd1bb79.tar.gz go-website-ca33a035c779ae14fb6330c8801c75f49dd1bb79.tar.xz | |
Add an internal caching option. It performs quite well.v0.0.22
Also refactor and clean up most linter warnings.
Diffstat (limited to 'page/render.go')
| -rw-r--r-- | page/render.go | 61 |
1 files changed, 7 insertions, 54 deletions
diff --git a/page/render.go b/page/render.go index 691a258..0ae5334 100644 --- a/page/render.go +++ b/page/render.go @@ -5,43 +5,21 @@ import ( "errors" "io/fs" "net/http" - "path/filepath" ) -func getURLPath(r *http.Request) string { - u := r.URL.Path - if u == "/" { - u = "/index" - } - - return u -} - // Render5xx is automatically called if any render fails, // additionally you can call it for your own uses. It will // try to use the 5xx template but will fall back to a plain // page if that fails. func Render5xx(w http.ResponseWriter, r *http.Request, vars map[string]interface{}, statusCode int) { - u := getURLPath(r) - - Logger.Printf("%s %s %d %s", - r.RemoteAddr, - r.Method, - statusCode, - u) - p := NewPage(TemplateDirectory + "/5xx") buf := &bytes.Buffer{} err := p.Render(buf) if err != nil { - Logger.Printf("%s %s path: %s while trying 5xx: %s", - r.RemoteAddr, - r.Method, - u, - err) + logErr(r, "while trying 5xx", err) http.Error(w, "Internal server error", statusCode) return @@ -51,32 +29,20 @@ func Render5xx(w http.ResponseWriter, r *http.Request, _, _ = w.Write(buf.Bytes()) - Logger.Printf("%s %s %d %s", r.RemoteAddr, r.Method, statusCode, u) + logReq(r, statusCode) } // Render4xx is mostly used to render a 404 page, pulls from 404 template. // automatically called by Render if a page is missing. func Render4xx(w http.ResponseWriter, r *http.Request, vars map[string]interface{}, statusCode int) { - u := getURLPath(r) - - Logger.Printf("%s %s %d %s", - r.RemoteAddr, - r.Method, - statusCode, - u) - p := NewPage(TemplateDirectory + "/4xx") buf := &bytes.Buffer{} err := p.Render(buf) if err != nil { - Logger.Printf("%s %s path: %s while trying 404: %s", - r.RemoteAddr, - r.Method, - u, - err) + logErr(r, "while trying 404", err) Render5xx(w, r, vars, http.StatusInternalServerError) return @@ -91,17 +57,13 @@ func Render4xx(w http.ResponseWriter, r *http.Request, return } - Logger.Printf("%s %s %d %s", r.RemoteAddr, r.Method, statusCode, u) + logReq(r, statusCode) } // Render is a lower level option, allowing you to specify local // variables and the status code in which to return. func Render(w http.ResponseWriter, r *http.Request, path string, vars map[string]interface{}, statusCode int) { - u := getURLPath(r) - - u = filepath.Join(".", u) - // Sepcifically use the specified path for the page p := NewPage(path) @@ -119,11 +81,7 @@ func Render(w http.ResponseWriter, r *http.Request, return } - Logger.Printf("%s %s path: %s rendering encountered: %s", - r.RemoteAddr, - r.Method, - u, - err) + logErr(r, "rendering encountered", err) Render5xx(w, r, vars, http.StatusInternalServerError) return @@ -133,18 +91,13 @@ func Render(w http.ResponseWriter, r *http.Request, _, err = w.Write(buf.Bytes()) if err != nil { - Logger.Printf("%s %s %d %s: while writing buf: %s", - r.RemoteAddr, - r.Method, - statusCode, - u, - err) + logErr(r, "while writing to buf", err) Render5xx(w, r, nil, http.StatusInternalServerError) return } - Logger.Printf("%s %s %d %s", r.RemoteAddr, r.Method, statusCode, u) + logReq(r, statusCode) } // RenderWithVars allows you to specify a specific page and whether or not |
