aboutsummaryrefslogtreecommitdiff
path: root/cmd
diff options
context:
space:
mode:
authorMitchell Riedstra <mitch@riedstra.dev>2021-07-07 15:39:51 -0400
committerMitchell Riedstra <mitch@riedstra.dev>2021-07-07 15:39:51 -0400
commite6d53f71c9718ecdb9fde16a924d75a71aadd2d2 (patch)
treeb71c2789f0e95c6ee5d522d1d5b36dc38de8daad /cmd
parentfe9ec7a0b45c9fd23a615a8b95ade3e9c1ea2d12 (diff)
downloadgo-website-e6d53f71c9718ecdb9fde16a924d75a71aadd2d2.tar.gz
go-website-e6d53f71c9718ecdb9fde16a924d75a71aadd2d2.tar.xz
Add the handler file that was accidently ignored
Diffstat (limited to 'cmd')
-rw-r--r--cmd/server/handlers.go53
1 files changed, 53 insertions, 0 deletions
diff --git a/cmd/server/handlers.go b/cmd/server/handlers.go
new file mode 100644
index 0000000..60e0a35
--- /dev/null
+++ b/cmd/server/handlers.go
@@ -0,0 +1,53 @@
+package main
+
+import (
+ "net/http"
+ "path/filepath"
+
+ "github.com/gorilla/mux"
+ "riedstra.dev/mitch/go-website/page"
+)
+
+type App struct {
+ ReIndexPath string
+ StaticDirectory string
+}
+
+func (a *App) PageHandler(w http.ResponseWriter, r *http.Request) {
+ u := r.URL.Path
+ if u == "/" {
+ u = "/index"
+ }
+ u = filepath.Join(".", u)
+
+ page.RenderForPath(w, r, u)
+}
+
+func (a *App) RebuildIndexHandler(w http.ResponseWriter, r *http.Request) {
+ u := r.URL.Path
+ if u == "/" {
+ u = "/index"
+ }
+ u = filepath.Join(".", u)
+
+ p := page.NewPage("index")
+ err := p.RebuildIndex()
+
+ page.RenderWithVars(w, r, u, map[string]interface{}{
+ "IndexError": err,
+ })
+}
+
+// StaticHandler simply returns a HTTP handler that looks at the current
+// directory and exposes `static` via HTTP `/static`
+func (a *App) StaticHandler() http.Handler {
+ return http.StripPrefix("/static/", http.FileServer(http.Dir(a.StaticDirectory)))
+}
+
+func (a *App) ServeHTTP(w http.ResponseWriter, r *http.Request) {
+ rtr := mux.NewRouter()
+ rtr.HandleFunc(a.ReIndexPath, a.RebuildIndexHandler)
+ rtr.PathPrefix("/static/").Handler(a.StaticHandler())
+ rtr.PathPrefix("/").HandlerFunc(a.PageHandler)
+ rtr.ServeHTTP(w, r)
+}