-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathgomods.go
91 lines (85 loc) · 1.78 KB
/
gomods.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
package gomods
import (
"encoding/json"
"fmt"
"io"
"net/http"
"os"
"regexp"
"strings"
)
var gomodsRegex = regexp.MustCompile("(list|info|mod|zip)")
var modVersionRegex = regexp.MustCompile("(.*)\\.(info|mod|zip)")
// DefaultGoBinaryPath is the default Golang binary installed on the machine
var DefaultGoBinaryPath = os.Getenv("GOROOT") + "/bin/go"
// Serve handles the incoming requests and serves the module files like .mod and etc
func (conf *Config) Serve(w http.ResponseWriter, r *http.Request) error {
m := Module{}
if err := m.ParseImportPath(r.URL.Path); err != nil {
return fmt.Errorf("module url is empty")
}
dp, err := m.fetch(r, *conf)
if err != nil {
return err
}
switch m.FileExt {
case "list":
list, err := dp.List(r.Context(), m.Name)
if err != nil {
return err
}
_, err = w.Write([]byte(strings.Join(list, "\n")))
if err != nil {
return err
}
return nil
case "info":
info, err := dp.Info(r.Context(), m.Name, m.Version)
if err != nil {
return err
}
_, err = w.Write(info)
if err != nil {
return err
}
return nil
case "mod":
mod, err := dp.GoMod(r.Context(), m.Name, m.Version)
if err != nil {
return err
}
_, err = w.Write(mod)
if err != nil {
return err
}
return nil
case "zip":
zip, err := dp.Zip(r.Context(), m.Name, m.Version)
if err != nil {
return err
}
defer zip.Close()
w.Write([]byte{})
_, err = io.Copy(w, zip)
if err != nil {
return err
}
return nil
case "latest":
info, err := dp.Latest(r.Context(), m.Name)
if err != nil {
return err
}
json, err := json.Marshal(info)
if err != nil {
return err
}
_, err = w.Write(json)
if err != nil {
return err
}
return nil
default:
return fmt.Errorf("the requested file's extension is not supported")
}
}