From 072133c61b61d14f1ce7a866c0bfb741f19e92a0 Mon Sep 17 00:00:00 2001 From: Andrew Gerrand Date: Wed, 18 Sep 2013 15:20:41 +1000 Subject: [PATCH] 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 --- cmd/godoc/blog.go | 68 +++++++++++++++++++++++++++++++++++++++++++ cmd/godoc/handlers.go | 3 +- 2 files changed, 69 insertions(+), 2 deletions(-) create mode 100644 cmd/godoc/blog.go diff --git a/cmd/godoc/blog.go b/cmd/godoc/blog.go new file mode 100644 index 0000000000..779986354b --- /dev/null +++ b/cmd/godoc/blog.go @@ -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 +} diff --git a/cmd/godoc/handlers.go b/cmd/godoc/handlers.go index bb20243766..348b567e0d 100644 --- a/cmd/godoc/handlers.go +++ b/cmd/godoc/handlers.go @@ -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=",