-
Notifications
You must be signed in to change notification settings - Fork 6
/
vanity.go
283 lines (256 loc) · 6.88 KB
/
vanity.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
package vanity
import (
"errors"
"fmt"
"log"
"net/http"
"os"
"strings"
)
type (
handler struct {
log Logger
vcs string
vcsURL string
host string
moduleServerURL string
static *staticDir
indexPageHandler http.Handler
robotsTxt string
}
staticDir struct {
uRLPath string
path string
fs http.Handler
}
// Option represents a functional option for configuring the vanity middleware.
Option func(http.Handler) error
// Logger describes functions available for logging purposes.
Logger interface {
Printf(format string, v ...interface{})
}
)
const (
// mPkgGoDev is the default module server by Google.
mPkgGoDev = "https://pkg.go.dev/"
// mGitHub is not an actual module server, but a source code repository.
mGitHub = "https://github.com/"
)
// DefaultIndexPageHandler serves given indexFilePath over HTTP via http.ServeFile(w, r, name).
func DefaultIndexPageHandler(indexFilePath string) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, indexFilePath)
})
}
func (h *handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet {
status := http.StatusMethodNotAllowed
http.Error(w, http.StatusText(status), status)
return
}
if h.static != nil && strings.HasPrefix(r.URL.Path, h.static.uRLPath) {
h.static.fs.ServeHTTP(w, r)
return
}
if r.URL.Path == "/" || r.URL.Path == "" {
if h.indexPageHandler != nil {
h.indexPageHandler.ServeHTTP(w, r)
return
}
DefaultIndexPageHandler(h.static.path+"/index.html").ServeHTTP(w, r)
}
if r.URL.Path == "/robots.txt" {
_, _ = fmt.Fprintf(w, "%s", h.robotsTxt)
return
}
host := r.Host
if h.host != "" {
host = h.host
}
// Respond to Go tool with vcs info meta tag
if r.FormValue("go-get") == "1" {
path := r.URL.Path
const cmd = "/cmd/"
if strings.HasPrefix(r.URL.Path, cmd) {
path = path[len(cmd):]
}
vcsroot := h.vcsURL
shortPath := pathComponents(path)
stripSubPackagesFromPath := len(shortPath) > 0
if stripSubPackagesFromPath {
vcsroot = h.vcsURL + shortPath[0]
}
importRoot := strings.TrimSuffix(host+r.URL.Path, "/")
metaTag := fmt.Sprintf(`<meta name="go-import" content="%v %v %v">`, importRoot, h.vcs, vcsroot)
if _, err := w.Write([]byte(metaTag)); err != nil {
h.log.Printf("vanity: i/o error writing go tool http response: %v", err)
}
return
}
// Redirect browsers to Go module site.
url := h.browserURL(host, r.URL.Path)
http.Redirect(w, r, url, http.StatusTemporaryRedirect)
}
func pathComponents(path string) []string {
f := func(c rune) bool {
return c == '/'
}
return strings.FieldsFunc(path, f)
}
func (h *handler) browserURL(host, path string) string {
// host = kkn.fi
// path = /foo/bar
if strings.HasPrefix(h.moduleServerURL, mGitHub) {
pkg := path
components := pathComponents(pkg)
return stripSuffixSlash(h.moduleServerURL) + "/" + components[len(components)-1]
}
switch h.moduleServerURL {
case mPkgGoDev:
fallthrough
default:
pkg := path
return fmt.Sprintf("%v%v%v", mPkgGoDev, host, pkg)
}
}
func stripSuffixSlash(s string) string {
if strings.HasSuffix(s, "/") {
return s[0 : len(s)-1]
}
return s
}
// Handler is an HTTP middleware that redirects browsers to Go module server
// (pkg.go.dev or similar) or Go cmd line tool to VCS repository. Handler can
// be configured by providing options. VCS repository is git by default. VCS
// can be set with VCS(). Configurable Logger defaults to os.Stderr. Logger can
// be configured with SetLogger(). Module server URL is https://pkg.go.dev/ and
// it can be configured via ModuleServerURL() func. VCSURL() func must be used
// to set VCS repository URL (such as https://github.com/kare/).
func NewHandlerWithOptions(opts ...Option) (http.Handler, error) {
v := &handler{
log: log.New(os.Stderr, "", log.LstdFlags),
vcs: "git",
moduleServerURL: mPkgGoDev,
}
for _, option := range opts {
if err := option(v); err != nil {
return nil, err
}
}
return v, nil
}
// VCS sets the version control type.
func VCS(vcs string) Option {
return func(h http.Handler) error {
v := h.(*handler)
v.vcs = vcs
return nil
}
}
func addSuffixSlash(s string) string {
if strings.HasSuffix(s, "/") {
return s
}
return s + "/"
}
// VCSURL sets the VCS repository url address.
func VCSURL(vcsURL string) Option {
return func(h http.Handler) error {
v := h.(*handler)
v.vcsURL = addSuffixSlash(vcsURL)
return nil
}
}
// Host sets the hostname of the vanity server. Host defaults to HTTP request
// hostname.
func Host(host string) Option {
return func(h http.Handler) error {
v := h.(*handler)
v.host = host
return nil
}
}
// Log sets the logger used by vanity package's error logger.
func Log(l Logger) Option {
return func(h http.Handler) error {
v := h.(*handler)
v.log = l
return nil
}
}
// ModuleServerURL sets Go module server address for browser redirect.
func ModuleServerURL(moduleServerURL string) Option {
return func(h http.Handler) error {
v := h.(*handler)
v.moduleServerURL = addSuffixSlash(moduleServerURL)
return nil
}
}
var ErrNotReadable = errors.New("vanity: static dir path directory is not readable")
// StaticDir serves a file system directory over HTTP. Given path is the local
// file system path to directory. Given urlPath is the path portion of the URL for the server.
func StaticDir(path, URLPath string) Option {
return func(h http.Handler) error {
v := h.(*handler)
info, err := os.Stat(path)
if err != nil {
return fmt.Errorf("vanity: static dir path stat error: %w", err)
}
if !info.IsDir() {
return errors.New("vanity: static dir path is not a directory")
}
const (
read = 4
// write = 2
// exec = 1
)
const (
readUser = read << 6
readGroup = read << 3
readOther = read << 0
)
if info.Mode().Perm()&readOther == 0 {
return ErrNotReadable
}
if info.Mode().Perm()&readGroup == 0 {
return ErrNotReadable
}
if info.Mode().Perm()&readUser == 0 {
return ErrNotReadable
}
dir := http.Dir(path)
server := http.FileServer(dir)
v.static = &staticDir{
path: path,
uRLPath: URLPath,
fs: http.StripPrefix(URLPath, server),
}
return nil
}
}
// IndexPageHandler sets a handler for index.html page.
func IndexPageHandler(index http.Handler) Option {
return func(h http.Handler) error {
v := h.(*handler)
v.indexPageHandler = index
return nil
}
}
// DefaultRobotsTxt is the default value for /robots.txt file.
var DefaultRobotsTxt = `user-agent: *
Allow: /$
Allow: /.static/*$
Disallow: /`
// RobotsTxt takes in a value for robots.txt. If value is empty, the value of
// `DefaultRobotsTxt` is used
func RobotsTxt(robotsTxt string) Option {
return func(h http.Handler) error {
v := h.(*handler)
if robotsTxt != "" {
v.robotsTxt = robotsTxt
} else {
v.robotsTxt = DefaultRobotsTxt
}
return nil
}
}