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=",