forked from go101/go101
-
Notifications
You must be signed in to change notification settings - Fork 3
/
go-get.go
138 lines (127 loc) · 3.14 KB
/
go-get.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
package main
import (
"bytes"
"net/http"
"strings"
)
type GoGetInfo struct {
RootPackage,
GoGetSourceRepo, // only supports github now
GoDocWebsite string
}
// ToDo: retire the SubPackage field.
var gogetInfos = map[string]GoGetInfo{
"tinyrouter": {
RootPackage: "go101.org/tinyrouter",
GoGetSourceRepo: "go101/tinyrouter",
GoDocWebsite: "https://pkg.go.dev/",
},
"skia": {
RootPackage: "go101.org/skia",
GoGetSourceRepo: "go101/go-skia",
GoDocWebsite: "https://pkg.go.dev/",
},
"go101": {
RootPackage: "go101.org/go101",
GoGetSourceRepo: "go101/go101",
},
"golang101": {
RootPackage: "go101.org/golang101",
GoGetSourceRepo: "golang101/golang101",
},
"gold": {
RootPackage: "go101.org/gold",
GoGetSourceRepo: "go101/gold",
GoDocWebsite: "https://pkg.go.dev/",
},
"golds": {
RootPackage: "go101.org/golds",
GoGetSourceRepo: "go101/golds",
GoDocWebsite: "https://pkg.go.dev/",
},
"ebooktool": {
RootPackage: "go101.org/ebooktool",
GoGetSourceRepo: "go101/ebooktool",
GoDocWebsite: "https://pkg.go.dev/",
},
"nstd": {
RootPackage: "go101.org/nstd",
GoGetSourceRepo: "go101/nstd",
GoDocWebsite: "https://pkg.go.dev/",
},
"gotv": {
RootPackage: "go101.org/gotv",
GoGetSourceRepo: "go101/gotv",
GoDocWebsite: "https://pkg.go.dev/",
},
"godev": {
RootPackage: "go101.org/godev",
GoGetSourceRepo: "go101/godev",
GoDocWebsite: "https://pkg.go.dev/",
},
}
func (go101 *Go101) ServeGoGetPages(w http.ResponseWriter, r *http.Request, rootPkg, subPkg string) {
var version string
if subPkg != "" {
atIndex := strings.IndexByte(subPkg, '@')
if atIndex >= 0 {
subPkg = subPkg[:atIndex]
version = subPkg[atIndex:]
}
} else {
atIndex := strings.IndexByte(rootPkg, '@')
if atIndex > 0 {
version = rootPkg[atIndex:]
rootPkg = rootPkg[:atIndex]
}
}
// simple handling for pkg.go.dev
if len(version) < 3 || version[1] != 'v' || version[2] < '0' || version[2] > '9' {
version = ""
}
info, exists := gogetInfos[rootPkg]
if !exists {
if subPkg == "" {
if rootPkg == "" {
rootPkg = "index.html"
}
go101.serveGroupItem(w, r, "website", rootPkg)
} else {
http.Redirect(w, r, "/", http.StatusNotFound)
}
return
}
item := rootPkg
if subPkg != "" {
item += "/" + subPkg
}
page, isLocal := go101.gogetPages.Get(item, version), go101.IsLocalServer()
if page == nil {
info.GoGetSourceRepo = "https://github.com/" + info.GoGetSourceRepo
if info.GoDocWebsite != "" {
info.GoDocWebsite += info.RootPackage + "/" + subPkg + version
} else {
info.GoDocWebsite = info.GoGetSourceRepo
if subPkg != "" {
info.GoDocWebsite += "/tree/master/" + subPkg
}
}
var err error
var buf bytes.Buffer
t := retrievePageTemplate(Template_GoGet, !isLocal)
if err = t.Execute(&buf, &info); err == nil {
page = buf.Bytes()
} else {
page = []byte(err.Error())
}
if !isLocal {
go101.gogetPages.Set(item, version, page)
}
}
if isLocal {
w.Header().Set("Cache-Control", "no-cache, private, max-age=0")
} else {
w.Header().Set("Cache-Control", "max-age=50000") // about 14 hours
}
w.Write(page)
}