-
-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathserver.go
228 lines (193 loc) · 5.04 KB
/
server.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
// Package sponsors provides GitHub sponsors management.
package sponsors
import (
"bytes"
"context"
"fmt"
"image"
"image/color"
"image/draw"
"image/png"
"io"
"log"
"net/http"
"strconv"
"strings"
"sync"
"time"
"github.com/shurcooL/githubv4"
)
// pixel is a png used for missing avatars.
var pixel []byte
// initialize gray pixel for missing avatar responses.
func init() {
var buf bytes.Buffer
r := image.Rect(0, 0, 1, 1)
img := image.NewRGBA(r)
c := color.RGBA{0xF8, 0xF9, 0xFA, 0xFF}
draw.Draw(img, r, &image.Uniform{c}, image.ZP, draw.Src)
png.Encode(&buf, img)
pixel = buf.Bytes()
}
// Sponsor model.
type Sponsor struct {
// Name of the sponsor.
Name string
// Login name of the sponsor.
Login string
// AvatarURL of the sponsor.
AvatarURL string
}
// Server manager.
type Server struct {
// URL is the url of the server.
URL string
// Client is the github client.
Client *githubv4.Client
// CacheTTL is the duration until the cache expires.
CacheTTL time.Duration
// cache
mu sync.Mutex
cacheTimestamp time.Time
cache []Sponsor
}
// ServeHTTP implementation.
func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
path := r.URL.Path
// logging
start := time.Now()
log.Printf("%s %s", r.Method, path)
defer func() {
log.Printf("%s %s -> %s", r.Method, path, time.Since(start))
}()
// prime cache
err := s.primeCache(ctx)
if err != nil {
log.Printf("error priming cache: %s", err)
http.Error(w, "Error fetching sponsors", http.StatusInternalServerError)
return
}
// routing
switch {
case strings.HasPrefix(path, "/sponsor/markdown"):
s.serveMarkdown(w, r)
case strings.HasPrefix(path, "/sponsor/avatar"):
s.serveAvatar(w, r)
case strings.HasPrefix(path, "/sponsor/profile"):
s.serveProfile(w, r)
default:
http.Error(w, "Not Found", http.StatusNotImplemented)
}
}
// serveMarkdown serves a list of markdown links which you can copy/paste into your Readme.
func (s *Server) serveMarkdown(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/markdown")
for i := 0; i < 100; i++ {
fmt.Fprintf(w, `[<img src="%s/sponsor/avatar/%d" width="35">](%s/sponsor/profile/%d)`, s.URL, i, s.URL, i)
fmt.Fprintf(w, "\n")
}
}
// serveAvatar redirects to a sponsor's avatar image.
func (s *Server) serveAvatar(w http.ResponseWriter, r *http.Request) {
// /sponsor/avatar/{index}
index := strings.Replace(r.URL.Path, "/sponsor/avatar/", "", 1)
n, err := strconv.Atoi(index)
if err != nil {
log.Printf("error parsing index: %s", err)
http.Error(w, "Sponsor index must be a number", http.StatusBadRequest)
return
}
// check index bounds
if n > len(s.cache)-1 {
w.Header().Set("Content-Type", "image/png")
io.Copy(w, bytes.NewReader(pixel))
return
}
// redirect to avatar
sponsor := s.cache[n]
w.Header().Set("Location", sponsor.AvatarURL)
w.WriteHeader(http.StatusTemporaryRedirect)
fmt.Fprintf(w, "Redirecting to %s", sponsor.AvatarURL)
}
// serveProfile redirects to a sponsor's profile.
func (s *Server) serveProfile(w http.ResponseWriter, r *http.Request) {
// /sponsor/profile/{index}
index := strings.Replace(r.URL.Path, "/sponsor/profile/", "", 1)
n, err := strconv.Atoi(index)
if err != nil {
log.Printf("error parsing index: %s", err)
http.Error(w, "Sponsor index must be a number", http.StatusBadRequest)
return
}
// check index bounds
if n > len(s.cache)-1 {
http.Error(w, "Not found", http.StatusNotFound)
return
}
// redirect to profile
sponsor := s.cache[n]
url := fmt.Sprintf("https://github.com/%s", sponsor.Login)
w.Header().Set("Location", url)
w.WriteHeader(http.StatusTemporaryRedirect)
fmt.Fprintf(w, "Redirecting to %s", url)
}
// primeCache implementation.
func (s *Server) primeCache(ctx context.Context) error {
s.mu.Lock()
defer s.mu.Unlock()
// check ttl
if time.Since(s.cacheTimestamp) <= s.CacheTTL {
return nil
}
// fetch
log.Printf("cache miss, fetching sponsors")
sponsors, err := s.getSponsors(ctx)
if err != nil {
return err
}
s.cache = sponsors
s.cacheTimestamp = time.Now()
return nil
}
// getSponsors implementation.
func (s *Server) getSponsors(ctx context.Context) ([]Sponsor, error) {
var sponsors []Sponsor
var q sponsorships
var cursor string
for {
err := s.Client.Query(ctx, &q, map[string]interface{}{
"cursor": githubv4.String(cursor),
})
if err != nil {
return nil, err
}
for _, edge := range q.Viewer.SponsorshipsAsMaintainer.Edges {
sponsor := edge.Node.Sponsor
sponsors = append(sponsors, sponsor)
}
if !q.Viewer.SponsorshipsAsMaintainer.PageInfo.HasNextPage {
break
}
cursor = q.Viewer.SponsorshipsAsMaintainer.PageInfo.EndCursor
}
return sponsors, nil
}
// sponsorships query.
type sponsorships struct {
Viewer struct {
Login string
SponsorshipsAsMaintainer struct {
PageInfo struct {
EndCursor string
HasNextPage bool
}
Edges []struct {
Node struct {
Sponsor Sponsor
}
Cursor string
}
} `graphql:"sponsorshipsAsMaintainer(first: 100, after: $cursor)"`
}
}