-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcontext.go
167 lines (140 loc) · 3.69 KB
/
context.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
package pbj
import (
"fmt"
"net/http"
"net/url"
"path/filepath"
"strings"
"github.com/labstack/echo/v5"
"github.com/pkg/errors"
"github.com/pocketbase/pocketbase/apis"
"github.com/pocketbase/pocketbase/models"
"github.com/pocketbase/pocketbase/tools/template"
)
type Context interface {
Request() *http.Request
Response() *echo.Response
FormValue(string) string
FormValues() (url.Values, error)
PathParam(string) string
QueryParam(string) string
Admin() *models.Admin
User() *models.Record
Set(string, any)
Props() map[string]any
Refresh() error
Push(url string) error
Replace(url string) error
Render(string) error
}
var (
_ Context = (*pageContext)(nil)
_ Context = (*eventContext)(nil)
)
// Page Context
type pageContext struct {
echo.Context
page *Page
props map[string]any
}
func (ctx *pageContext) Admin() *models.Admin {
admin, _ := ctx.Get(apis.ContextAdminKey).(*models.Admin)
return admin
}
func (ctx *pageContext) User() *models.Record {
user, _ := ctx.Get(apis.ContextAuthRecordKey).(*models.Record)
return user
}
func (ctx *pageContext) Event(n string) string {
return fmt.Sprintf("%s/@%s", ctx.Request().URL.Path, n)
}
func (ctx *pageContext) Props() map[string]any {
return ctx.props
}
func (ctx *pageContext) Set(k string, v any) {
ctx.props[k] = v
}
func (ctx *pageContext) Unwrap() echo.Context {
return ctx.Context
}
func (ctx *pageContext) Refresh() error {
ctx.Response().Header().Set("HX-Refresh", "true")
return nil
}
func (ctx *pageContext) Push(url string) error {
ctx.Response().Header().Set("HX-Redirect", url)
return nil
}
func (ctx *pageContext) Replace(url string) error {
ctx.Response().Header().Set("HX-Location", url)
return nil
}
func (ctx *pageContext) Render(name string) error {
reg := template.NewRegistry()
// Simplify the api - file based routing?
if name == "" {
name = ctx.page.route
}
parts, _ := filepath.Glob("templates/partials/*.html")
parts = append([]string{"templates/" + name + ".html"}, parts...)
html, err := reg.AddFuncs(map[string]any{
"event": ctx.Event,
}).LoadFiles(parts...).Render(ctx.Props())
if err != nil {
return errors.Wrap(err, "failed to render")
}
return ctx.HTML(http.StatusOK, html)
}
// Event Context
type eventContext struct {
echo.Context
app *App
page *Page
props map[string]any
}
func (ctx *eventContext) Admin() *models.Admin {
admin, _ := ctx.Get(apis.ContextAdminKey).(*models.Admin)
return admin
}
func (ctx *eventContext) User() *models.Record {
user, _ := ctx.Get(apis.ContextAuthRecordKey).(*models.Record)
return user
}
func (ctx *eventContext) Event(n string) string {
path := ctx.Request().URL.Path
parts := strings.Split(path, "/@")
return fmt.Sprintf("%s/@%s", parts[0], n)
}
func (ctx *eventContext) Props() map[string]any {
return ctx.props
}
func (ctx *eventContext) Set(k string, v any) {
ctx.props[k] = v
}
func (ctx *eventContext) Unwrap() echo.Context {
return ctx.Context
}
func (ctx *eventContext) Refresh() error {
ctx.Response().Header().Set("HX-Refresh", "true")
return ctx.NoContent(http.StatusOK)
}
func (ctx *eventContext) Push(url string) error {
ctx.Response().Header().Set("HX-Redirect", url)
return ctx.NoContent(http.StatusOK)
}
func (ctx *eventContext) Replace(url string) error {
ctx.Response().Header().Set("HX-Location", url)
return ctx.NoContent(http.StatusOK)
}
func (ctx *eventContext) Render(name string) error {
reg := template.NewRegistry()
parts, _ := filepath.Glob("templates/partials/*.html")
parts = append([]string{"templates/" + name + ".html"}, parts...)
html, err := reg.AddFuncs(map[string]any{
"event": ctx.Event,
}).LoadFiles(parts...).Render(ctx.Props())
if err != nil {
return err
}
return ctx.HTML(http.StatusOK, html)
}