10 "github.com/gorilla/mux"
14 ctx = context.Background()
15 cookieAge = "31536000"
18 func NewHandler(s Service, staticDir string) http.Handler {
21 r.PathPrefix("/static").Handler(http.StripPrefix("/static",
22 http.FileServer(http.Dir(path.Join(".", staticDir)))))
24 r.HandleFunc("/", func(w http.ResponseWriter, req *http.Request) {
27 sessionID, _ := req.Cookie("session_id")
28 if sessionID != nil && len(sessionID.Value) > 0 {
29 location = "/timeline"
32 w.Header().Add("Location", location)
33 w.WriteHeader(http.StatusFound)
34 }).Methods(http.MethodGet)
36 r.HandleFunc("/signin", func(w http.ResponseWriter, req *http.Request) {
37 err := s.ServeSigninPage(ctx, w)
39 s.ServeErrorPage(ctx, w, err)
42 }).Methods(http.MethodGet)
44 r.HandleFunc("/signin", func(w http.ResponseWriter, req *http.Request) {
45 instance := req.FormValue("instance")
46 url, sessionId, err := s.GetAuthUrl(ctx, instance)
48 s.ServeErrorPage(ctx, w, err)
52 w.Header().Add("Set-Cookie", fmt.Sprintf("session_id=%s;max-age=%s", sessionId, cookieAge))
53 w.Header().Add("Location", url)
54 w.WriteHeader(http.StatusFound)
55 }).Methods(http.MethodPost)
57 r.HandleFunc("/oauth_callback", func(w http.ResponseWriter, req *http.Request) {
58 ctx := getContextWithSession(context.Background(), req)
59 token := req.URL.Query().Get("code")
60 _, err := s.GetUserToken(ctx, "", nil, token)
62 s.ServeErrorPage(ctx, w, err)
66 w.Header().Add("Location", "/timeline")
67 w.WriteHeader(http.StatusFound)
68 }).Methods(http.MethodGet)
70 r.HandleFunc("/timeline", func(w http.ResponseWriter, req *http.Request) {
71 ctx := getContextWithSession(context.Background(), req)
73 maxID := req.URL.Query().Get("max_id")
74 sinceID := req.URL.Query().Get("since_id")
75 minID := req.URL.Query().Get("min_id")
77 err := s.ServeTimelinePage(ctx, w, nil, maxID, sinceID, minID)
79 s.ServeErrorPage(ctx, w, err)
82 }).Methods(http.MethodGet)
84 r.HandleFunc("/thread/{id}", func(w http.ResponseWriter, req *http.Request) {
85 ctx := getContextWithSession(context.Background(), req)
86 id, _ := mux.Vars(req)["id"]
87 reply := req.URL.Query().Get("reply")
88 err := s.ServeThreadPage(ctx, w, nil, id, len(reply) > 1)
90 s.ServeErrorPage(ctx, w, err)
93 }).Methods(http.MethodGet)
95 r.HandleFunc("/like/{id}", func(w http.ResponseWriter, req *http.Request) {
96 ctx := getContextWithSession(context.Background(), req)
97 id, _ := mux.Vars(req)["id"]
98 err := s.Like(ctx, w, nil, id)
100 s.ServeErrorPage(ctx, w, err)
104 w.Header().Add("Location", req.Header.Get("Referer")+"#status-"+id)
105 w.WriteHeader(http.StatusFound)
106 }).Methods(http.MethodGet)
108 r.HandleFunc("/unlike/{id}", func(w http.ResponseWriter, req *http.Request) {
109 ctx := getContextWithSession(context.Background(), req)
110 id, _ := mux.Vars(req)["id"]
111 err := s.UnLike(ctx, w, nil, id)
113 s.ServeErrorPage(ctx, w, err)
117 w.Header().Add("Location", req.Header.Get("Referer")+"#status-"+id)
118 w.WriteHeader(http.StatusFound)
119 }).Methods(http.MethodGet)
121 r.HandleFunc("/retweet/{id}", func(w http.ResponseWriter, req *http.Request) {
122 ctx := getContextWithSession(context.Background(), req)
123 id, _ := mux.Vars(req)["id"]
124 err := s.Retweet(ctx, w, nil, id)
126 s.ServeErrorPage(ctx, w, err)
130 w.Header().Add("Location", req.Header.Get("Referer")+"#status-"+id)
131 w.WriteHeader(http.StatusFound)
132 }).Methods(http.MethodGet)
134 r.HandleFunc("/unretweet/{id}", func(w http.ResponseWriter, req *http.Request) {
135 ctx := getContextWithSession(context.Background(), req)
136 id, _ := mux.Vars(req)["id"]
137 err := s.UnRetweet(ctx, w, nil, id)
139 s.ServeErrorPage(ctx, w, err)
143 w.Header().Add("Location", req.Header.Get("Referer")+"#status-"+id)
144 w.WriteHeader(http.StatusFound)
145 }).Methods(http.MethodGet)
147 r.HandleFunc("/post", func(w http.ResponseWriter, req *http.Request) {
148 ctx := getContextWithSession(context.Background(), req)
150 err := req.ParseMultipartForm(4 << 20)
152 s.ServeErrorPage(ctx, w, err)
156 content := getMultipartFormValue(req.MultipartForm, "content")
157 replyToID := getMultipartFormValue(req.MultipartForm, "reply_to_id")
158 visibility := getMultipartFormValue(req.MultipartForm, "visibility")
159 isNSFW := "on" == getMultipartFormValue(req.MultipartForm, "is_nsfw")
161 files := req.MultipartForm.File["attachments"]
163 id, err := s.PostTweet(ctx, w, nil, content, replyToID, visibility, isNSFW, files)
165 s.ServeErrorPage(ctx, w, err)
169 location := "/timeline" + "#status-" + id
170 if len(replyToID) > 0 {
171 location = "/thread/" + replyToID + "#status-" + id
173 w.Header().Add("Location", location)
174 w.WriteHeader(http.StatusFound)
175 }).Methods(http.MethodPost)
177 r.HandleFunc("/notifications", func(w http.ResponseWriter, req *http.Request) {
178 ctx := getContextWithSession(context.Background(), req)
180 maxID := req.URL.Query().Get("max_id")
181 minID := req.URL.Query().Get("min_id")
183 err := s.ServeNotificationPage(ctx, w, nil, maxID, minID)
185 s.ServeErrorPage(ctx, w, err)
188 }).Methods(http.MethodGet)
190 r.HandleFunc("/user/{id}", func(w http.ResponseWriter, req *http.Request) {
191 ctx := getContextWithSession(context.Background(), req)
193 id, _ := mux.Vars(req)["id"]
194 maxID := req.URL.Query().Get("max_id")
195 minID := req.URL.Query().Get("min_id")
197 err := s.ServeUserPage(ctx, w, nil, id, maxID, minID)
199 s.ServeErrorPage(ctx, w, err)
202 }).Methods(http.MethodGet)
204 r.HandleFunc("/follow/{id}", func(w http.ResponseWriter, req *http.Request) {
205 ctx := getContextWithSession(context.Background(), req)
207 id, _ := mux.Vars(req)["id"]
209 err := s.Follow(ctx, w, nil, id)
211 s.ServeErrorPage(ctx, w, err)
215 w.Header().Add("Location", req.Header.Get("Referer"))
216 w.WriteHeader(http.StatusFound)
217 }).Methods(http.MethodPost)
219 r.HandleFunc("/unfollow/{id}", func(w http.ResponseWriter, req *http.Request) {
220 ctx := getContextWithSession(context.Background(), req)
222 id, _ := mux.Vars(req)["id"]
224 err := s.UnFollow(ctx, w, nil, id)
226 s.ServeErrorPage(ctx, w, err)
230 w.Header().Add("Location", req.Header.Get("Referer"))
231 w.WriteHeader(http.StatusFound)
232 }).Methods(http.MethodPost)
234 r.HandleFunc("/about", func(w http.ResponseWriter, req *http.Request) {
235 ctx := getContextWithSession(context.Background(), req)
237 err := s.ServeAboutPage(ctx, w, nil)
239 s.ServeErrorPage(ctx, w, err)
242 }).Methods(http.MethodGet)
244 r.HandleFunc("/emojis", func(w http.ResponseWriter, req *http.Request) {
245 ctx := getContextWithSession(context.Background(), req)
247 err := s.ServeEmojiPage(ctx, w, nil)
249 s.ServeErrorPage(ctx, w, err)
252 }).Methods(http.MethodGet)
254 r.HandleFunc("/signout", func(w http.ResponseWriter, req *http.Request) {
255 // TODO remove session from database
256 w.Header().Add("Set-Cookie", fmt.Sprintf("session_id=;max-age=0"))
257 w.Header().Add("Location", "/")
258 w.WriteHeader(http.StatusFound)
259 }).Methods(http.MethodGet)
264 func getContextWithSession(ctx context.Context, req *http.Request) context.Context {
265 sessionID, err := req.Cookie("session_id")
269 return context.WithValue(ctx, "session_id", sessionID.Value)
272 func getMultipartFormValue(mf *multipart.Form, key string) (val string) {
273 vals, ok := mf.Value[key]