-
Notifications
You must be signed in to change notification settings - Fork 8
/
web.go
122 lines (103 loc) · 2.59 KB
/
web.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package web
import (
"encoding/xml"
"html/template"
"io/ioutil"
"net/http"
"path"
"runtime/debug"
"github.com/golang/glog"
"golang.org/x/net/websocket"
"github.com/yangsf5/claw/center"
)
const (
TEMPLATE_DIR = "./view"
TEMPLATE_COMMON_DIR = "./view/common"
)
var (
templates = make(map[string] *template.Template)
mux *http.ServeMux
)
func Start() {
if mux == nil {
panic("Claw.Web please register http handler.")
}
cacheTemplate()
type HttpConfig struct {
ListenAddr string `xml:"listenAddr,attr"`
}
type ConfigPack struct {
XMLName xml.Name `xml:"clawconfig"`
Http HttpConfig `xml:"http"`
}
var cfg ConfigPack
center.GetConfig(&cfg)
err := http.ListenAndServe(cfg.Http.ListenAddr, mux)
checkError(err)
}
func RegisterHttpHandler(pattern string, handler func(http.ResponseWriter, *http.Request)) {
if mux == nil {
mux = http.NewServeMux()
}
mux.HandleFunc(pattern, safeHandler(handler))
}
func RegisterWebSocketHandler(pattern string, handler func(*websocket.Conn)) {
if mux == nil {
mux = http.NewServeMux()
}
mux.Handle(pattern, websocket.Handler(handler))
}
func StaticDirHandler(prefix string, staticDir string) {
if mux == nil {
mux = http.NewServeMux()
}
mux.HandleFunc(prefix, func(w http.ResponseWriter, r *http.Request) {
file := staticDir + r.URL.Path[len(prefix)-1:]
http.ServeFile(w, r, file)
})
}
func cacheTemplate() {
fileInfoArr, err := ioutil.ReadDir(TEMPLATE_DIR)
checkError(err)
header := TEMPLATE_COMMON_DIR + "/header.html"
footer := TEMPLATE_COMMON_DIR + "/footer.html"
glog.Infof("Common template [%s %s]", header, footer)
var tplName, tplPath string
for _, fileInfo := range fileInfoArr {
tplName = fileInfo.Name()
if ext := path.Ext(tplName); ext != ".html" {
continue
}
tplPath = TEMPLATE_DIR + "/" + tplName
glog.Infof("Loading template %s", tplPath)
t := template.Must(template.ParseFiles(tplPath, header, footer))
templates[tplPath] = t
}
}
func checkError(err error) {
if err != nil {
panic(err)
}
}
func RenderHtml(w http.ResponseWriter, tmpl string, data interface{}) {
tmpl = TEMPLATE_DIR + "/" + tmpl
tpl, ok := templates[tmpl];
if !ok {
glog.Errorf("Render html, but template is nil, name=%s", tmpl)
return
}
err := tpl.Execute(w, data)
checkError(err)
}
func safeHandler(fn http.HandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
defer func() {
if err, ok := recover().(error); ok {
http.Error(w, err.Error(), http.StatusInternalServerError)
glog.Errorf("WARN: panic in %v - %v", fn, err)
glog.Errorf(string(debug.Stack()))
}
}()
fn(w, r)
}
}