-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathblog.go
58 lines (48 loc) · 1.61 KB
/
blog.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
// 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.
// Command blog is a web server for the Go blog that can run on App Engine or
// as a stand-alone HTTP server.
package main
import (
"net/http"
"strings"
"time"
"github.com/Go-zh/tools/blog"
"github.com/Go-zh/tools/godoc/static"
_ "github.com/Go-zh/tools/playground"
)
const hostname = "blog.go-zh.org" // default hostname for blog server
var config = blog.Config{
Hostname: hostname,
BaseURL: "https://" + hostname,
GodocURL: "https://go-zh.org",
HomeArticles: 5, // articles to display on the home page
FeedArticles: 10, // articles to include in Atom and JSON feeds
PlayEnabled: true,
FeedTitle: "Go 语言博客",
}
func init() {
// Redirect "/blog/" to "/", because the menu bar link is to "/blog/"
// but we're serving from the root.
redirect := func(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, "/", http.StatusFound)
}
http.HandleFunc("/blog", redirect)
http.HandleFunc("/blog/", redirect)
// Keep these static file handlers in sync with app.yaml.
static := http.FileServer(http.Dir("static"))
http.Handle("/favicon.ico", static)
http.Handle("/fonts.css", static)
http.Handle("/fonts/", static)
http.Handle("/lib/godoc/", http.StripPrefix("/lib/godoc/", http.HandlerFunc(staticHandler)))
}
func staticHandler(w http.ResponseWriter, r *http.Request) {
name := r.URL.Path
b, ok := static.Files[name]
if !ok {
http.NotFound(w, r)
return
}
http.ServeContent(w, r, name, time.Time{}, strings.NewReader(b))
}