/
transport.go
163 lines (141 loc) · 4.07 KB
/
transport.go
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
package service
import (
"context"
"encoding/json"
"errors"
"github.com/go-kit/kit/endpoint"
"github.com/the-gigi/delinkcious/pkg/auth_util"
om "github.com/the-gigi/delinkcious/pkg/object_model"
"net/http"
"net/url"
"os"
"strings"
)
type followRequest struct {
Followed string `json:"followed"`
Follower string `json:"follower"`
}
type followResponse struct {
Err string `json:"err"`
}
type unfollowRequest struct {
Followed string `json:"followed"`
Follower string `json:"follower"`
}
type unfollowResponse struct {
Err string `json:"err"`
}
type getByUsernameRequest struct {
Username string `json:"username"`
}
type getFollowersResponse struct {
Followers map[string]bool `json:"followers"`
Err string `json:"err"`
}
type getFollowingResponse struct {
Following map[string]bool `json:"following"`
Err string `json:"err"`
}
func newGetByUsernameRequest(username string) (request getByUsernameRequest, err error) {
request.Username, err = url.PathUnescape(username)
return
}
func decodeFollowRequest(_ context.Context, r *http.Request) (interface{}, error) {
var request followRequest
err := json.NewDecoder(r.Body).Decode(&request)
if err != nil {
return nil, err
}
return request, nil
}
func decodeUnfollowRequest(_ context.Context, r *http.Request) (interface{}, error) {
var request unfollowRequest
err := json.NewDecoder(r.Body).Decode(&request)
if err != nil {
return nil, err
}
return request, nil
}
func decodeGetFollowingRequest(_ context.Context, r *http.Request) (interface{}, error) {
parts := strings.Split(r.URL.Path, "/")
username := parts[len(parts)-1]
if username == "" || username == "following" {
return nil, errors.New("user name must not be empty")
}
return newGetByUsernameRequest(username)
}
func decodeGetFollowersRequest(_ context.Context, r *http.Request) (interface{}, error) {
if os.Getenv("DELINKCIOUS_MUTUAL_AUTH") != "false" {
token := r.Header["Delinkcious-Caller-Token"]
if len(token) == 0 || token[0] == "" {
return nil, errors.New("missing caller token")
}
if !auth_util.HasCaller("link-manager", token[0]) {
return nil, errors.New("unauthorized caller")
}
}
parts := strings.Split(r.URL.Path, "/")
username := parts[len(parts)-1]
if username == "" || username == "followers" {
return nil, errors.New("user name must not be empty")
}
return newGetByUsernameRequest(username)
}
func encodeResponse(_ context.Context, w http.ResponseWriter, response interface{}) error {
return json.NewEncoder(w).Encode(response)
}
func makeFollowEndpoint(svc om.SocialGraphManager) endpoint.Endpoint {
return func(_ context.Context, request interface{}) (interface{}, error) {
req := request.(followRequest)
err := svc.Follow(req.Followed, req.Follower)
res := followResponse{}
if err != nil {
res.Err = err.Error()
}
return res, nil
}
}
func makeUnfollowEndpoint(svc om.SocialGraphManager) endpoint.Endpoint {
return func(_ context.Context, request interface{}) (interface{}, error) {
req := request.(unfollowRequest)
err := svc.Unfollow(req.Followed, req.Follower)
res := unfollowResponse{}
if err != nil {
res.Err = err.Error()
}
return res, nil
}
}
//func wasteCPU() {
// fmt.Println("wasteCPU() here!")
// go func() {
// for {
// if rand.Int() % 8000 == 0 {
// time.Sleep(50 * time.Microsecond)
// }
// }
// }()
//}
func makeGetFollowingEndpoint(svc om.SocialGraphManager) endpoint.Endpoint {
return func(_ context.Context, request interface{}) (interface{}, error) {
req := request.(getByUsernameRequest)
followingMap, err := svc.GetFollowing(req.Username)
res := getFollowingResponse{Following: followingMap}
if err != nil {
res.Err = err.Error()
}
return res, nil
}
}
func makeGetFollowersEndpoint(svc om.SocialGraphManager) endpoint.Endpoint {
return func(_ context.Context, request interface{}) (interface{}, error) {
//wasteCPU()
req := request.(getByUsernameRequest)
followersMap, err := svc.GetFollowers(req.Username)
res := getFollowersResponse{Followers: followersMap}
if err != nil {
res.Err = err.Error()
}
return res, nil
}
}