-
Notifications
You must be signed in to change notification settings - Fork 1
/
ctrl.go
300 lines (245 loc) · 8 KB
/
ctrl.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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
package htmx
import (
"context"
"database/sql"
"errors"
"github.com/gofiber/fiber/v2"
goth "github.com/zeiss/fiber-goth"
"github.com/zeiss/fiber-goth/adapters"
reload "github.com/zeiss/fiber-reload"
"gorm.io/gorm"
)
// Controller is the interface for the htmx controller.
type Controller interface {
// Init is called when the controller is initialized.
Init(ctx *fiber.Ctx) error
// Prepare is called before the controller is executed.
Prepare() error
// Finalize is called after the controller is executed.
Finalize() error
// Get is called when the controller is executed with the GET method.
Get() error
// Post is called when the controller is executed with the POST method.
Post() error
// Put is called when the controller is executed with the PUT method.
Put() error
// Patch is called when the controller is executed with the PATCH method.
Patch() error
// Delete is called when the controller is executed with the DELETE method.
Delete() error
// Options is called when the controller is executed with the OPTIONS method.
Options() error
// Trace is called when the controller is executed with the TRACE method.
Trace() error
// Head is called when the controller is executed with the HEAD method.
Head() error
// Error is called when an error occurs.
Error(err error) error
// Reset resets the controller.
Reset()
// Ctx returns the fiber.Ctx.
Ctx() *fiber.Ctx
}
// TransactionController is the interface for a controller that also does database transactions.
type TransactionController interface {
// Returns the transaction.
Tx() *gorm.DB
// Begin begins a transaction.
Begin(*gorm.DB) error
// Commit commits the transaction.
Commmit() error
// Rollback rolls back the transaction.
Rollback() error
}
var _ Controller = (*DefaultController)(nil)
// UnimplementedController is not to be used anymore.
// Deprecated: Use DefaultController instead.
type UnimplementedController = DefaultController
// NewDefaultController returns a new default controller.
func NewDefaultController() *DefaultController {
return &DefaultController{}
}
// UnimplementedController is the default controller implementation.
type DefaultController struct {
ctx *fiber.Ctx
}
// Init is called when the controller is initialized.
func (c *DefaultController) Init(ctx *fiber.Ctx) error {
c.Reset()
c.ctx = ctx
return nil
}
// Prepare is called before the controller is executed.
func (c *DefaultController) Prepare() error {
return nil
}
// Finalize is called after the controller is executed.
func (c *DefaultController) Finalize() error {
return nil
}
// Get is called when the controller is executed with the GET method.
func (c *DefaultController) Get() error {
return fiber.NewError(fiber.StatusNotImplemented)
}
// Post is called when the controller is executed with the POST method.
func (c *DefaultController) Post() error {
return fiber.NewError(fiber.StatusNotImplemented)
}
// Put is called when the controller is executed with the PUT method.
func (c *DefaultController) Put() error {
return fiber.NewError(fiber.StatusNotImplemented)
}
// Patch is called when the controller is executed with the PATCH method.
func (c *DefaultController) Patch() error {
return fiber.NewError(fiber.StatusNotImplemented)
}
// Delete is called when the controller is executed with the DELETE method.
func (c *DefaultController) Delete() error {
return fiber.NewError(fiber.StatusNotImplemented)
}
// Options is called when the controller is executed with the OPTIONS method.
func (c *DefaultController) Options() error {
return fiber.NewError(fiber.StatusNotImplemented)
}
// Error is called when an error occurs.
func (c *DefaultController) Error(err error) error {
return fiber.NewError(fiber.StatusInternalServerError)
}
// Head is called when the controller is executed with the HEAD method.
func (c *DefaultController) Head() error {
return fiber.NewError(fiber.StatusNotImplemented)
}
// Trace is called when the controller is executed with the TRACE method.
func (c *DefaultController) Trace() error {
return fiber.NewError(fiber.StatusNotImplemented)
}
// BindForm binds the form to the given struct.
func (c *DefaultController) BindBody(obj interface{}) error {
return c.ctx.BodyParser(obj)
}
// BindParams binds the params to the given struct.
func (c *DefaultController) BindParams(obj interface{}) error {
return c.ctx.ParamsParser(obj)
}
// BindQuery binds the query to the given struct.
func (c *DefaultController) BindQuery(obj interface{}) error {
return c.ctx.QueryParser(obj)
}
// Values is a helper function to get the values from the context.
func (c *DefaultController) Values(key any, value ...any) (val any) {
return c.ctx.Locals(key, value...)
}
// ValuesString is a helper function to get the values as a string from the context.
func (c *DefaultController) ValuesString(key any, value ...any) (val string) {
return c.ctx.Locals(key, value...).(string)
}
// ValuesInt is a helper function to get the values as an int from the context.
func (c *DefaultController) ValuesInt(key any, value ...any) (val int) {
return c.ctx.Locals(key, value...).(int)
}
// ValuesBool is a helper function to get the values as a bool from the context.
func (c *DefaultController) ValuesBool(key any, value ...any) (val bool) {
return c.ctx.Locals(key, value...).(bool)
}
// Session is a helper function to get the session from the context.
func (c *DefaultController) Session() adapters.GothSession {
session, err := goth.SessionFromContext(c.Ctx())
if err != nil {
return adapters.GothSession{}
}
return session
}
// Messages is a helper function to get the message from the context.
func (c *DefaultController) Messages(msgs ...HtmxMessage) {
m := MessagesFromContext(c.Ctx())
m.Add(msgs...)
}
// Context returns the context.
func (c *DefaultController) Context() context.Context {
return c.ctx.Context()
}
// Ctx returns the fiber.Ctx.
func (c *DefaultController) Ctx() *fiber.Ctx {
return c.ctx
}
// IsDevelopment returns true if the environment is development.
func (c *DefaultController) IsDevelopment() bool {
return reload.IsDevelopment(c.ctx.UserContext())
}
// IsProduction returns true if the environment is production.
func (c *DefaultController) IsProduction() bool {
return reload.IsProduction(c.ctx.UserContext())
}
// Redirect redirects to the given path.
func (c *DefaultController) Redirect(path string) error {
Redirect(c.ctx, path)
return nil
}
// Render renders a component.
func (c *DefaultController) Render(node Node, opt ...RenderOpt) error {
return RenderComp(c.ctx, node, opt...)
}
// Path returns the path.
func (c *DefaultController) Path() string {
return c.ctx.Path()
}
// OriginalURL returns the original URL.
func (c *DefaultController) OriginalURL() string {
return c.ctx.OriginalURL()
}
// Reset resets the controller.
func (c *DefaultController) Reset() {
c.ctx = nil
}
var _ TransactionController = (*DefaultTransactionController)(nil)
// NewTransactionController returns a new transaction controller.
func NewTransactionController() *DefaultTransactionController {
return &DefaultTransactionController{
DefaultController: NewDefaultController(),
}
}
// DefaultTransactionController is the interface for the htmx transaction controller.
type DefaultTransactionController struct {
*DefaultController
tx *gorm.DB
}
// Begin begins a transaction.
func (c *DefaultTransactionController) Begin(conn *gorm.DB) error {
tx := conn.Begin()
if tx.Error != nil {
return tx.Error
}
c.tx = tx
return nil
}
// Commit commits the transaction.
func (c *DefaultTransactionController) Commmit() error {
if c.tx == nil {
return nil
}
c.tx.Commit()
if err := c.tx.Error; err != nil {
return err
}
c.tx = nil
return nil
}
// Rollback rolls back the transaction.
func (c *DefaultTransactionController) Rollback() error {
if c.tx == nil {
return nil
}
c.tx.Rollback()
if err := c.tx.Error; err != nil && !errors.Is(err, sql.ErrTxDone) {
return err
}
c.tx = nil
return nil
}
// Txn returns the transaction.
func (c *DefaultTransactionController) Tx() *gorm.DB {
if c.tx != nil {
return c.tx
}
return nil
}