/
pi.go
88 lines (72 loc) · 2.27 KB
/
pi.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package main
import (
"io"
"net/http"
"log"
"os"
"fmt"
"time"
"github.com/bmizerany/pat"
"github.com/hoisie/mustache"
)
// 默认的http端口
var listenPort int = 80
var (
assetsDir string
controllerName string
)
//TODO: 实现一个Controller的Interface,默认从对应的views目录加载index.html.mustache并Write
func Render(t string, context ...interface{}) string {
return mustache.RenderFileInLayout("views/" + controllerName + "/" + t + ".html.mustache",
"views/layout/pi.html.mustache", context)
}
// hello world, the web server
func HelloController(w http.ResponseWriter, r *http.Request) {
controllerName = "hello"
var body string = Render("index", nil)
io.WriteString(w, body)
}
// Serve static files
func AssetsServer(w http.ResponseWriter, r *http.Request) {
filename := string(assetsDir + r.URL.Path[8:])
// fmt.Println(filename)
// Set MIME
if filename[len(filename)-3:] == ".css" {
w.Header().Set("Content-Type", "text/css")
}
// Set expire headers to now + 1 year
yearLater := time.Now().AddDate(1, 0, 0)
w.Header().Set("Expires", yearLater.Format(http.TimeFormat))
http.ServeFile(w, r, filename)
}
// About
func AboutController(w http.ResponseWriter, r *http.Request) {
controllerName = "about"
io.WriteString(w, Render("index", nil))
}
// Contact
func ContactController(w http.ResponseWriter, r *http.Request) {
controllerName = "contact"
io.WriteString(w, Render("index", nil))
}
func main() {
// 静态资源目录
assetsDir = "./public/"
m := pat.New()
m.Get("/", http.HandlerFunc(HelloController))
m.Get("/assets/:file", http.HandlerFunc(AssetsServer))
m.Get("/about", http.HandlerFunc(AboutController))
m.Get("/contact", http.HandlerFunc(ContactController))
// Register this pat with the default serve mux so that other packages
// may also be exported. (i.e. /debug/pprof/*)
http.Handle("/", m)
// 非生产环境http使用7000端口
if os.Getenv("PI_ENV") != "production" {
listenPort = 7000
}
var fullListenParam string = fmt.Sprintf(":%d", listenPort)
err := http.ListenAndServe(fullListenParam, nil)
if err != nil {
log.Fatal("ListenAndServe: ", err)
}
}