-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbuilder.go
95 lines (81 loc) · 2.05 KB
/
builder.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
package web
import (
"bytes"
"net/http"
"strings"
"time"
"github.com/NYTimes/gziphandler"
h "github.com/theplant/htmlgo"
)
type Builder struct {
EventsHub
layoutFunc LayoutFunc
}
func New() (b *Builder) {
b = new(Builder)
b.layoutFunc = defaultLayoutFunc
return
}
func (b *Builder) LayoutFunc(mf LayoutFunc) (r *Builder) {
if mf == nil {
panic("layout func is nil")
}
b.layoutFunc = mf
return b
}
func (p *Builder) EventFuncs(vs ...interface{}) (r *Builder) {
p.addMultipleEventFuncs(vs...)
return p
}
type ComponentsPack string
var startTime = time.Now()
func PacksHandler(contentType string, packs ...ComponentsPack) http.Handler {
return Default.PacksHandler(contentType, packs...)
}
func (b *Builder) PacksHandler(contentType string, packs ...ComponentsPack) http.Handler {
buf := bytes.NewBuffer(nil)
for _, pk := range packs {
// buf = append(buf, []byte(fmt.Sprintf("\n// pack %d\n", i+1))...)
// buf = append(buf, []byte(fmt.Sprintf("\nconsole.log('pack %d, length %d');\n", i+1, len(pk)))...)
buf.WriteString(string(pk))
// fmt.Println(contentType)
if strings.Contains(strings.ToLower(contentType), "javascript") {
buf.WriteString(";")
}
buf.WriteString("\n\n")
}
body := bytes.NewReader(buf.Bytes())
return gziphandler.GzipHandler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", contentType)
http.ServeContent(w, r, "", startTime, body)
}))
}
func NoopLayoutFunc(in PageFunc) PageFunc {
return in
}
func defaultLayoutFunc(in PageFunc) PageFunc {
return func(ctx *EventContext) (r PageResponse, err error) {
r, err = in(ctx)
if r.PageTitle != "" {
ctx.Injector.Title(r.PageTitle)
}
if err != nil {
panic(err)
}
r.Body = h.HTMLComponents{
h.RawHTML("<!DOCTYPE html>\n"),
h.Tag("html").Children(
h.Head(
ctx.Injector.GetHeadHTMLComponent(),
),
h.Body(
h.Div(
r.Body,
).Id("app").Attr("v-cloak", true),
ctx.Injector.GetTailHTMLComponent(),
).Class("front"),
).Attr(ctx.Injector.HTMLLangAttrs()...),
}
return
}
}