9 "github.com/gorilla/mux"
13 ctx = context.Background()
14 cookieAge = "31536000"
17 func getContextWithSession(ctx context.Context, req *http.Request) context.Context {
18 sessionID, err := req.Cookie("session_id")
22 return context.WithValue(ctx, "session_id", sessionID.Value)
25 func NewHandler(s Service, staticDir string) http.Handler {
28 r.PathPrefix("/static").Handler(http.StripPrefix("/static",
29 http.FileServer(http.Dir(path.Join(".", staticDir)))))
31 r.HandleFunc("/", func(w http.ResponseWriter, req *http.Request) {
34 sessionID, _ := req.Cookie("session_id")
35 if sessionID != nil && len(sessionID.Value) > 0 {
36 location = "/timeline"
39 w.Header().Add("Location", location)
40 w.WriteHeader(http.StatusSeeOther)
41 }).Methods(http.MethodGet)
43 r.HandleFunc("/signin", func(w http.ResponseWriter, req *http.Request) {
44 err := s.ServeSigninPage(ctx, w)
46 s.ServeErrorPage(ctx, w, err)
49 }).Methods(http.MethodGet)
51 r.HandleFunc("/signin", func(w http.ResponseWriter, req *http.Request) {
52 instance := req.FormValue("instance")
53 url, sessionId, err := s.GetAuthUrl(ctx, instance)
55 s.ServeErrorPage(ctx, w, err)
59 w.Header().Add("Set-Cookie", fmt.Sprintf("session_id=%s;max-age=%s", sessionId, cookieAge))
60 w.Header().Add("Location", url)
61 w.WriteHeader(http.StatusSeeOther)
62 }).Methods(http.MethodPost)
64 r.HandleFunc("/oauth_callback", func(w http.ResponseWriter, req *http.Request) {
65 ctx := getContextWithSession(context.Background(), req)
66 token := req.URL.Query().Get("code")
67 _, err := s.GetUserToken(ctx, "", nil, token)
69 s.ServeErrorPage(ctx, w, err)
73 w.Header().Add("Location", "/timeline")
74 w.WriteHeader(http.StatusSeeOther)
75 }).Methods(http.MethodGet)
77 r.HandleFunc("/timeline", func(w http.ResponseWriter, req *http.Request) {
78 ctx := getContextWithSession(context.Background(), req)
80 maxID := req.URL.Query().Get("max_id")
81 sinceID := req.URL.Query().Get("since_id")
82 minID := req.URL.Query().Get("min_id")
84 err := s.ServeTimelinePage(ctx, w, nil, maxID, sinceID, minID)
86 s.ServeErrorPage(ctx, w, err)
89 }).Methods(http.MethodGet)
91 r.HandleFunc("/thread/{id}", func(w http.ResponseWriter, req *http.Request) {
92 ctx := getContextWithSession(context.Background(), req)
93 id, _ := mux.Vars(req)["id"]
94 reply := req.URL.Query().Get("reply")
95 err := s.ServeThreadPage(ctx, w, nil, id, len(reply) > 1)
97 s.ServeErrorPage(ctx, w, err)
100 }).Methods(http.MethodGet)
102 r.HandleFunc("/like/{id}", func(w http.ResponseWriter, req *http.Request) {
103 ctx := getContextWithSession(context.Background(), req)
104 id, _ := mux.Vars(req)["id"]
105 err := s.Like(ctx, w, nil, id)
107 s.ServeErrorPage(ctx, w, err)
111 w.Header().Add("Location", req.Header.Get("Referer"))
112 w.WriteHeader(http.StatusSeeOther)
113 }).Methods(http.MethodGet)
115 r.HandleFunc("/unlike/{id}", func(w http.ResponseWriter, req *http.Request) {
116 ctx := getContextWithSession(context.Background(), req)
117 id, _ := mux.Vars(req)["id"]
118 err := s.UnLike(ctx, w, nil, id)
120 s.ServeErrorPage(ctx, w, err)
124 w.Header().Add("Location", req.Header.Get("Referer"))
125 w.WriteHeader(http.StatusSeeOther)
126 }).Methods(http.MethodGet)
128 r.HandleFunc("/retweet/{id}", func(w http.ResponseWriter, req *http.Request) {
129 ctx := getContextWithSession(context.Background(), req)
130 id, _ := mux.Vars(req)["id"]
131 err := s.Retweet(ctx, w, nil, id)
133 s.ServeErrorPage(ctx, w, err)
137 w.Header().Add("Location", req.Header.Get("Referer"))
138 w.WriteHeader(http.StatusSeeOther)
139 }).Methods(http.MethodGet)
141 r.HandleFunc("/unretweet/{id}", func(w http.ResponseWriter, req *http.Request) {
142 ctx := getContextWithSession(context.Background(), req)
143 id, _ := mux.Vars(req)["id"]
144 err := s.UnRetweet(ctx, w, nil, id)
146 s.ServeErrorPage(ctx, w, err)
150 w.Header().Add("Location", req.Header.Get("Referer"))
151 w.WriteHeader(http.StatusSeeOther)
152 }).Methods(http.MethodGet)
154 r.HandleFunc("/post", func(w http.ResponseWriter, req *http.Request) {
155 ctx := getContextWithSession(context.Background(), req)
156 content := req.FormValue("content")
157 replyToID := req.FormValue("reply_to_id")
158 err := s.PostTweet(ctx, w, nil, content, replyToID)
160 s.ServeErrorPage(ctx, w, err)
164 location := "/timeline"
165 if len(replyToID) > 0 {
166 location = "/thread/" + replyToID
168 w.Header().Add("Location", location)
169 w.WriteHeader(http.StatusSeeOther)
170 }).Methods(http.MethodPost)
172 r.HandleFunc("/signout", func(w http.ResponseWriter, req *http.Request) {
173 // TODO remove session from database
174 w.Header().Add("Set-Cookie", fmt.Sprintf("session_id=;max-age=0"))
175 w.Header().Add("Location", "/")
176 w.WriteHeader(http.StatusSeeOther)
177 }).Methods(http.MethodGet)