-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontext.go
More file actions
174 lines (142 loc) · 2.73 KB
/
context.go
File metadata and controls
174 lines (142 loc) · 2.73 KB
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
package pages
import (
"context"
"html/template"
"net/http"
"sync"
)
type contextKey struct{}
func FromContext(ctx context.Context) *Context {
c, _ := ctx.Value(contextKey{}).(*Context)
return c
}
func MustContext(ctx context.Context) *Context {
if c := FromContext(ctx); c != nil {
return c
}
panic("Pages context is nil, you should use pages.NewContext before pages.MustContext or use pages.FromContext directly.")
}
var ctxPool = &sync.Pool{
New: func() any {
c := new(Context)
c.Reset()
return c
},
}
func NewContext(parent context.Context) (context.Context, context.CancelFunc) {
c := ctxPool.Get().(*Context)
cancel := func() {
c.Reset()
ctxPool.Put(c)
}
return context.WithValue(parent, contextKey{}, c), cancel
}
type Context struct {
dom *DOM
site *Site
page *Page
err error
debug bool
guest bool
status int
content template.HTML
template string
}
func (c *Context) Reset() {
c.status = http.StatusOK
c.template = ""
c.content = ""
c.debug = false
c.guest = true
c.dom = nil
c.site = nil
c.page = nil
c.err = nil
}
func (c *Context) DOM() *DOM {
if c.dom == nil {
c.dom = new(DOM)
}
return c.dom
}
func (c *Context) Site() *Site {
return c.site
}
func (c *Context) SetSite(site *Site) {
c.site = site
}
func (c *Context) HasSite() bool {
return c.site != nil
}
func (c *Context) Page() *Page {
return c.page
}
func (c *Context) SetPage(page *Page) {
if page != nil {
if page.Site == nil && c.HasSite() {
page.SiteID = c.site.ID
page.Site = c.site
}
}
c.page = page
}
func (c *Context) HasPage() bool {
return c.page != nil
}
func (c *Context) Error() error {
return c.err
}
func (c *Context) SetError(err error) {
c.err = err
}
func (c *Context) HasError() bool {
return c.err != nil
}
func (c *Context) Debug() bool {
return c.debug
}
func (c *Context) SetDebug(debug bool) {
c.debug = debug
}
func (c *Context) Guest() bool {
return c.guest
}
func (c *Context) SetGuest(guest bool) {
c.guest = guest
}
func (c *Context) Status() int {
if c.status <= 0 {
return http.StatusOK
}
if c.HasError() && c.status < http.StatusBadRequest {
return http.StatusInternalServerError
}
return c.status
}
func (c *Context) SetStatus(status int) {
c.status = status
}
func (c *Context) Content() template.HTML {
return c.content
}
func (c *Context) SetContent(content template.HTML) {
c.content = content
}
func (c *Context) HasContent() bool {
return c.content != ""
}
func (c *Context) Template() string {
if c.template != "" {
return c.template
}
if c.HasPage() {
return c.page.Template
}
return ""
}
func (c *Context) SetTemplate(template string) {
c.template = template
}
func (c *Context) HasTemplate() bool {
return c.Template() != ""
}