-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgithub.go
More file actions
47 lines (40 loc) · 797 Bytes
/
Copy pathgithub.go
File metadata and controls
47 lines (40 loc) · 797 Bytes
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
package main
import (
"encoding/json"
"log"
"net/http"
"time"
"github.com/cloudflare/golibs/lrucache"
)
var (
githubCache = lrucache.NewMultiLRUCache(8, 64)
)
func githubAuth(token string) string {
if v, ex := githubCache.Get(token); ex {
if v, ok := v.(string); ok {
return v
}
}
resp, err := http.Get(*githubBase + "/user?access_token=" + token)
if err != nil {
log.Println(err)
return ""
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
githubCache.Set(token, "", time.Now().Add(*githubTTL))
return ""
}
v := &githubUser{}
err = json.NewDecoder(resp.Body).Decode(v)
if err != nil {
log.Println(err)
return ""
}
githubCache.Set(token, v.Login, time.Now().Add(*githubTTL))
return v.Login
}
type githubUser struct {
Login string
Email string
}