Пример #1
0
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 = 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,
	})
	if err != nil {
		log.Fatal(err)
	}
	blogServer = s
}
Пример #2
0
// reloadingBlogServer is an handler that restarts the blog server on each page
// view. Inefficient; don't enable by default. Handy when editing blog content.
func reloadingBlogServer(w http.ResponseWriter, r *http.Request) {
	s, err := blog.NewServer(config)
	if err != nil {
		http.Error(w, err.Error(), 500)
		return
	}
	s.ServeHTTP(w, r)
}
Пример #3
0
func init() {
	config.ContentPath = "content/"
	config.TemplatePath = "template/"
	s, err := blog.NewServer(config)
	if err != nil {
		panic(err)
	}
	http.Handle("/", s)
}
Пример #4
0
func main() {
	flag.Parse()
	config.ContentPath = *contentPath
	config.TemplatePath = *templatePath
	if *reload {
		http.HandleFunc("/", reloadingBlogServer)
	} else {
		s, err := blog.NewServer(config)
		if err != nil {
			log.Fatal(err)
		}
		http.Handle("/", s)
	}
	fs := http.FileServer(http.Dir(*staticPath))
	http.Handle("/static/", http.StripPrefix("/static/", fs))
	log.Fatal(http.ListenAndServe(*httpAddr, nil))
}