Split option processing from serving
git-svn-id: file:///srv/svn/repo/toyohime/trunk@88 922d331f-388e-da47-97a9-ad700dc0b8b9
This commit is contained in:
parent
2524b874df
commit
3e556645fa
32
vanity.go
32
vanity.go
@ -62,15 +62,7 @@ func WithRedirector(redir Redirector) Option {
|
||||
}
|
||||
}
|
||||
|
||||
// Returns an http.Handler that serves the vanity URL information for a single
|
||||
// repository. Each Option gives additional information to agents about the
|
||||
// repository or provides help to browsers that may have navigated to the vanity
|
||||
// URL. The WithImport Option is mandatory since the go tool requires it to
|
||||
// fetch the repository.
|
||||
func Handler(opts ...Option) http.Handler {
|
||||
var redir Redirector
|
||||
|
||||
tpl := func() *template.Template {
|
||||
func compile(opts []Option) (*template.Template, Redirector) {
|
||||
// Process options.
|
||||
var cfg config
|
||||
for _, opt := range opts {
|
||||
@ -101,16 +93,17 @@ Nothing to see here; <a href="{{ . }}">move along</a>.
|
||||
</html>
|
||||
`, tagBlk)
|
||||
|
||||
redir = cfg.redir
|
||||
return template.Must(template.New("").Parse(h))
|
||||
}()
|
||||
|
||||
if redir == nil {
|
||||
redir = func(pkg string) string {
|
||||
// Use default GDDO Redirector.
|
||||
if cfg.redir == nil {
|
||||
cfg.redir = func(pkg string) string {
|
||||
return "https://godoc.org/" + pkg
|
||||
}
|
||||
}
|
||||
|
||||
return template.Must(template.New("").Parse(h)), cfg.redir
|
||||
}
|
||||
|
||||
func handlerFrom(tpl *template.Template, redir Redirector) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
// Redirect to https.
|
||||
if r.URL.Scheme == "http" {
|
||||
@ -142,6 +135,15 @@ Nothing to see here; <a href="{{ . }}">move along</a>.
|
||||
})
|
||||
}
|
||||
|
||||
// Returns an http.Handler that serves the vanity URL information for a single
|
||||
// repository. Each Option gives additional information to agents about the
|
||||
// repository or provides help to browsers that may have navigated to the vanity
|
||||
// URL. The WithImport Option is mandatory since the go tool requires it to
|
||||
// fetch the repository.
|
||||
func Handler(opts ...Option) http.Handler {
|
||||
return handlerFrom(compile(opts))
|
||||
}
|
||||
|
||||
// Helpers for common VCSs.
|
||||
|
||||
// Redirects gddo to browsable source files for GitHub hosted repositories.
|
||||
|
Loading…
x
Reference in New Issue
Block a user