-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
130 lines (119 loc) · 3.35 KB
/
main.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
// A trivial redirector for google.golang.org.
package main
import (
"fmt"
"html/template"
"net/http"
"os"
"strings"
)
var repoMap = map[string]Repo{
"api": {
VCS: "git",
URL: "https://github.com/googleapis/google-api-go-client",
},
"appengine": {
VCS: "git",
URL: "https://github.com/golang/appengine",
},
"cloud": {
// This repo is now at "cloud.google.com/go", but still specifying the repo
// here gives nicer errors in the go tool.
VCS: "git",
URL: "https://github.com/googleapis/google-cloud-go",
},
"genproto": {
VCS: "git",
URL: "https://github.com/googleapis/go-genproto",
},
"grpc": {
VCS: "git",
URL: "https://github.com/grpc/grpc-go",
},
"protobuf": {
VCS: "git",
URL: "https://go.googlesource.com/protobuf",
Src: github("protocolbuffers/protobuf-go"),
},
}
// Repo represents a repository containing Go code.
type Repo struct {
// VCS and URL set the go-import meta-tag,
// as per https://go.dev/ref/mod#vcs-find.
VCS string
URL string
// Src sets additional control over where to
// link to for viewing source code. Optional.
Src *src
}
// src represents a pkg.go.dev source redirect.
// See https://cs.opensource.google/go/x/pkgsite/+/master:internal/source/meta-tags.go;l=19;drc=19794c8aeb90c0a8f17c5ee1ed187bd005a1fd40?q=sourceMeta&ss=go%2Fx%2Fpkgsite.
type src struct {
URL string
DirTpl string
FileTpl string
}
// github returns the *src representing a repo on github.com.
func github(base string) *src {
return &src{
URL: fmt.Sprintf("https://github.com/%s", base),
DirTpl: fmt.Sprintf("https://github.com/%s/tree/master{/dir}", base),
FileTpl: fmt.Sprintf("https://github.com/%s/tree/master{/dir}/{file}#L{line}", base),
}
}
func main() {
http.HandleFunc("/", handler)
port := os.Getenv("PORT")
if port == "" {
port = "8080"
fmt.Printf("Defaulting to port %s\n", port)
}
fmt.Printf("Listening on port %s\n", port)
if err := http.ListenAndServe(fmt.Sprintf(":%s", port), nil); err != nil {
fmt.Fprintf(os.Stderr, "http.ListenAndServe: %v\n", err)
return
}
}
func handler(w http.ResponseWriter, r *http.Request) {
head, tail := strings.TrimPrefix(r.URL.Path, "/"), ""
if i := strings.Index(head, "/"); i != -1 {
head, tail = head[:i], head[i:]
}
if head == "" {
http.Redirect(w, r, "https://cloud.google.com/go/google.golang.org", http.StatusFound)
return
}
repo, ok := repoMap[head]
if !ok {
http.NotFound(w, r)
return
}
docURL := "https://pkg.go.dev/google.golang.org/" + head + tail
// For users visiting in a browser, redirect straight to pkg.go.dev.
if isBrowser := r.FormValue("go-get") == ""; isBrowser {
http.Redirect(w, r, docURL, http.StatusFound)
return
}
data := struct {
Head, DocURL string
Repo Repo
}{head, docURL, repo}
w.Header().Set("Content-Type", "text/html; charset=utf-8")
if err := tmpl.Execute(w, data); err != nil {
fmt.Fprintf(os.Stderr, "tmpl.Execute: %v\n", err)
}
}
var tmpl = template.Must(template.New("redir").Parse(`<!DOCTYPE html>
<html>
<head>
<meta name="go-import" content="google.golang.org/{{.Head}} {{.Repo.VCS}} {{.Repo.URL}}">
{{if .Repo.Src}}
<meta name="go-source" content="google.golang.org/{{.Head}} {{.Repo.Src.URL}} {{.Repo.Src.DirTpl}} {{.Repo.Src.FileTpl}}">
{{end}}
<meta http-equiv="refresh" content="0; url={{.DocURL}}">
</head>
<body>
<a href="{{.DocURL}}">Redirecting to documentation...</a>
</body>
</html>
`))