-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
85 lines (74 loc) · 1.76 KB
/
main.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
package main
import (
"log"
"net/http"
"os"
"strings"
"time"
"github.com/bluele/gcache"
"github.com/joho/godotenv"
"github.com/octokit/go-octokit/octokit"
)
type gistRes struct {
owner string
version string
}
func main() {
// ignore the error, it may be in the environment already
godotenv.Load()
var auth octokit.AuthMethod
if token, exists := os.LookupEnv("GITHUB_TOKEN"); exists {
auth = octokit.TokenAuth{
AccessToken: token,
}
}
client := octokit.NewClient(auth)
gists := client.Gists()
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
params := strings.Split(r.URL.String(), "/")
if len(params) != 3 {
http.Error(w, "Bad parameters", http.StatusBadRequest)
return
}
id, file := params[1], params[2]
gist, err := getGist(gists, id)
if err != nil {
http.Error(w, "Gist not found", http.StatusNotFound)
return
}
newURL :=
"https://gistcdn.githack.com/" +
gist.owner + "/" + id + "/raw/" + gist.version + "/" + file
w.Header().Set("Access-Control-Allow-Origin", "*")
http.Redirect(w, r, newURL, 301)
})
port := "3030"
_, isNow := os.LookupEnv("NOW")
envPort, portExists := os.LookupEnv("PORT")
if isNow {
port = "443"
} else if portExists {
port = envPort
}
log.Fatal(http.ListenAndServe(":"+port, nil))
}
var cache = gcache.New(50).ARC().Expiration(time.Hour).Build()
func getGist(gists *octokit.GistsService, id string) (gistRes, error) {
cacheVal, _ := cache.Get(id)
if cacheVal != nil {
return cacheVal.(gistRes), nil
}
var result gistRes
gist, res := gists.One(nil, octokit.M{
"gist_id": id,
})
if res.HasError() {
return result, res.Err
}
result = gistRes{
owner: gist.Owner.Login,
version: gist.History[0].Version,
}
cache.Set(id, result)
return result, nil
}