aboutsummaryrefslogtreecommitdiff
path: root/cmd/server/handlers.go
blob: d4b714014f217d0174b34f4891e08b0af5195ad1 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
package main

import (
	"fmt"
	"net/http"
	"path/filepath"
	"strings"

	"github.com/gorilla/mux"
	"riedstra.dev/mitch/go-website/page"
	"riedstra.dev/mitch/go-website/rediscache"
)

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.HandleFunc("/login", a.LoginHandler)
	rtr.Handle("/logout", a.RequiresLogin(http.HandlerFunc(a.LogoutHandler)))
	rtr.PathPrefix("/edit/").Handler(
		a.RequiresLogin(http.StripPrefix("/edit/", http.HandlerFunc(a.EditPage)))).Methods("GET")
	rtr.PathPrefix("/edit/").Handler(
		a.RequiresLogin(http.StripPrefix("/edit/", http.HandlerFunc(a.SaveEditPage)))).Methods("POST")

	if a.redisPool != nil && !a.IsLoggedIn(r) {
		rtr.PathPrefix(fmt.Sprintf("/%s/{tag}", a.FeedPrefix)).Handler(
			rediscache.HandleWithParams(a.redisPool, a.RedisKey,
				http.HandlerFunc(a.FeedHandler)))

		rtr.PathPrefix("/_json/").Handler(
			rediscache.HandleWithParams(a.redisPool, a.RedisKey,
				http.StripPrefix("/_json", http.HandlerFunc(a.PageJsonHandler))))

		rtr.PathPrefix("/_md/").Handler(
			rediscache.HandleWithParams(a.redisPool, a.RedisKey,
				http.StripPrefix("/_md", http.HandlerFunc(a.PageMarkdownHandler))))

		rtr.PathPrefix("/").Handler(rediscache.Handle(
			a.redisPool, a.RedisKey, http.HandlerFunc(a.PageHandler)))
	} else {
		rtr.PathPrefix("/_md/").Handler(http.StripPrefix("/_md",
			http.HandlerFunc(a.PageMarkdownHandler)))

		rtr.PathPrefix("/_json/").Handler(http.StripPrefix("/_json",
			http.HandlerFunc(a.PageJsonHandler)))

		rtr.PathPrefix(fmt.Sprintf("/%s/{tag}", a.FeedPrefix)).HandlerFunc(
			a.FeedHandler)

		rtr.PathPrefix("/").Handler(http.HandlerFunc(a.PageHandler))
	}

	rtr.ServeHTTP(w, r)
}

func (a *App) PageHandler(w http.ResponseWriter, r *http.Request) {
	u := r.URL.Path
	if u == "/" {
		u = "/index"
	}

	loggedIn := a.IsLoggedIn(r)

	if u == "/dashboard" && loggedIn {
		u = page.TemplateDirectory + "/dashboard"
	}

	// Render nothing inside of the template directory if we're not logged in
	if strings.HasPrefix(u[1:], filepath.Clean(page.TemplateDirectory)) &&
		!loggedIn {

		page.Render4xx(w, r, map[string]interface{}{
			"LoggedIn": loggedIn,
		}, 404)
		return
	}

	u = filepath.Join(".", u)

	page.RenderWithVars(w, r, u, map[string]interface{}{
		"LoggedIn": loggedIn,
	})
}
func (a *App) PageJsonHandler(w http.ResponseWriter, r *http.Request) {
	u := r.URL.Path
	if u == "/" {
		u = "/index"
	}

	// Skip template directory
	if strings.HasPrefix(u[1:], filepath.Clean(page.TemplateDirectory)) {
		page.Render4xx(w, r, map[string]interface{}{}, 404)
		return
	}

	u = filepath.Join(".", u)

	page.RenderJson(w, r, u, map[string]interface{}{}, 200)
}

func (a *App) PageMarkdownHandler(w http.ResponseWriter, r *http.Request) {
	u := r.URL.Path
	if u == "/" {
		u = "/index"
	}

	// Skip template directory
	if strings.HasPrefix(u[1:], filepath.Clean(page.TemplateDirectory)) {
		page.Render4xx(w, r, map[string]interface{}{}, 404)
		return
	}

	u = filepath.Join(".", u)

	page.RenderMarkdown(w, r, u, map[string]interface{}{}, 200)
}

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)))
}