2013-09-17 23:20:41 -06:00
|
|
|
// 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"
|
2013-10-03 02:10:57 -06:00
|
|
|
"strings"
|
2013-09-17 23:20:41 -06:00
|
|
|
"sync"
|
|
|
|
|
2014-11-09 14:50:40 -07:00
|
|
|
"golang.org/x/tools/blog"
|
|
|
|
"golang.org/x/tools/godoc/redirect"
|
2013-09-17 23:20:41 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2014-11-09 14:50:40 -07:00
|
|
|
blogRepo = "golang.org/x/blog"
|
2018-08-21 15:39:23 -06:00
|
|
|
blogURL = "https://blog.golang.org/"
|
2013-10-03 02:10:57 -06:00
|
|
|
blogPath = "/blog/"
|
2013-09-17 23:20:41 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
blogServer http.Handler // set by blogInit
|
|
|
|
blogInitOnce sync.Once
|
2013-10-02 22:29:16 -06:00
|
|
|
playEnabled bool
|
2013-09-17 23:20:41 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
// Initialize blog only when first accessed.
|
2013-10-03 02:10:57 -06:00
|
|
|
http.HandleFunc(blogPath, func(w http.ResponseWriter, r *http.Request) {
|
2018-04-08 00:55:29 -06:00
|
|
|
blogInitOnce.Do(func() {
|
|
|
|
blogInit(r.Host)
|
|
|
|
})
|
2013-09-17 23:20:41 -06:00
|
|
|
blogServer.ServeHTTP(w, r)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-04-08 00:55:29 -06:00
|
|
|
func blogInit(host string) {
|
2018-08-21 15:39:23 -06:00
|
|
|
// Binary distributions included the blog content in "/blog".
|
|
|
|
// We stopped including this in Go 1.11.
|
2013-09-17 23:20:41 -06:00
|
|
|
root := filepath.Join(runtime.GOROOT(), "blog")
|
|
|
|
|
2018-08-21 15:39:23 -06:00
|
|
|
// Prefer content from the golang.org/x/blog repository if present.
|
2013-09-17 23:20:41 -06:00
|
|
|
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)
|
2013-10-03 02:10:57 -06:00
|
|
|
blogServer = http.HandlerFunc(blogRedirectHandler)
|
2013-09-17 23:20:41 -06:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
s, err := blog.NewServer(blog.Config{
|
2018-04-08 00:55:29 -06:00
|
|
|
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"),
|
2013-09-17 23:20:41 -06:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
blogServer = s
|
|
|
|
}
|
2013-10-03 02:10:57 -06:00
|
|
|
|
|
|
|
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)
|