aboutsummaryrefslogtreecommitdiff
path: root/http
diff options
context:
space:
mode:
authorMitch Riedstra <mitch@riedstra.us>2020-11-24 23:20:54 -0500
committerMitch Riedstra <mitch@riedstra.us>2020-11-24 23:20:54 -0500
commitd83f4bca3f7026696a41225caac11807ed06fc2f (patch)
tree634552e0253407785f81cb28cf136fadec4d65e4 /http
parent2203a437cc7ba0ca087a47d6e99476ba5e09ae71 (diff)
downloadgo-website-d83f4bca3f7026696a41225caac11807ed06fc2f.tar.gz
go-website-d83f4bca3f7026696a41225caac11807ed06fc2f.tar.xz
Add more comments. Expand the interface. Allow templates to more easily be rendered with external variables.v0.0.11
Diffstat (limited to 'http')
-rw-r--r--http/main.go27
-rw-r--r--http/render.go62
2 files changed, 64 insertions, 25 deletions
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) {
diff --git a/http/render.go b/http/render.go
new file mode 100644
index 0000000..b73ae76
--- /dev/null
+++ b/http/render.go
@@ -0,0 +1,62 @@
+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)
+}