-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
viewmgr.go
207 lines (166 loc) · 4.81 KB
/
viewmgr.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
package web
import (
"bytes"
"os"
"io"
"io/ioutil"
"reflect"
"template"
"fmt"
)
type RenderBuffer struct {
io.Writer
}
type Renderable interface {
Render(w RenderBuffer) (err os.Error)
}
type Prepareable interface {
Prepare(request *Request) (err os.Error)
}
type PrepareableRenderable interface {
Renderable
Prepareable
}
type View interface {
PrepareableRenderable
fmt.Stringer
}
/**
Page is a RequestHandler and is designed for handling http requests.
A page is a view and structured by sub-views.
Usage:
feed := new(MyFeedOrViewModel)
page, err := web.NewPage("home.html", feed)
app.Handle("/home", page)
*/
type Page struct {
templateView
}
type HtmlPage Page
func NewHtmlPage(fn string, feed interface{}) (page *HtmlPage, err os.Error) {
p, err := NewPage(fn, feed)
page = (*HtmlPage)(p)
return
}
func NewPage(fn string, feed interface{}) (page *Page, err os.Error) {
f, err := os.Open(fn, os.O_RDONLY, 0)
if err != nil {
return
}
s, err := ioutil.ReadAll(f)
if err != nil {
return
}
page, err = NewPageFromString(string(s), feed)
return
}
func NewPageFromString(s string, feed interface{}) (page *Page, err os.Error) {
var temp *template.Template
temp, err = template.Parse(s, nil/* TODO: make use of it? */)
if err != nil {
return
}
page = &Page{ templateView{ temp, feed } }
return
}
func NewHtmlPageFromString(s string, feed interface{}) (page *HtmlPage, err os.Error) {
var pg *Page
pg, err = NewPageFromString(s, feed)
if err != nil {
page = (*HtmlPage)(pg)
}
return
}
func (p *Page) HandleRequest(request *Request, response *Response) (err os.Error) {
buf := RenderBuffer{ response.BodyWriter }
err = p.templateView.Prepare(request)
if err != nil {
return
}
err = p.templateView.Render(buf)
return
}
/*
func (vm *Page) HandleSubpath(subpath string, request *Request) (handled bool) {
return
}
*/
func (p *HtmlPage) HandleRequest(request *Request, response *Response) (err os.Error) {
response.Header.Set("Content-Type", "text/html")
err = (*Page)(p).HandleRequest(request, response)
return
}
type viewStringer struct { PrepareableRenderable }
func (v viewStringer) String() string {
b := bytes.NewBuffer(make([]byte, 0, 512))
err := v.Render(RenderBuffer{ b })
if err != nil {
return err.String()
}
return b.String()
}
type nothingPrepareable struct { Renderable }
func (v nothingPrepareable) Prepare(request *Request) os.Error { return nil }
func NewView(v Renderable) (view View) {
if _, ok := v.(Renderable); ok {
}
var vv PrepareableRenderable
if _, ok := v.(Prepareable); ok {
vv, _ = v.(PrepareableRenderable)
} else {
vv = nothingPrepareable{ v }
}
view = viewStringer{ vv }
return
}
// Template implements View interface.
type templateView struct {
template *template.Template
feed interface{}
}
func NewTemplate(fn string, feed interface{}) (view View, err os.Error) {
f, err := os.Open(fn, os.O_RDONLY, 0)
if err != nil {
return
}
s, err := ioutil.ReadAll(f)
if err != nil {
return
}
view, err = NewTemplateFromString(string(s), feed)
return
}
func NewTemplateFromString(s string, feed interface{}) (view View, err os.Error) {
var temp *template.Template
temp, err = template.Parse(s, nil/* TODO: make use of it? */)
if err != nil {
return
}
var tv *templateView
tv = &templateView{ temp, feed }
view = NewView(tv)
return
}
func (v *templateView) Prepare(request *Request) (err os.Error) {
if p, ok := v.feed.(Prepareable); ok {
err = p.Prepare(request)
// TODO: prepare feed fields
}
vv := reflect.NewValue(v.feed)
if p, ok := vv.(*reflect.PtrValue); ok {
vv = p.Elem()
}
if sv, ok := vv.(*reflect.StructValue); ok {
for i := 0; i < sv.NumField(); i += 1 {
f := sv.Field(i)
if p, ok := f.Interface().(Prepareable); ok {
err = p.Prepare(request)
}
}
}
return
}
func (v *templateView) Render(w RenderBuffer) (err os.Error) {
err = v.template.Execute( w, v.feed )
return
}