Browse Source

Tidying, add redirection option

master
Chris Smith 5 years ago
parent
commit
b20a16ec40
2 changed files with 48 additions and 10 deletions
  1. 17
    1
      README.md
  2. 31
    9
      main.go

+ 17
- 1
README.md View File

@@ -13,11 +13,27 @@ $ github-release-redirector -port 1234 -repo user/repo
13 13
 or using docker:
14 14
 
15 15
 ```console
16
-$ docker run -p 8080 csmith/github-release-redirector -repo user/repo
16
+$ docker run -p 8080:8080 csmith/github-release-redirector -repo user/repo
17
+```
18
+
19
+## Options
20
+
21
+```
22
+Usage of github-release-redirector:
23
+  -port int
24
+    	the port to listen on for HTTP requests (default 8080)
25
+  -redirect string
26
+    	if specified, requests for / will be redirected to this url
27
+  -repo string
28
+    	the repository to redirect releases for, in user/repo format [required]
17 29
 ```
18 30
 
19 31
 ## Notes
20 32
 
33
+If the `-redirect` option is specified, then requests to the root (i.e. `/`)
34
+will be redirected to that URL, and no further processing will be done for
35
+that request.
36
+
21 37
 Releases are refreshed at startup and then once an hour.
22 38
 
23 39
 Assets are matched on their name, so an asset attached to the latest release

+ 31
- 9
main.go View File

@@ -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)

Loading…
Cancel
Save