This repository has been archived by the owner on Feb 17, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 53
/
handlers.go
192 lines (174 loc) · 6.15 KB
/
handlers.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
package main
import (
"fmt"
"io/ioutil"
"net/http"
"path/filepath"
"strconv"
"strings"
"time"
"github.com/gorilla/mux"
"github.com/kjk/u"
)
// url: /{forum}/viewraw?topicId=${topicId}&postId=${postId}
func handleViewRaw(w http.ResponseWriter, r *http.Request) {
forum, topicID, postID := getTopicAndPostID(w, r)
if 0 == topicID {
http.Redirect(w, r, fmt.Sprintf("/%s/", forum.ForumUrl), 302)
return
}
topic := forum.Store.TopicByID(topicID)
if nil == topic {
logger.Noticef("handleViewRaw(): didn't find topic with id %d, referer: %q", topicID, getReferer(r))
http.Redirect(w, r, fmt.Sprintf("/%s/", forum.ForumUrl), 302)
return
}
post := topic.Posts[postID-1]
w.Header().Set("Content-Type", "text/plain")
sha1 := post.MessageSha1
msgFilePath := forum.Store.MessageFilePath(sha1)
msg, _ := ioutil.ReadFile(msgFilePath)
w.Write([]byte("****** Raw:\n"))
w.Write(msg)
w.Write([]byte("\n\n****** Converted:\n"))
w.Write([]byte(msgToHtml(string(msg))))
}
func serveFileFromDir(w http.ResponseWriter, r *http.Request, dir, fileName string) {
filePath := filepath.Join(dir, fileName)
if !u.PathExists(filePath) {
logger.Noticef("serveFileFromDir() file %q doesn't exist, referer: %q", fileName, getReferer(r))
}
http.ServeFile(w, r, filePath)
}
// url: /s/*
func handleStatic(w http.ResponseWriter, r *http.Request) {
file := r.URL.Path[len("/s/"):]
serveFileFromDir(w, r, "static", file)
}
// url: /img/*
func handleStaticImg(w http.ResponseWriter, r *http.Request) {
file := r.URL.Path[len("/img/"):]
serveFileFromDir(w, r, "img", file)
}
// url: /robots.txt
func handleRobotsTxt(w http.ResponseWriter, r *http.Request) {
serveFileFromDir(w, r, "static", "robots.txt")
}
func getTopicAndPostID(w http.ResponseWriter, r *http.Request) (*Forum, int, int) {
forum := mustGetForum(w, r)
if forum == nil {
http.Redirect(w, r, "/", 302)
return nil, 0, 0
}
topicIDStr := strings.TrimSpace(r.FormValue("topicId"))
postIDStr := strings.TrimSpace(r.FormValue("postId"))
topicID, err := strconv.Atoi(topicIDStr)
if err != nil || topicID == 0 {
http.Redirect(w, r, fmt.Sprintf("/%s/", forum.ForumUrl), 302)
return nil, 0, 0
}
postID, err := strconv.Atoi(postIDStr)
if err != nil || postID == 0 {
http.Redirect(w, r, fmt.Sprintf("/%s/", forum.ForumUrl), 302)
return forum, 0, 0
}
return forum, topicID, postID
}
// url: /{forum}/postdel?topicId=${topicId}&postId=${postId}
func handlePostDelete(w http.ResponseWriter, r *http.Request) {
if forum, topicID, postID := getTopicAndPostID(w, r); forum != nil {
//fmt.Printf("handlePostDelete(): forum: %q, topicId: %d, postId: %d\n", forum.ForumUrl, topicId, postId)
// TODO: handle error?
forum.Store.DeletePost(topicID, postID)
http.Redirect(w, r, fmt.Sprintf("/%s/topic?id=%d", forum.ForumUrl, topicID), 302)
}
}
// url: /{forum}/postundel?topicId=${topicId}&postId=${postId}
func handlePostUndelete(w http.ResponseWriter, r *http.Request) {
if forum, topicID, postID := getTopicAndPostID(w, r); forum != nil {
//fmt.Printf("handlePostUndelete(): forum: %q, topicId: %d, postId: %d\n", forum.ForumUrl, topicId, postId)
// TODO: handle error?
forum.Store.UndeletePost(topicID, postID)
http.Redirect(w, r, fmt.Sprintf("/%s/topic?id=%d", forum.ForumUrl, topicID), 302)
}
}
func getIPAddr(w http.ResponseWriter, r *http.Request) (*Forum, string) {
forum := mustGetForum(w, r)
if forum == nil {
http.Redirect(w, r, "/", 302)
return nil, ""
}
ipAddr := strings.TrimSpace(r.FormValue("ip"))
if ipAddr == "" {
http.Redirect(w, r, fmt.Sprintf("/%s/", forum.ForumUrl), 302)
return nil, ""
}
return forum, ipAddr
}
// url: /{forum}/blockip?ip=${ip}
func handleBlockIP(w http.ResponseWriter, r *http.Request) {
if forum, ip := getIPAddr(w, r); forum != nil {
//fmt.Printf("handleBlockIP(): forum: %q, ip: %s\n", forum.ForumUrl, ip)
forum.Store.BlockIP(ip)
http.Redirect(w, r, fmt.Sprintf("/%s/postsby?ip=%s", forum.ForumUrl, ip), 302)
}
}
// url: /{forum}/unblockip?ip=${ip}
func handleUnblockIP(w http.ResponseWriter, r *http.Request) {
if forum, ip := getIPAddr(w, r); forum != nil {
//fmt.Printf("handleUnblockIP(): forum: %q, ip: %s\n", forum.ForumUrl, ip)
forum.Store.UnblockIP(ip)
http.Redirect(w, r, fmt.Sprintf("/%s/postsby?ip=%s", forum.ForumUrl, ip), 302)
}
}
// url: /
func handleMain(w http.ResponseWriter, r *http.Request) {
if !isTopLevelURL(r.URL.Path) {
http.NotFound(w, r)
return
}
model := struct {
Forums *[]*Forum
AnalyticsCode string
}{
Forums: &appState.Forums,
AnalyticsCode: *config.AnalyticsCode,
}
ExecTemplate(w, tmplMain, model)
}
// // https://blog.gopheracademy.com/advent-2016/exposing-go-on-the-internet/
func initHTTPServer() *http.Server {
r := mux.NewRouter()
r.HandleFunc("/", makeTimingHandler(handleMain))
r.HandleFunc("/{forum}", makeTimingHandler(handleForum))
r.HandleFunc("/{forum}/", makeTimingHandler(handleForum))
r.HandleFunc("/{forum}/rss", makeTimingHandler(handleRss))
r.HandleFunc("/{forum}/rssall", makeTimingHandler(handleRssAll))
r.HandleFunc("/{forum}/topic", makeTimingHandler(handleTopic))
r.HandleFunc("/{forum}/postsby", makeTimingHandler(handlePostsBy))
r.HandleFunc("/{forum}/postdel", makeTimingHandler(handlePostDelete))
r.HandleFunc("/{forum}/postundel", makeTimingHandler(handlePostUndelete))
r.HandleFunc("/{forum}/viewraw", makeTimingHandler(handleViewRaw))
r.HandleFunc("/{forum}/newpost", makeTimingHandler(handleNewPost))
r.HandleFunc("/{forum}/blockip", makeTimingHandler(handleBlockIP))
r.HandleFunc("/{forum}/unblockip", makeTimingHandler(handleUnblockIP))
smux := &http.ServeMux{}
smux.HandleFunc("/oauthtwittercb", handleOauthTwitterCallback)
smux.HandleFunc("/login", handleLogin)
smux.HandleFunc("/logout", handleLogout)
smux.HandleFunc("/favicon.ico", http.NotFound)
smux.HandleFunc("/robots.txt", handleRobotsTxt)
smux.HandleFunc("/logs", handleLogs)
smux.HandleFunc("/s/", makeTimingHandler(handleStatic))
smux.HandleFunc("/img/", makeTimingHandler(handleStaticImg))
smux.Handle("/", r)
srv := &http.Server{
ReadTimeout: 5 * time.Second,
WriteTimeout: 5 * time.Second,
// TODO: 1.8 only
// IdleTimeout: 120 * time.Second,
Handler: smux,
}
// TODO: track connections and their state
return srv
}