/
main.go
140 lines (127 loc) · 2.62 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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
package main
import (
"github.com/kr/tarutil"
"io"
"log"
"net/http"
"os"
"os/exec"
"path"
"strconv"
"strings"
"time"
)
const (
MaxTarSize = 2 * 1000 * 1000
)
var Q = make(chan *job)
var (
version = capture("go", "version")
distenv = capture("go", "tool", "dist", "env")
)
func main() {
for i := 0; i < 5; i++ {
go worker(i)
}
http.HandleFunc("/info", handleInfo)
http.Handle("/build/", http.StripPrefix("/build/", http.HandlerFunc(handleBuild)))
listen := ":" + os.Getenv("PORT")
if listen == ":" {
listen = ":8000"
}
err := http.ListenAndServe(listen, nil)
if err != nil {
panic(err)
}
}
func handleInfo(w http.ResponseWriter, r *http.Request) {
w.Write(version)
w.Write(distenv)
}
func handleBuild(w http.ResponseWriter, r *http.Request) {
r.ParseForm()
var insecure bool
rawInsecure := r.FormValue("insecure")
if rawInsecure != "" && rawInsecure == "true" {
insecure = true
}
j := &job{
insecure: insecure,
pkg: r.URL.Path,
tar: http.MaxBytesReader(w, r.Body, MaxTarSize),
done: make(chan struct{}),
}
Q <- j
<-j.done
const httpTooLarge = "http: request body too large"
if j.err != nil && j.err.Error() == httpTooLarge {
http.Error(w, httpTooLarge, http.StatusRequestEntityTooLarge)
return
}
if j.err != nil {
log.Println(j.err)
http.Error(w, "unprocessable entity", 422)
w.Write(j.out)
return
}
defer j.bin.Close()
http.ServeContent(w, r, "", time.Time{}, j.bin)
}
func worker(n int) {
gopath := "/tmp/" + strconv.Itoa(n)
for j := range Q {
if err := build(j, gopath); err != nil {
j.err = err
}
j.done <- struct{}{}
}
}
func build(j *job, gopath string) error {
defer os.RemoveAll(gopath)
if err := os.RemoveAll(gopath); err != nil {
return err
}
err := tarutil.ExtractAll(j.tar, gopath, 0)
if err != nil {
return err
}
j.out, err = goget(gopath, j.pkg,j.insecure)
if err != nil {
return err
}
j.bin, err = os.Open(gopath + "/bin/" + path.Base(j.pkg))
return err
}
func goget(gopath, pkg string,insecure bool) ([]byte, error) {
var cmd *exec.Cmd
if insecure {
cmd = exec.Command("go", "get", "-v","-insecure", pkg)
} else {
cmd = exec.Command("go", "get", pkg)
}
for _, env := range os.Environ() {
if strings.Contains(env,"GOPATH=") {
env = "GOPATH="+gopath
}
cmd.Env = append(cmd.Env,env)
}
return cmd.CombinedOutput()
}
type job struct {
tar io.Reader
pkg string
gopath string
bin *os.File
out []byte
err error
insecure bool
done chan struct{}
}
func capture(name string, arg ...string) []byte {
cmd := exec.Command(name, arg...)
out, err := cmd.CombinedOutput()
if err != nil {
panic(err)
}
return out
}