forked from go101/go101
-
Notifications
You must be signed in to change notification settings - Fork 0
/
go-get.go
65 lines (56 loc) · 1.41 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
package main
import (
"bytes"
"net/http"
)
type GoGetInfo struct {
SubPackage, // assume most one-depth sub-packages
RootPackage,
GoGetSourceRepo,
GoDocSourceRepo string
}
var gogetInfos = map[string]GoGetInfo{
"tinyrouter": {
SubPackage: "",
RootPackage: "go101.org/tinyrouter",
GoGetSourceRepo: "https://github.com/go101/tinyrouter",
GoDocSourceRepo: "https://github.com/go101/tinyrouter",
},
"skia": {
SubPackage: "",
RootPackage: "go101.org/skia",
GoGetSourceRepo: "https://github.com/go101/go-skia",
GoDocSourceRepo: "https://github.com/go101/go-skia",
},
}
func (go101 *Go101) ServeGoGetPages(w http.ResponseWriter, r *http.Request, rootPkg, subPkg string) {
info, exists := gogetInfos[rootPkg]
if !exists {
http.Redirect(w, r, "/article/101.html", http.StatusNotFound)
return
}
item := rootPkg
if subPkg != "" {
item += "/" + subPkg
}
page, isLocal := go101.gogetPages.Get(item), go101.IsLocalServer()
if page == nil {
t := retrievePageTemplate(Template_GoGet, !isLocal)
var err error
var buf bytes.Buffer
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=5000") // about 1.5 hours
}
w.Write(page)
}