-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
120 lines (104 loc) · 3.7 KB
/
main.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
package main
import (
"bytes"
"encoding/json"
"html/template"
"log"
"net/http"
"strconv"
"github.com/bslatkin/template-perf/static"
"github.com/daaku/go.httpgzip"
)
type Data struct {
Name string `json:"name"`
Color string `json:"color"`
Sex string `json:"sex"`
Legs int `json:"legs"`
}
//go:generate go run generate_names.go
var (
domTemplate = template.Must(template.New("static").Parse(static.Files["dom_render.tpl"]))
fragmentDivsTemplate = template.Must(template.New("static").Parse(static.Files["fragment_divs_render.tpl"]))
rafTemplate = template.Must(template.New("static").Parse(static.Files["raf_render.tpl"]))
templateTagTemplate = template.Must(template.New("static").Parse(static.Files["template_tag_render.tpl"]))
serverTemplate = template.Must(template.New("dynamic").Parse(static.Files["server_render.tpl"]))
serverDivsTemplate = template.Must(template.New("dynamic").Parse(static.Files["server_divs_render.tpl"]))
)
func dataForTemplate(data []Data) template.JS {
encoded, err := json.Marshal(data)
if err != nil {
panic(err)
}
var buffer bytes.Buffer
json.HTMLEscape(&buffer, encoded)
return template.JS(buffer.Bytes())
}
func getCatsFromParam(r *http.Request) []Data {
count, err := strconv.Atoi(r.FormValue("count"))
if err != nil {
count = 1000
}
return getCats(count)
}
func domRenderHandler(w http.ResponseWriter, r *http.Request) {
cats := getCatsFromParam(r)
if err := domTemplate.Execute(w, dataForTemplate(cats)); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}
func fragmentDivsRenderHandler(w http.ResponseWriter, r *http.Request) {
cats := getCatsFromParam(r)
if err := fragmentDivsTemplate.Execute(w, dataForTemplate(cats)); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}
func rafRenderHandler(w http.ResponseWriter, r *http.Request) {
cats := getCatsFromParam(r)
if err := rafTemplate.Execute(w, dataForTemplate(cats)); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}
func templateTagRenderHandler(w http.ResponseWriter, r *http.Request) {
cats := getCatsFromParam(r)
if err := templateTagTemplate.Execute(w, dataForTemplate(cats)); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}
func serverRenderHandle(w http.ResponseWriter, r *http.Request) {
cats := getCatsFromParam(r)
if err := serverTemplate.Execute(w, cats); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}
func serverDivsRenderHandle(w http.ResponseWriter, r *http.Request) {
cats := getCatsFromParam(r)
if err := serverDivsTemplate.Execute(w, cats); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}
func wrapHandler(h http.Handler) http.Handler {
gzipHandler := httpgzip.NewHandler(h)
handler := func(w http.ResponseWriter, r *http.Request) {
if r.FormValue("nochunk") != "" {
w.Header().Set("Transfer-Encoding", "identity")
}
w.Header().Set("Content-Type", "text/html")
if r.FormValue("nogzip") == "" {
gzipHandler.ServeHTTP(w, r)
} else {
h.ServeHTTP(w, r)
}
}
return http.HandlerFunc(handler)
}
func init() {
http.Handle("/dom_render", wrapHandler(http.HandlerFunc(domRenderHandler)))
http.Handle("/fragment_divs_render", wrapHandler(http.HandlerFunc(fragmentDivsRenderHandler)))
http.Handle("/raf_render", wrapHandler(http.HandlerFunc(rafRenderHandler)))
http.Handle("/template_tag_render", wrapHandler(http.HandlerFunc(templateTagRenderHandler)))
http.Handle("/server_render", wrapHandler(http.HandlerFunc(serverRenderHandle)))
http.Handle("/server_divs_render", wrapHandler(http.HandlerFunc(serverDivsRenderHandle)))
}
func main() {
log.Fatal(http.ListenAndServe(":8080", nil))
}