|
@@ -17,6 +17,7 @@ import (
|
17
|
17
|
var (
|
18
|
18
|
owner string
|
19
|
19
|
repo string
|
|
20
|
+ redirect *string
|
20
|
21
|
ctx context.Context
|
21
|
22
|
client *github.Client
|
22
|
23
|
release *github.RepositoryRelease
|
|
@@ -31,7 +32,17 @@ func fetchLatest() {
|
31
|
32
|
release = latest
|
32
|
33
|
}
|
33
|
34
|
|
|
35
|
+func temporaryRedirect(w http.ResponseWriter, url string) {
|
|
36
|
+ w.Header().Add("Location", url)
|
|
37
|
+ w.WriteHeader(http.StatusTemporaryRedirect)
|
|
38
|
+}
|
|
39
|
+
|
34
|
40
|
func serve(w http.ResponseWriter, request *http.Request) {
|
|
41
|
+ if "/" == request.RequestURI && len(*redirect) > 0 {
|
|
42
|
+ temporaryRedirect(w, *redirect)
|
|
43
|
+ return
|
|
44
|
+ }
|
|
45
|
+
|
35
|
46
|
if release == nil {
|
36
|
47
|
w.WriteHeader(http.StatusInternalServerError)
|
37
|
48
|
_, _ = fmt.Fprint(w, "Unknown release")
|
|
@@ -40,8 +51,7 @@ func serve(w http.ResponseWriter, request *http.Request) {
|
40
|
51
|
|
41
|
52
|
for _, asset := range release.Assets {
|
42
|
53
|
if "/" + *asset.Name == request.RequestURI {
|
43
|
|
- w.Header().Add("Location", *asset.BrowserDownloadURL)
|
44
|
|
- w.WriteHeader(http.StatusTemporaryRedirect)
|
|
54
|
+ temporaryRedirect(w, *asset.BrowserDownloadURL)
|
45
|
55
|
return
|
46
|
56
|
}
|
47
|
57
|
}
|
|
@@ -50,20 +60,32 @@ func serve(w http.ResponseWriter, request *http.Request) {
|
50
|
60
|
_, _ = fmt.Fprint(w, "Asset not found in release ", *release.Name)
|
51
|
61
|
}
|
52
|
62
|
|
|
63
|
+func parseRepo(fullRepo *string) error {
|
|
64
|
+ if len(*fullRepo) == 0 {
|
|
65
|
+ return fmt.Errorf("the repository option must be specified")
|
|
66
|
+ }
|
|
67
|
+ if strings.Count(*fullRepo, "/") != 1 {
|
|
68
|
+ return fmt.Errorf("the repository must be specified in `user/repo` format")
|
|
69
|
+ }
|
|
70
|
+ repoParts := strings.Split(*fullRepo, "/")
|
|
71
|
+ owner = repoParts[0]
|
|
72
|
+ repo = repoParts[1]
|
|
73
|
+ return nil
|
|
74
|
+}
|
|
75
|
+
|
53
|
76
|
func main() {
|
54
|
|
- var fullRepo = flag.String("repo", "", "the repository to redirect releases for, in user/repo format")
|
|
77
|
+ redirect = flag.String("redirect", "", "if specified, requests for / will be redirected to this url")
|
|
78
|
+ var fullRepo = flag.String("repo", "", "the repository to redirect releases for, in user/repo format [required]")
|
55
|
79
|
var port = flag.Int("port", 8080, "the port to listen on for HTTP requests")
|
|
80
|
+
|
56
|
81
|
flag.Parse()
|
57
|
82
|
|
58
|
|
- if strings.Count(*fullRepo, "/") != 1 {
|
59
|
|
- log.Println("Repository must be specified in user/repo format")
|
|
83
|
+ if err := parseRepo(fullRepo); err != nil {
|
|
84
|
+ _, _ = fmt.Fprintf(os.Stderr, "Error: %s\n\n", err.Error())
|
|
85
|
+ flag.Usage()
|
60
|
86
|
return
|
61
|
87
|
}
|
62
|
88
|
|
63
|
|
- repoParts := strings.Split(*fullRepo, "/")
|
64
|
|
- owner = repoParts[0]
|
65
|
|
- repo = repoParts[1]
|
66
|
|
-
|
67
|
89
|
client = github.NewClient(nil)
|
68
|
90
|
|
69
|
91
|
ticker := time.NewTicker(time.Hour)
|