aboutsummaryrefslogtreecommitdiff
path: root/service/transport.go
blob: 709a2ff2b61f58e837ac1ac5454b08f502b39417 (plain)
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
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) {
		location := "/signin"

		sessionID, _ := req.Cookie("session_id")
		if sessionID != nil && len(sessionID.Value) > 0 {
			location = "/timeline"
		}

		w.Header().Add("Location", location)
		w.WriteHeader(http.StatusSeeOther)
	}).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
		}

		location := "/timeline"
		if len(replyToID) > 0 {
			location = "/thread/" + replyToID
		}
		w.Header().Add("Location", location)
		w.WriteHeader(http.StatusSeeOther)
	}).Methods(http.MethodPost)

	r.HandleFunc("/signout", func(w http.ResponseWriter, req *http.Request) {
		// TODO remove session from database
		w.Header().Add("Set-Cookie", fmt.Sprintf("session_id=;max-age=0"))
		w.Header().Add("Location", "/")
		w.WriteHeader(http.StatusSeeOther)
	}).Methods(http.MethodGet)

	return r
}