forked from go101/go101
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgo-get.go
113 lines (102 loc) · 2.53 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
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/",
},
}
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 {
http.Redirect(w, r, "/article/101.html", http.StatusNotFound)
return
}
item := rootPkg
if subPkg != "" {
item += "/" + subPkg + version
} else {
item += version
}
page, isLocal := go101.gogetPages.Get(item), 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.articlePages.Set(item, 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)
}