From 5e4da01c3ae3ae2e870faba9085d9d9213c01c29 Mon Sep 17 00:00:00 2001 From: r Date: Fri, 13 Dec 2019 18:08:26 +0000 Subject: Initial commit --- service/transport.go | 165 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 165 insertions(+) create mode 100644 service/transport.go (limited to 'service/transport.go') diff --git a/service/transport.go b/service/transport.go new file mode 100644 index 0000000..f4f5ed7 --- /dev/null +++ b/service/transport.go @@ -0,0 +1,165 @@ +package service + +import ( + "context" + "fmt" + "net/http" + "path" + + "github.com/gorilla/mux" +) + +var ( + ctx = context.Background() + cookieAge = "31536000" +) + +func getContextWithSession(ctx context.Context, req *http.Request) context.Context { + sessionID, err := req.Cookie("session_id") + if err != nil { + return ctx + } + return context.WithValue(ctx, "session_id", sessionID.Value) +} + +func NewHandler(s Service, staticDir string) http.Handler { + r := mux.NewRouter() + + r.PathPrefix("/static").Handler(http.StripPrefix("/static", + http.FileServer(http.Dir(path.Join(".", staticDir))))) + + r.HandleFunc("/", func(w http.ResponseWriter, req *http.Request) { + err := s.ServeHomePage(ctx, w) + if err != nil { + s.ServeErrorPage(ctx, w, err) + return + } + }).Methods(http.MethodGet) + + r.HandleFunc("/signin", func(w http.ResponseWriter, req *http.Request) { + err := s.ServeSigninPage(ctx, w) + if err != nil { + s.ServeErrorPage(ctx, w, err) + return + } + }).Methods(http.MethodGet) + + r.HandleFunc("/signin", func(w http.ResponseWriter, req *http.Request) { + instance := req.FormValue("instance") + url, sessionId, err := s.GetAuthUrl(ctx, instance) + if err != nil { + s.ServeErrorPage(ctx, w, err) + return + } + + w.Header().Add("Set-Cookie", fmt.Sprintf("session_id=%s;max-age=%s", sessionId, cookieAge)) + w.Header().Add("Location", url) + w.WriteHeader(http.StatusSeeOther) + }).Methods(http.MethodPost) + + r.HandleFunc("/oauth_callback", func(w http.ResponseWriter, req *http.Request) { + ctx := getContextWithSession(context.Background(), req) + token := req.URL.Query().Get("code") + _, err := s.GetUserToken(ctx, "", nil, token) + if err != nil { + s.ServeErrorPage(ctx, w, err) + return + } + + w.Header().Add("Location", "/timeline") + w.WriteHeader(http.StatusSeeOther) + }).Methods(http.MethodGet) + + r.HandleFunc("/timeline", func(w http.ResponseWriter, req *http.Request) { + ctx := getContextWithSession(context.Background(), req) + + maxID := req.URL.Query().Get("max_id") + sinceID := req.URL.Query().Get("since_id") + minID := req.URL.Query().Get("min_id") + + err := s.ServeTimelinePage(ctx, w, nil, maxID, sinceID, minID) + if err != nil { + s.ServeErrorPage(ctx, w, err) + return + } + }).Methods(http.MethodGet) + + r.HandleFunc("/thread/{id}", func(w http.ResponseWriter, req *http.Request) { + ctx := getContextWithSession(context.Background(), req) + id, _ := mux.Vars(req)["id"] + reply := req.URL.Query().Get("reply") + err := s.ServeThreadPage(ctx, w, nil, id, len(reply) > 1) + if err != nil { + s.ServeErrorPage(ctx, w, err) + return + } + }).Methods(http.MethodGet) + + r.HandleFunc("/like/{id}", func(w http.ResponseWriter, req *http.Request) { + ctx := getContextWithSession(context.Background(), req) + id, _ := mux.Vars(req)["id"] + err := s.Like(ctx, w, nil, id) + if err != nil { + s.ServeErrorPage(ctx, w, err) + return + } + + w.Header().Add("Location", req.Header.Get("Referer")) + w.WriteHeader(http.StatusSeeOther) + }).Methods(http.MethodGet) + + r.HandleFunc("/unlike/{id}", func(w http.ResponseWriter, req *http.Request) { + ctx := getContextWithSession(context.Background(), req) + id, _ := mux.Vars(req)["id"] + err := s.UnLike(ctx, w, nil, id) + if err != nil { + s.ServeErrorPage(ctx, w, err) + return + } + + w.Header().Add("Location", req.Header.Get("Referer")) + w.WriteHeader(http.StatusSeeOther) + }).Methods(http.MethodGet) + + r.HandleFunc("/retweet/{id}", func(w http.ResponseWriter, req *http.Request) { + ctx := getContextWithSession(context.Background(), req) + id, _ := mux.Vars(req)["id"] + err := s.Retweet(ctx, w, nil, id) + if err != nil { + s.ServeErrorPage(ctx, w, err) + return + } + + w.Header().Add("Location", req.Header.Get("Referer")) + w.WriteHeader(http.StatusSeeOther) + }).Methods(http.MethodGet) + + r.HandleFunc("/unretweet/{id}", func(w http.ResponseWriter, req *http.Request) { + ctx := getContextWithSession(context.Background(), req) + id, _ := mux.Vars(req)["id"] + err := s.UnRetweet(ctx, w, nil, id) + if err != nil { + s.ServeErrorPage(ctx, w, err) + return + } + + w.Header().Add("Location", req.Header.Get("Referer")) + w.WriteHeader(http.StatusSeeOther) + }).Methods(http.MethodGet) + + r.HandleFunc("/post", func(w http.ResponseWriter, req *http.Request) { + ctx := getContextWithSession(context.Background(), req) + content := req.FormValue("content") + replyToID := req.FormValue("reply_to_id") + err := s.PostTweet(ctx, w, nil, content, replyToID) + if err != nil { + s.ServeErrorPage(ctx, w, err) + return + } + + w.Header().Add("Location", req.Header.Get("Referer")) + w.WriteHeader(http.StatusSeeOther) + }).Methods(http.MethodPost) + + return r +} -- cgit v1.2.3