12 "github.com/gorilla/mux"
16 ctx = context.Background()
17 cookieAge = "31536000"
20 func NewHandler(s Service, staticDir string) http.Handler {
23 r.PathPrefix("/static").Handler(http.StripPrefix("/static",
24 http.FileServer(http.Dir(path.Join(".", staticDir)))))
26 r.HandleFunc("/", func(w http.ResponseWriter, req *http.Request) {
29 sessionID, _ := req.Cookie("session_id")
30 if sessionID != nil && len(sessionID.Value) > 0 {
31 location = "/timeline/home"
34 w.Header().Add("Location", location)
35 w.WriteHeader(http.StatusFound)
36 }).Methods(http.MethodGet)
38 r.HandleFunc("/signin", func(w http.ResponseWriter, req *http.Request) {
39 err := s.ServeSigninPage(ctx, w)
41 s.ServeErrorPage(ctx, w, err)
44 }).Methods(http.MethodGet)
46 r.HandleFunc("/signin", func(w http.ResponseWriter, req *http.Request) {
47 instance := req.FormValue("instance")
48 url, sessionId, err := s.GetAuthUrl(ctx, instance)
50 s.ServeErrorPage(ctx, w, err)
54 w.Header().Add("Set-Cookie", fmt.Sprintf("session_id=%s;max-age=%s", sessionId, cookieAge))
55 w.Header().Add("Location", url)
56 w.WriteHeader(http.StatusFound)
57 }).Methods(http.MethodPost)
59 r.HandleFunc("/oauth_callback", func(w http.ResponseWriter, req *http.Request) {
60 ctx := getContextWithSession(context.Background(), req)
61 token := req.URL.Query().Get("code")
62 _, err := s.GetUserToken(ctx, "", nil, token)
64 s.ServeErrorPage(ctx, w, err)
68 w.Header().Add("Location", "/timeline/home")
69 w.WriteHeader(http.StatusFound)
70 }).Methods(http.MethodGet)
72 r.HandleFunc("/timeline", func(w http.ResponseWriter, req *http.Request) {
73 w.Header().Add("Location", "/timeline/home")
74 w.WriteHeader(http.StatusFound)
75 }).Methods(http.MethodGet)
77 r.HandleFunc("/timeline/{type}", func(w http.ResponseWriter, req *http.Request) {
78 ctx := getContextWithSession(context.Background(), req)
80 timelineType, _ := mux.Vars(req)["type"]
81 maxID := req.URL.Query().Get("max_id")
82 sinceID := req.URL.Query().Get("since_id")
83 minID := req.URL.Query().Get("min_id")
85 err := s.ServeTimelinePage(ctx, w, nil, timelineType, maxID, sinceID, minID)
87 s.ServeErrorPage(ctx, w, err)
90 }).Methods(http.MethodGet)
92 r.HandleFunc("/thread/{id}", func(w http.ResponseWriter, req *http.Request) {
93 ctx := getContextWithSession(context.Background(), req)
94 id, _ := mux.Vars(req)["id"]
95 reply := req.URL.Query().Get("reply")
96 err := s.ServeThreadPage(ctx, w, nil, id, len(reply) > 1)
98 s.ServeErrorPage(ctx, w, err)
101 }).Methods(http.MethodGet)
103 r.HandleFunc("/likedby/{id}", func(w http.ResponseWriter, req *http.Request) {
104 ctx := getContextWithSession(context.Background(), req)
105 id, _ := mux.Vars(req)["id"]
107 err := s.ServeLikedByPage(ctx, w, nil, id)
109 s.ServeErrorPage(ctx, w, err)
112 }).Methods(http.MethodGet)
114 r.HandleFunc("/retweetedby/{id}", func(w http.ResponseWriter, req *http.Request) {
115 ctx := getContextWithSession(context.Background(), req)
116 id, _ := mux.Vars(req)["id"]
118 err := s.ServeRetweetedByPage(ctx, w, nil, id)
120 s.ServeErrorPage(ctx, w, err)
123 }).Methods(http.MethodGet)
125 r.HandleFunc("/like/{id}", func(w http.ResponseWriter, req *http.Request) {
126 ctx := getContextWithSession(context.Background(), req)
127 id, _ := mux.Vars(req)["id"]
128 err := s.Like(ctx, w, nil, id)
130 s.ServeErrorPage(ctx, w, err)
134 w.Header().Add("Location", req.Header.Get("Referer")+"#status-"+id)
135 w.WriteHeader(http.StatusFound)
136 }).Methods(http.MethodGet)
138 r.HandleFunc("/unlike/{id}", func(w http.ResponseWriter, req *http.Request) {
139 ctx := getContextWithSession(context.Background(), req)
140 id, _ := mux.Vars(req)["id"]
141 err := s.UnLike(ctx, w, nil, id)
143 s.ServeErrorPage(ctx, w, err)
147 w.Header().Add("Location", req.Header.Get("Referer")+"#status-"+id)
148 w.WriteHeader(http.StatusFound)
149 }).Methods(http.MethodGet)
151 r.HandleFunc("/retweet/{id}", func(w http.ResponseWriter, req *http.Request) {
152 ctx := getContextWithSession(context.Background(), req)
153 id, _ := mux.Vars(req)["id"]
154 err := s.Retweet(ctx, w, nil, id)
156 s.ServeErrorPage(ctx, w, err)
160 w.Header().Add("Location", req.Header.Get("Referer")+"#status-"+id)
161 w.WriteHeader(http.StatusFound)
162 }).Methods(http.MethodGet)
164 r.HandleFunc("/unretweet/{id}", func(w http.ResponseWriter, req *http.Request) {
165 ctx := getContextWithSession(context.Background(), req)
166 id, _ := mux.Vars(req)["id"]
167 err := s.UnRetweet(ctx, w, nil, id)
169 s.ServeErrorPage(ctx, w, err)
173 w.Header().Add("Location", req.Header.Get("Referer")+"#status-"+id)
174 w.WriteHeader(http.StatusFound)
175 }).Methods(http.MethodGet)
177 r.HandleFunc("/post", func(w http.ResponseWriter, req *http.Request) {
178 ctx := getContextWithSession(context.Background(), req)
180 err := req.ParseMultipartForm(4 << 20)
182 s.ServeErrorPage(ctx, w, err)
186 content := getMultipartFormValue(req.MultipartForm, "content")
187 replyToID := getMultipartFormValue(req.MultipartForm, "reply_to_id")
188 format := getMultipartFormValue(req.MultipartForm, "format")
189 visibility := getMultipartFormValue(req.MultipartForm, "visibility")
190 isNSFW := "on" == getMultipartFormValue(req.MultipartForm, "is_nsfw")
192 files := req.MultipartForm.File["attachments"]
194 id, err := s.PostTweet(ctx, w, nil, content, replyToID, format, visibility, isNSFW, files)
196 s.ServeErrorPage(ctx, w, err)
200 location := "/timeline/home" + "#status-" + id
201 if len(replyToID) > 0 {
202 location = "/thread/" + replyToID + "#status-" + id
204 w.Header().Add("Location", location)
205 w.WriteHeader(http.StatusFound)
206 }).Methods(http.MethodPost)
208 r.HandleFunc("/notifications", func(w http.ResponseWriter, req *http.Request) {
209 ctx := getContextWithSession(context.Background(), req)
211 maxID := req.URL.Query().Get("max_id")
212 minID := req.URL.Query().Get("min_id")
214 err := s.ServeNotificationPage(ctx, w, nil, maxID, minID)
216 s.ServeErrorPage(ctx, w, err)
219 }).Methods(http.MethodGet)
221 r.HandleFunc("/user/{id}", func(w http.ResponseWriter, req *http.Request) {
222 ctx := getContextWithSession(context.Background(), req)
224 id, _ := mux.Vars(req)["id"]
225 maxID := req.URL.Query().Get("max_id")
226 minID := req.URL.Query().Get("min_id")
228 err := s.ServeUserPage(ctx, w, nil, id, maxID, minID)
230 s.ServeErrorPage(ctx, w, err)
233 }).Methods(http.MethodGet)
235 r.HandleFunc("/follow/{id}", func(w http.ResponseWriter, req *http.Request) {
236 ctx := getContextWithSession(context.Background(), req)
238 id, _ := mux.Vars(req)["id"]
240 err := s.Follow(ctx, w, nil, id)
242 s.ServeErrorPage(ctx, w, err)
246 w.Header().Add("Location", req.Header.Get("Referer"))
247 w.WriteHeader(http.StatusFound)
248 }).Methods(http.MethodPost)
250 r.HandleFunc("/unfollow/{id}", func(w http.ResponseWriter, req *http.Request) {
251 ctx := getContextWithSession(context.Background(), req)
253 id, _ := mux.Vars(req)["id"]
255 err := s.UnFollow(ctx, w, nil, id)
257 s.ServeErrorPage(ctx, w, err)
261 w.Header().Add("Location", req.Header.Get("Referer"))
262 w.WriteHeader(http.StatusFound)
263 }).Methods(http.MethodPost)
265 r.HandleFunc("/about", func(w http.ResponseWriter, req *http.Request) {
266 ctx := getContextWithSession(context.Background(), req)
268 err := s.ServeAboutPage(ctx, w, nil)
270 s.ServeErrorPage(ctx, w, err)
273 }).Methods(http.MethodGet)
275 r.HandleFunc("/emojis", func(w http.ResponseWriter, req *http.Request) {
276 ctx := getContextWithSession(context.Background(), req)
278 err := s.ServeEmojiPage(ctx, w, nil)
280 s.ServeErrorPage(ctx, w, err)
283 }).Methods(http.MethodGet)
285 r.HandleFunc("/search", func(w http.ResponseWriter, req *http.Request) {
286 ctx := getContextWithSession(context.Background(), req)
288 q := req.URL.Query().Get("q")
289 qType := req.URL.Query().Get("type")
290 offsetStr := req.URL.Query().Get("offset")
294 if len(offsetStr) > 1 {
295 offset, err = strconv.Atoi(offsetStr)
297 s.ServeErrorPage(ctx, w, err)
302 err = s.ServeSearchPage(ctx, w, nil, q, qType, offset)
304 s.ServeErrorPage(ctx, w, err)
307 }).Methods(http.MethodGet)
309 r.HandleFunc("/settings", func(w http.ResponseWriter, req *http.Request) {
310 ctx := getContextWithSession(context.Background(), req)
312 err := s.ServeSettingsPage(ctx, w, nil)
314 s.ServeErrorPage(ctx, w, err)
317 }).Methods(http.MethodGet)
319 r.HandleFunc("/settings", func(w http.ResponseWriter, req *http.Request) {
320 ctx := getContextWithSession(context.Background(), req)
322 visibility := req.FormValue("visibility")
323 copyScope := req.FormValue("copy_scope") == "true"
324 settings := &model.Settings{
325 DefaultVisibility: visibility,
326 CopyScope: copyScope,
329 err := s.SaveSettings(ctx, w, nil, settings)
331 s.ServeErrorPage(ctx, w, err)
335 w.Header().Add("Location", req.Header.Get("Referer"))
336 w.WriteHeader(http.StatusFound)
337 }).Methods(http.MethodPost)
339 r.HandleFunc("/signout", func(w http.ResponseWriter, req *http.Request) {
340 // TODO remove session from database
341 w.Header().Add("Set-Cookie", fmt.Sprintf("session_id=;max-age=0"))
342 w.Header().Add("Location", "/")
343 w.WriteHeader(http.StatusFound)
344 }).Methods(http.MethodGet)
349 func getContextWithSession(ctx context.Context, req *http.Request) context.Context {
350 sessionID, err := req.Cookie("session_id")
354 return context.WithValue(ctx, "session_id", sessionID.Value)
357 func getMultipartFormValue(mf *multipart.Form, key string) (val string) {
358 vals, ok := mf.Value[key]