forked from kabukky/journey
-
Notifications
You must be signed in to change notification settings - Fork 0
/
blog.go
164 lines (156 loc) · 4.56 KB
/
blog.go
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package server
import (
"github.com/kabukky/journey/database"
"github.com/kabukky/journey/filenames"
"github.com/kabukky/journey/templates"
"net/http"
"path/filepath"
"regexp"
"strconv"
)
var validPostPath = regexp.MustCompile("^/([\\p{L}\\p{M}\\p{N}-]*)/?$")
var validIndexPath = regexp.MustCompile("^/(page/)?([\\p{L}\\p{M}\\p{N}-]*)/?$")
var validAuthorPath = regexp.MustCompile("^/author/([\\p{L}\\p{M}\\p{N}-]*)(/page|/rss)?/?(\\d+)?/?$")
var validTagPath = regexp.MustCompile("^/tag/([\\p{L}\\p{M}\\p{N}-]*)(/page|/rss)?/?(\\d+)?/?$")
func indexHandler(w http.ResponseWriter, r *http.Request) {
m := validIndexPath.FindStringSubmatch(r.URL.Path)
if m == nil {
http.Redirect(w, r, "/", http.StatusFound)
return
}
if m[2] == "" {
http.Redirect(w, r, "/", http.StatusFound)
return
}
page, err := strconv.Atoi(m[2])
if err != nil {
http.Redirect(w, r, "/", http.StatusFound)
return
}
// Render index template
err = templates.ShowIndexTemplate(w, page)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
return
}
func authorHandler(w http.ResponseWriter, r *http.Request) {
m := validAuthorPath.FindStringSubmatch(r.URL.Path)
if m == nil {
http.Redirect(w, r, "/", http.StatusFound)
return
}
if m[2] == "" {
// Render author template (first page)
err := templates.ShowAuthorTemplate(w, m[1], 1)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
return
} else if m[2] == "/rss" {
// Render author rss feed
err := templates.ShowAuthorRss(w, m[1])
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
return
}
page, err := strconv.Atoi(m[3])
if err != nil {
http.Redirect(w, r, "/", http.StatusFound)
return
}
// Render author template
err = templates.ShowAuthorTemplate(w, m[1], page)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
return
}
func tagHandler(w http.ResponseWriter, r *http.Request) {
m := validTagPath.FindStringSubmatch(r.URL.Path)
if m == nil {
http.Redirect(w, r, "/", http.StatusFound)
return
}
if m[2] == "" {
// Render tag template (first page)
err := templates.ShowTagTemplate(w, m[1], 1)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
return
} else if m[2] == "/rss" {
// Render tag rss feed
err := templates.ShowTagRss(w, m[1])
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
return
}
page, err := strconv.Atoi(m[3])
if err != nil {
http.Redirect(w, r, "/", http.StatusFound)
return
}
// Render tag template
err = templates.ShowTagTemplate(w, m[1], page)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
return
}
func postHandler(w http.ResponseWriter, r *http.Request) {
m := validPostPath.FindStringSubmatch(r.URL.Path)
if m == nil {
http.Redirect(w, r, "/", http.StatusFound)
return
}
if m[1] == "" {
// Render index template (first page)
err := templates.ShowIndexTemplate(w, 1)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
return
} else if m[1] == "rss" {
// Render index rss feed
err := templates.ShowIndexRss(w)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
return
}
// Render post template
err := templates.ShowPostTemplate(w, m[1])
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
return
}
func generateAssetHandler(mux *http.ServeMux) {
// Function to be able to change the asset path at runtime (e.g. when the theme changes)
mux.Handle("/assets/", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// TODO: It might be possible to do this more efficently. Getting the theme from the database at every request? Seems like too much.
activeTheme, err := database.RetrieveActiveTheme()
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
http.ServeFile(w, r, filepath.Join(filenames.ThemesFilepath, *activeTheme, r.URL.Path))
return
}))
}
func InitializeBlog(mux *http.ServeMux) {
mux.Handle("/", http.HandlerFunc(postHandler))
mux.Handle("/page/", http.HandlerFunc(indexHandler))
mux.Handle("/author/", http.HandlerFunc(authorHandler))
mux.Handle("/tag/", http.HandlerFunc(tagHandler))
// Handle for serving asset files
generateAssetHandler(mux)
mux.Handle("/images/", http.StripPrefix("/images/", http.FileServer(http.Dir(filenames.ImagesFilepath))))
mux.Handle("/public/", http.StripPrefix("/public/", http.FileServer(http.Dir(filenames.PublicFilepath))))
}