mirror of
https://github.com/golang/go
synced 2024-11-19 00:24:41 -07:00
d11f6ec946
Currently, links inside blog articles are absolute links to golang.org. But when a godoc server is run locally, the blog package should serve local links pointing to the local godoc server. It is not possible to simply change the links inside the blog templates to relative urls because the blog articles are independant pages on their own. And moreover, they are served from blog.golang.org. Rather, the blog package consumes and serves blog articles. So, a flag was added in the Config struct to denote whether to convert the links or not. This flag is then set from the call site in godoc package where the blog server is initialized from. This was required because "golang.org/x/tools/blog" is a package which can be used by other code to serve blog pages and not just godoc. This preserves existing functionality for all working code which imports "golang.org/x/tools/blog" and changes the functionality only when a godoc server is run locally. And while here, replace relevant bytes.Buffer occurences with strings.Builder. Fixes golang/go#22681 Change-Id: I7dbf9c5f2f93fd0b7e17915238de1c084fcd1431 Reviewed-on: https://go-review.googlesource.com/105835 Reviewed-by: Andrew Bonventre <andybons@golang.org> Run-TryBot: Andrew Bonventre <andybons@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org>
85 lines
2.0 KiB
Go
85 lines
2.0 KiB
Go
// Copyright 2013 The Go Authors. All rights reserved.
|
|
// Use of this source code is governed by a BSD-style
|
|
// license that can be found in the LICENSE file.
|
|
|
|
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"go/build"
|
|
"log"
|
|
"net/http"
|
|
"os"
|
|
"path/filepath"
|
|
"runtime"
|
|
"strings"
|
|
"sync"
|
|
|
|
"golang.org/x/tools/blog"
|
|
"golang.org/x/tools/godoc/redirect"
|
|
)
|
|
|
|
const (
|
|
blogRepo = "golang.org/x/blog"
|
|
blogURL = "http://blog.golang.org/"
|
|
blogPath = "/blog/"
|
|
)
|
|
|
|
var (
|
|
blogServer http.Handler // set by blogInit
|
|
blogInitOnce sync.Once
|
|
playEnabled bool
|
|
)
|
|
|
|
func init() {
|
|
// Initialize blog only when first accessed.
|
|
http.HandleFunc(blogPath, func(w http.ResponseWriter, r *http.Request) {
|
|
blogInitOnce.Do(func() {
|
|
blogInit(r.Host)
|
|
})
|
|
blogServer.ServeHTTP(w, r)
|
|
})
|
|
}
|
|
|
|
func blogInit(host string) {
|
|
// Binary distributions will include the blog content in "/blog".
|
|
root := filepath.Join(runtime.GOROOT(), "blog")
|
|
|
|
// Prefer content from go.blog repository if present.
|
|
if pkg, err := build.Import(blogRepo, "", build.FindOnly); err == nil {
|
|
root = pkg.Dir
|
|
}
|
|
|
|
// If content is not available fall back to redirect.
|
|
if fi, err := os.Stat(root); err != nil || !fi.IsDir() {
|
|
fmt.Fprintf(os.Stderr, "Blog content not available locally. "+
|
|
"To install, run \n\tgo get %v\n", blogRepo)
|
|
blogServer = http.HandlerFunc(blogRedirectHandler)
|
|
return
|
|
}
|
|
|
|
s, err := blog.NewServer(blog.Config{
|
|
BaseURL: blogPath,
|
|
BasePath: strings.TrimSuffix(blogPath, "/"),
|
|
ContentPath: filepath.Join(root, "content"),
|
|
TemplatePath: filepath.Join(root, "template"),
|
|
HomeArticles: 5,
|
|
PlayEnabled: playEnabled,
|
|
ServeLocalLinks: strings.HasPrefix(host, "localhost"),
|
|
})
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
blogServer = s
|
|
}
|
|
|
|
func blogRedirectHandler(w http.ResponseWriter, r *http.Request) {
|
|
if r.URL.Path == blogPath {
|
|
http.Redirect(w, r, blogURL, http.StatusFound)
|
|
return
|
|
}
|
|
blogPrefixHandler.ServeHTTP(w, r)
|
|
}
|
|
|
|
var blogPrefixHandler = redirect.PrefixHandler(blogPath, blogURL)
|