diff options
| author | Mitchell Riedstra <mitch@riedstra.dev> | 2021-02-15 15:31:37 -0500 |
|---|---|---|
| committer | Mitchell Riedstra <mitch@riedstra.dev> | 2021-02-15 15:32:05 -0500 |
| commit | fe9ec7a0b45c9fd23a615a8b95ade3e9c1ea2d12 (patch) | |
| tree | 80844a62d5d18b30862cfdc610aae88713fe97d9 /http/render.go | |
| parent | d83f4bca3f7026696a41225caac11807ed06fc2f (diff) | |
| download | go-website-fe9ec7a0b45c9fd23a615a8b95ade3e9c1ea2d12.tar.gz go-website-fe9ec7a0b45c9fd23a615a8b95ade3e9c1ea2d12.tar.xz | |
Another re-structure. Deleting code is wonderful.v0.0.12
Diffstat (limited to 'http/render.go')
| -rw-r--r-- | http/render.go | 62 |
1 files changed, 0 insertions, 62 deletions
diff --git a/http/render.go b/http/render.go deleted file mode 100644 index b73ae76..0000000 --- a/http/render.go +++ /dev/null @@ -1,62 +0,0 @@ -package http - -import ( - "net/http" - "path/filepath" - "strings" -) - -func Render(w http.ResponseWriter, r *http.Request, - path string, vars map[string]interface{}, statusCode int) { - - u := r.URL.Path - if u == "/" { - u = "/index" - } - u = filepath.Join(".", u) - - // Sepcifically use the specified path for the page - p := NewPage(path) - - if vars != nil { - p.SetVars(vars) - } - - 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, statusCode, u) - -} - -// RenderWithVars allows you to specify a specific page and whether or not -// you wish to override vars. If left nil they will not be overridden. -// Also see RenderForPath if you don't need to override them -func RenderWithVars(w http.ResponseWriter, r *http.Request, - path string, vars map[string]interface{}) { - - Render(w, r, path, vars, 200) -} - -// RenderForPath takes the path to a page and finish up the rendering -// Allowing you to place logic on what page is rendered by your handlers -func RenderForPath(w http.ResponseWriter, r *http.Request, path string) { - RenderWithVars(w, r, path, nil) -} |
