-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler.go
90 lines (79 loc) · 2.42 KB
/
handler.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
package vanity
import (
"html/template"
"net/http"
"strings"
)
// MetaTemplateLine is the html->head->meta line that must exist in vanity
// page html template.
const MetaTemplateLine = `<meta name="go-import" content="{{.Prefix}}{{.Path}} {{.VCS}} {{.URL}}">`
// PageTmpl is the html template used to render the vanity pages.
//
// It's exported so users of this package could replace it to a different one if
// they so desire. But when replacing it, MetaTemplateLine (or equivalent)
// must exists in the template, or the vanity page might be invalid.
//
// The data used to execute the template is defined in PageTmplData.
var PageTmpl Templater = template.Must(template.New("vanity").Parse(`<!DOCTYPE html>
<html>
<head>
<title>Vanity page for {{.Prefix}}{{.Reqpath}}</title>
` + MetaTemplateLine + `
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<meta http-equiv="refresh" content="0; url=https://pkg.go.dev/{{.Prefix}}{{.Reqpath}}">
</head>
<body><p>
Nothing to see here, <a href="https://pkg.go.dev/{{.Prefix}}{{.Reqpath}}">move along</a>.
</p></body>
</html>
`))
// PageTmplData is the data used to execute PageTmpl.
type PageTmplData struct {
// From Config.Prefix.
Prefix string
// From Config.Mappings
Path string
URL string
VCS VCS
// The path from the request,
// could be different from Path but always have Path as prefix.
Reqpath string
}
// Args defines the args used by Handler function.
type Args struct {
Config Config
// If set to true, do not render and handle the index page.
NoIndex bool
}
// Handler creates an HTTP handler that handles go vanity URL requests.
func Handler(args Args) http.HandlerFunc {
for i := range args.Config.Mappings {
if !strings.HasPrefix(args.Config.Mappings[i].Path, "/") {
// Common mistake, just append the trailing "/" for them.
args.Config.Mappings[i].Path = "/" + args.Config.Mappings[i].Path
}
}
indexHandler := IndexHandler(args)
return func(w http.ResponseWriter, r *http.Request) {
if !args.NoIndex && r.URL.Path == "/" {
indexHandler(w, r)
return
}
for _, m := range args.Config.Mappings {
if r.URL.Path != m.Path && !strings.HasPrefix(r.URL.Path, m.Path+"/") {
continue
}
data := PageTmplData{
Prefix: args.Config.Prefix,
Path: m.Path,
URL: m.URL,
VCS: m.VCS,
Reqpath: r.URL.Path,
}
PageTmpl.Execute(w, data)
return
}
// No mapping found, return 404.
http.NotFound(w, r)
}
}