Bläddra i källkod

Tidying, add redirection option

master
Chris Smith 5 år sedan
förälder
incheckning
b20a16ec40
2 ändrade filer med 48 tillägg och 10 borttagningar
  1. 17
    1
      README.md
  2. 31
    9
      main.go

+ 17
- 1
README.md Visa fil

13
 or using docker:
13
 or using docker:
14
 
14
 
15
 ```console
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
 ## Notes
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
 Releases are refreshed at startup and then once an hour.
37
 Releases are refreshed at startup and then once an hour.
22
 
38
 
23
 Assets are matched on their name, so an asset attached to the latest release
39
 Assets are matched on their name, so an asset attached to the latest release

+ 31
- 9
main.go Visa fil

17
 var (
17
 var (
18
 	owner string
18
 	owner string
19
 	repo string
19
 	repo string
20
+	redirect *string
20
 	ctx context.Context
21
 	ctx context.Context
21
 	client *github.Client
22
 	client *github.Client
22
 	release *github.RepositoryRelease
23
 	release *github.RepositoryRelease
31
 	release = latest
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
 func serve(w http.ResponseWriter, request *http.Request) {
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
 	if release == nil {
46
 	if release == nil {
36
 		w.WriteHeader(http.StatusInternalServerError)
47
 		w.WriteHeader(http.StatusInternalServerError)
37
 		_, _ = fmt.Fprint(w, "Unknown release")
48
 		_, _ = fmt.Fprint(w, "Unknown release")
40
 
51
 
41
 	for _, asset := range release.Assets {
52
 	for _, asset := range release.Assets {
42
 		if "/" + *asset.Name == request.RequestURI {
53
 		if "/" + *asset.Name == request.RequestURI {
43
-			w.Header().Add("Location", *asset.BrowserDownloadURL)
44
-			w.WriteHeader(http.StatusTemporaryRedirect)
54
+			temporaryRedirect(w, *asset.BrowserDownloadURL)
45
 			return
55
 			return
46
 		}
56
 		}
47
 	}
57
 	}
50
 	_, _ = fmt.Fprint(w, "Asset not found in release ", *release.Name)
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
 func main() {
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
 	var port = flag.Int("port", 8080, "the port to listen on for HTTP requests")
79
 	var port = flag.Int("port", 8080, "the port to listen on for HTTP requests")
80
+
56
 	flag.Parse()
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
 		return
86
 		return
61
 	}
87
 	}
62
 
88
 
63
-	repoParts := strings.Split(*fullRepo, "/")
64
-	owner = repoParts[0]
65
-	repo = repoParts[1]
66
-
67
 	client = github.NewClient(nil)
89
 	client = github.NewClient(nil)
68
 
90
 
69
 	ticker := time.NewTicker(time.Hour)
91
 	ticker := time.NewTicker(time.Hour)

Laddar…
Avbryt
Spara