-
Notifications
You must be signed in to change notification settings - Fork 0
/
go4.go
154 lines (138 loc) · 5.42 KB
/
go4.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
// Copyright 2021 Brad Fitzpatrick. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Serve go4.org
package main
import (
"fmt"
"io"
"net/http"
"strings"
)
const header = `<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<meta name="go-import" content="go4.org git https://github.com/go4org/go4">
<meta name="go-source" content="go4.org https://github.com/go4org/go4/ https://github.com/go4org/go4/tree/master{/dir} https://github.com/go4org/go4/blob/master{/dir}/{file}#L{line}">
`
const grpcHTML = `<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<meta name="go-import" content="go4.org/grpc git https://github.com/go4org/grpc">
<meta name="go-source" content="go4.org/grpc https://github.com/go4org/grpc https://github.com/go4org/grpc/tree/master{/dir} https://github.com/go4org/grpc/blob/master{/dir}/{file}#L{line}">
`
const grpcGenHTML = `<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<meta name="go-import" content="go4.org/grpc-codegen git https://github.com/go4org/grpc-codegen">
<meta name="go-source" content="go4.org/grpc-codegen https://github.com/go4org/grpc-codegen https://github.com/go4org/grpc-codegen/tree/master{/dir} https://github.com/go4org/grpc-codegen/blob/master{/dir}/{file}#L{line}">
`
const memHTML = `<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<meta name="go-import" content="go4.org/mem git https://github.com/go4org/mem">
<meta name="go-source" content="go4.org/mem https://github.com/go4org/mem https://github.com/go4org/mem/tree/master{/dir} https://github.com/go4org/mem/blob/master{/dir}/{file}#L{line}">
`
const unsafeHTML = `<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<meta name="go-import" content="go4.org/unsafe/assume-no-moving-gc git https://github.com/go4org/unsafe-assume-no-moving-gc">
`
const internHTML = `<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<meta name="go-import" content="go4.org/intern git https://github.com/go4org/intern">
`
const netipxHTML = `<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<meta name="go-import" content="go4.org/netipx git https://github.com/go4org/netipx">
`
func serveGo4(w http.ResponseWriter, req *http.Request) {
if req.URL.Path == "/golang/go-blockchain-support-whitepaper.pdf" {
http.Redirect(w, req, "https://www.youtube.com/watch?v=dQw4w9WgXcQ", http.StatusFound)
return
}
if req.URL.Path == "/" {
io.WriteString(w, header)
io.WriteString(w, `
</head>
<h1>go4.org</h1>
<p>Misc <a href="https://golang.org/">Go</a> packages.</p>
<ul>
<li><a href="https://godoc.org/?q=go4.org">Browse</a></li>
<li><a href="https://github.com/go4org">GitHub org</a></li>
<li><a href="https://github.com/go4org/go4/">About</a></li>
</ul>
<p>
<i>... go4, go four, gopher... get it?</i> Jokes are funnier explained!
</p>
`)
return
}
target := "https://godoc.org/go4.org" + req.URL.Path
if strings.IndexAny(target, " \t\n\r'\"<>&") != -1 {
w.WriteHeader(400)
return
}
switch {
case strings.HasPrefix(req.URL.Path, "/grpc-codegen"):
io.WriteString(w, grpcGenHTML)
case strings.HasPrefix(req.URL.Path, "/grpc"):
io.WriteString(w, grpcHTML)
case strings.HasPrefix(req.URL.Path, "/mem"):
io.WriteString(w, memHTML)
case strings.HasPrefix(req.URL.Path, "/netipx"):
io.WriteString(w, netipxHTML)
case strings.HasPrefix(req.URL.Path, "/intern"):
io.WriteString(w, internHTML)
case strings.HasPrefix(req.URL.Path, "/unsafe"):
if req.Method == "GET" && req.FormValue("go-get") == "1" {
io.WriteString(w, unsafeHTML)
} else if req.URL.Path == "/unsafe/assume-no-moving-gc" {
io.WriteString(w, "<html><head>\n")
writeRedirectHTML(w, "https://pkg.go.dev/go4.org/unsafe/assume-no-moving-gc")
return
}
default:
io.WriteString(w, header)
}
writeRedirectHTML(w, target)
}
func serveGo4GRPC(w http.ResponseWriter, req *http.Request) {
target := "https://godoc.org/grpc.go4.org" + req.URL.Path
if strings.IndexAny(target, " \t\n\r'\"<>&") != -1 {
w.WriteHeader(400)
return
}
io.WriteString(w, `<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<meta name="go-import" content="grpc.go4.org git https://github.com/go4org/grpc">
<meta name="go-source" content="grpc.go4.org https://github.com/go4org/grpc https://github.com/go4org/grpc/tree/master{/dir} https://github.com/go4org/grpc/blob/master{/dir}/{file}#L{line}">
`)
writeRedirectHTML(w, target)
}
func serveGo4GRPCCodegen(w http.ResponseWriter, req *http.Request) {
target := "https://godoc.org/grpc-codegen.go4.org" + req.URL.Path
if strings.IndexAny(target, " \t\n\r'\"<>&") != -1 {
w.WriteHeader(400)
return
}
io.WriteString(w, `<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<meta name="go-import" content="grpc-codegen.go4.org git https://github.com/go4org/grpc-codegen">
<meta name="go-source" content="grpc-codegen.go4.org https://github.com/go4org/grpc-codegen https://github.com/go4org/grpc-codegen/tree/master{/dir} https://github.com/go4org/grpc-codegen/blob/master{/dir}/{file}#L{line}">
`)
writeRedirectHTML(w, target)
}
func writeRedirectHTML(w io.Writer, target string) {
fmt.Fprintf(w, `<meta http-equiv="refresh" content="0; url=%s">
</head>
<body>
See <a href="%s">docs</a>.
</body>
</html>
`, target, target)
}