-
Notifications
You must be signed in to change notification settings - Fork 5
/
route_context.go
222 lines (191 loc) · 5.72 KB
/
route_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
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
package fir
import (
"bytes"
"encoding/gob"
"errors"
"net/http"
"net/url"
"reflect"
"github.com/goccy/go-json"
"github.com/fatih/structs"
firErrors "github.com/livefir/fir/internal/errors"
)
type ContextKey int
const (
// PathParamsKey is the key for the path params in the request context.
PathParamsKey ContextKey = iota
// UserKey is the key for the user id/name in the request context. It is used in the default channel function.
UserKey
)
type PathParams map[string]any
type userStore map[string]any
func init() {
gob.Register(userStore{})
}
// RouteContext is the context for a route handler.
// Its methods are used to return data or patch operations to the client.
type RouteContext struct {
event Event
request *http.Request
response http.ResponseWriter
urlValues url.Values
route *route
isOnLoad bool
}
func (c RouteContext) Event() Event {
return c.event
}
// Bind decodes the event params into the given struct
func (c RouteContext) Bind(v any) error {
if v == nil {
return errors.New("bind value cannot be nil")
}
val := reflect.ValueOf(v)
if val.Kind() != reflect.Ptr {
return errors.New("bind value must be a pointer to a struct")
}
if val.Kind() == reflect.Ptr {
val = val.Elem() // dereference the pointer
if val.Kind() != reflect.Struct {
return errors.New("bind value must be a pointer to a struct")
}
}
if err := c.BindPathParams(v); err != nil {
return err
}
if err := c.BindQueryParams(v); err != nil {
return err
}
return c.BindEventParams(v)
}
func (c RouteContext) BindPathParams(v any) error {
if v == nil {
return nil // nothing to bind
}
m, ok := v.(map[string]any)
if ok {
for k := range m {
if value := c.request.Context().Value(k); value != nil {
m[k] = value
}
}
v = m
return nil
}
pathParams, ok := c.request.Context().Value(PathParamsKey).(PathParams)
if !ok {
return nil
}
s := structs.New(v)
for _, field := range s.Fields() {
if field.IsExported() {
if v, ok := pathParams[field.Tag("json")]; ok {
err := field.Set(v)
if err != nil {
return err
}
continue
}
if v, ok := pathParams[field.Tag("json")]; ok {
err := field.Set(v)
if err != nil {
return err
}
}
}
}
return nil
}
func (c RouteContext) BindQueryParams(v any) error {
return c.route.formDecoder.Decode(v, c.request.URL.Query())
}
func (c RouteContext) BindEventParams(v any) error {
if c.event.Params == nil {
return nil
}
if c.event.IsForm {
if len(c.urlValues) == 0 {
var urlValues url.Values
if err := json.NewDecoder(bytes.NewReader(c.event.Params)).Decode(&urlValues); err != nil {
return err
}
c.urlValues = urlValues
}
return c.route.formDecoder.Decode(v, c.urlValues)
}
return json.NewDecoder(bytes.NewReader(c.event.Params)).Decode(v)
}
// Request returns the http.Request for the current context
func (c RouteContext) Request() *http.Request {
return c.request
}
// Response returns the http.ResponseWriter for the current context
func (c RouteContext) Response() http.ResponseWriter {
return c.response
}
// Redirect redirects the client to the given url
func (c RouteContext) Redirect(url string, status int) error {
if url == "" {
return errors.New("url is required")
}
if status < 300 || status > 308 {
return errors.New("status code must be between 300 and 308")
}
http.Redirect(c.response, c.request, url, status)
return nil
}
// KV is a wrapper for ctx.Data(map[string]any{key: data})
func (c RouteContext) KV(key string, data any) error {
return buildData(false, map[string]any{key: data})
}
// KV is a wrapper for ctx.State(map[string]any{key: data})
func (c RouteContext) StateKV(key string, data any) error {
return buildData(true, map[string]any{key: data})
}
// State data is only passed to event receiver without a bound template
// it can be acccessed in the event receiver via $event.detail
// e.g. @fir:myevent:ok="console.log('$event.detail.mykey')"
func (c RouteContext) State(dataset ...any) error {
return buildData(true, dataset...)
}
// Data sets the data to be hydrated into the route's template or an event's associated template/block action
// It accepts either a map or struct type so that fir can inject utility functions: fir.Error, fir.ActiveRoute etc.
// If the data is a struct, it will be converted to a map using github.com/fatih/structs
// If the data is a pointer to a struct, it will be dereferenced and converted to a map using github.com/fatih/structs
// If the data is a map, it will be used as is
// If the data is a pointer to a map, it will be dereferenced and used as is
// The function will return nil if no data is passed
// The function accepts variadic arguments so that you can pass multiple structs or maps which will be merged
func (c RouteContext) Data(dataset ...any) error {
return buildData(false, dataset...)
}
// FieldError sets the error message for the given field and can be looked up by {{fir.Error "myevent.field"}}
func (c RouteContext) FieldError(field string, err error) error {
if err == nil || field == "" {
return nil
}
return &firErrors.Fields{field: firErrors.User(err)}
}
// FieldErrors sets the error messages for the given fields and can be looked up by {{fir.Error "myevent.field"}}
func (c RouteContext) FieldErrors(fields map[string]error) error {
m := firErrors.Fields{}
for field, err := range fields {
if err != nil {
m[field] = firErrors.User(err)
}
}
return &m
}
func (c RouteContext) Status(code int, err error) error {
return &firErrors.Status{Code: code, Err: firErrors.User(err)}
}
func (c RouteContext) GetUserFromContext() string {
return getUserFromRequestContext(c.request)
}
func getUserFromRequestContext(r *http.Request) string {
user, ok := r.Context().Value(UserKey).(string)
if !ok {
return ""
}
return user
}