forked from kabukky/journey
-
Notifications
You must be signed in to change notification settings - Fork 0
/
blog.go
167 lines (158 loc) · 4.48 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
165
166
167
package server
import (
"github.com/dimfeld/httptreemux"
"github.com/kabukky/journey/filenames"
"github.com/kabukky/journey/structure/methods"
"github.com/kabukky/journey/templates"
"net/http"
"path/filepath"
"strconv"
)
func indexHandler(w http.ResponseWriter, r *http.Request, params map[string]string) {
number := params["number"]
if number == "" {
// Render index template (first page)
err := templates.ShowIndexTemplate(w, r, 1)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
return
}
page, err := strconv.Atoi(number)
if err != nil {
http.Redirect(w, r, "/", http.StatusFound)
return
}
// Render index template
err = templates.ShowIndexTemplate(w, r, page)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
return
}
func authorHandler(w http.ResponseWriter, r *http.Request, params map[string]string) {
slug := params["slug"]
function := params["function"]
number := params["number"]
if function == "" {
// Render author template (first page)
err := templates.ShowAuthorTemplate(w, r, slug, 1)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
return
} else if function == "rss" {
// Render author rss feed
err := templates.ShowAuthorRss(w, slug)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
return
}
page, err := strconv.Atoi(number)
if err != nil {
http.Redirect(w, r, "/", http.StatusFound)
return
}
// Render author template
err = templates.ShowAuthorTemplate(w, r, slug, page)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
return
}
func tagHandler(w http.ResponseWriter, r *http.Request, params map[string]string) {
slug := params["slug"]
function := params["function"]
number := params["number"]
if function == "" {
// Render tag template (first page)
err := templates.ShowTagTemplate(w, r, slug, 1)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
return
} else if function == "rss" {
// Render tag rss feed
err := templates.ShowTagRss(w, slug)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
return
}
page, err := strconv.Atoi(number)
if err != nil {
http.Redirect(w, r, "/", http.StatusFound)
return
}
// Render tag template
err = templates.ShowTagTemplate(w, r, slug, page)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
return
}
func postHandler(w http.ResponseWriter, r *http.Request, params map[string]string) {
slug := params["slug"]
if slug == "" {
http.Redirect(w, r, "/", http.StatusFound)
return
} else if slug == "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, r, slug)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
return
}
func assetsHandler(w http.ResponseWriter, r *http.Request, params map[string]string) {
// Read lock global blog
methods.Blog.RLock()
defer methods.Blog.RUnlock()
http.ServeFile(w, r, filepath.Join(filenames.ThemesFilepath, methods.Blog.ActiveTheme, "assets", params["filepath"]))
return
}
func imagesHandler(w http.ResponseWriter, r *http.Request, params map[string]string) {
http.ServeFile(w, r, filepath.Join(filenames.ImagesFilepath, params["filepath"]))
return
}
func publicHandler(w http.ResponseWriter, r *http.Request, params map[string]string) {
http.ServeFile(w, r, filepath.Join(filenames.PublicFilepath, params["filepath"]))
return
}
func InitializeBlog(router *httptreemux.TreeMux) {
// For index
router.GET("/", indexHandler)
router.GET("/:slug/", postHandler)
router.GET("/page/:number/", indexHandler)
// For author
router.GET("/author/:slug/", authorHandler)
router.GET("/author/:slug/:function/", authorHandler)
router.GET("/author/:slug/:function/:number/", authorHandler)
// For tag
router.GET("/tag/:slug/", tagHandler)
router.GET("/tag/:slug/:function/", tagHandler)
router.GET("/tag/:slug/:function/:number/", tagHandler)
// For serving asset files
router.GET("/assets/*filepath", assetsHandler)
router.GET("/images/*filepath", imagesHandler)
router.GET("/content/images/*filepath", imagesHandler) // This is here to keep compatibility with Ghost
router.GET("/public/*filepath", publicHandler)
}