This repository has been archived by the owner on Jul 16, 2019. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun.go
60 lines (47 loc) · 1.5 KB
/
run.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
package main
import (
"net/http"
"strings"
"github.com/Luzifer/gh-private-dl/privatehub"
http_helper "github.com/Luzifer/go_helpers/http"
log "github.com/Sirupsen/logrus"
"github.com/gorilla/mux"
sparta "github.com/mweagle/Sparta"
"github.com/spf13/cobra"
)
var executionPort string
func init() {
cmd := &cobra.Command{
Use: "run",
Short: "Run an HTTP server to serve this locally",
RunE: runLocally,
}
cmd.Flags().StringVar(&executionPort, "listen", ":3000", "IP/Port to listen on")
sparta.CommandLineOptions.Root.AddCommand(cmd)
}
func runLocally(cmd *cobra.Command, args []string) error {
r := mux.NewRouter()
r.HandleFunc("/{user}/{repo}/releases/download/{version}/{binary}", handleLocalExecution)
r.HandleFunc("/status", func(res http.ResponseWriter, r *http.Request) { res.WriteHeader(http.StatusOK) })
log.Printf("Starting local webserver on %s", executionPort)
return http.ListenAndServe(executionPort, http_helper.NewHTTPLogHandler(r))
}
func handleLocalExecution(res http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
_, githubToken, ok := r.BasicAuth()
if !ok || githubToken == "" {
http.Error(res, "You need to provide HTTP basic auth", http.StatusBadRequest)
return
}
dlURL, err := privatehub.GetDownloadURL(
strings.Join([]string{vars["user"], vars["repo"]}, "/"),
vars["version"],
vars["binary"],
githubToken,
)
if err != nil {
http.Error(res, err.Error(), http.StatusInternalServerError)
return
}
http.Redirect(res, r, dlURL, http.StatusFound)
}