1
0
mirror of https://github.com/golang/go synced 2024-11-05 15:06:09 -07:00

go.tools/cmd/godoc: include the blog server

Defer parsing of blog content until accessed for faster startup.
Fall back on redirect if blog content unavailable locally.

R=golang-dev, dsymonds
CC=golang-dev
https://golang.org/cl/13335052
This commit is contained in:
Andrew Gerrand 2013-09-18 15:20:41 +10:00
parent 371fdaacb9
commit 072133c61b
2 changed files with 69 additions and 2 deletions

68
cmd/godoc/blog.go Normal file
View File

@ -0,0 +1,68 @@
// 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"
"sync"
"code.google.com/p/go.tools/godoc/blog"
)
const (
blogRedirect = false
blogRepo = "code.google.com/p/go.blog"
blogURL = "http://blog.golang.org/"
)
var (
blogServer http.Handler // set by blogInit
blogInitOnce sync.Once
)
func init() {
// Initialize blog only when first accessed.
http.HandleFunc("/blog/", func(w http.ResponseWriter, r *http.Request) {
blogInitOnce.Do(blogInit)
blogServer.ServeHTTP(w, r)
})
}
func blogInit() {
// 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 = makePrefixRedirectHandler("/blog/", blogURL)
return
}
s, err := blog.NewServer(blog.Config{
BaseURL: "/blog/",
BasePath: "/blog",
ContentPath: filepath.Join(root, "content"),
TemplatePath: filepath.Join(root, "template"),
HomeArticles: 5,
PlayEnabled: *showPlayground,
})
if err != nil {
log.Fatal(err)
}
blogServer = s
}

View File

@ -126,7 +126,7 @@ var cmdRedirects = map[string]string{
}
var redirects = map[string]string{
"/blog": "http://blog.golang.org",
"/blog": "/blog/",
"/build": "http://build.golang.org",
"/change": "https://code.google.com/p/go/source/list",
"/cl": "https://gocodereview.appspot.com/",
@ -163,7 +163,6 @@ var redirects = map[string]string{
}
var prefixHelpers = map[string]string{
"blog": "http://blog.golang.org/",
"change": "https://code.google.com/p/go/source/detail?r=",
"cl": "https://codereview.appspot.com/",
"issue": "https://code.google.com/p/go/issues/detail?id=",