Skip to content

Commit b6e01a9

Browse files
committedAug 5, 2023
refactor!: assume HTMX for HTTP requests
1 parent 7240741 commit b6e01a9

File tree

5 files changed

+48
-38
lines changed

5 files changed

+48
-38
lines changed
 

‎pkg/background/background.go

+25-5
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,34 @@ func Run(ctx context.Context, backgrounds ...Background) <-chan struct{} {
3636
type BackgroundFunction func(ctx context.Context)
3737

3838
type Function struct {
39-
fn BackgroundFunction
39+
fn BackgroundFunction
40+
blocking Blocking
4041
}
4142

42-
func NewFunction(fn BackgroundFunction) Function {
43-
return Function{fn}
43+
type Blocking int
44+
45+
const (
46+
BlockingForever Blocking = iota // BlockingForever blocks forever.
47+
BlockingContext // BlockingContext blocks until context is done.
48+
)
49+
50+
// NewFunction create an adhoc function that implements the Background interface.
51+
func NewFunction(blocking Blocking, fn BackgroundFunction) Function {
52+
return Function{
53+
fn: fn,
54+
blocking: blocking,
55+
}
4456
}
4557

4658
func (f Function) Background(ctx context.Context, doneC chan<- struct{}) {
47-
doneC <- struct{}{}
48-
f.fn(ctx)
59+
switch f.blocking {
60+
case BlockingForever:
61+
doneC <- struct{}{}
62+
f.fn(ctx)
63+
case BlockingContext:
64+
f.fn(ctx)
65+
doneC <- struct{}{}
66+
default:
67+
panic("unhandled case")
68+
}
4969
}

‎web/controllers/auth.go

+1-4
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,7 @@ func LoginPost(c *fiber.Ctx, cc core.Context, store *session.Store) error {
2020
// Execute
2121
err := procs.AuthHTTPLogin(cc, username, password)
2222
if err != nil {
23-
if h.IsHTMXRequest(c) {
24-
return h.Render(c, "login", loginData(username, err.Error()), "form")
25-
}
26-
return h.Render(c, "login", loginData(username, err.Error()))
23+
return h.Render(c, "login", loginData(username, err.Error()), "form")
2724
}
2825

2926
// Response

‎web/helpers/http.go

+6-16
Original file line numberDiff line numberDiff line change
@@ -2,31 +2,21 @@ package helpers
22

33
import (
44
"github.com/gofiber/fiber/v2"
5+
"github.com/rs/zerolog/log"
56
)
67

78
const CSRFContextKey = "csrf_key"
89
const AnonymousContextKey = "anonymous_key"
910

10-
func IsHTMXRequest(c *fiber.Ctx) bool {
11-
_, isHTMXRequest := c.GetReqHeaders()["Hx-Request"]
12-
return isHTMXRequest
13-
}
14-
1511
func Redirect(c *fiber.Ctx, path string) error {
16-
if IsHTMXRequest(c) {
17-
c.Set("HX-Redirect", path)
18-
return nil
19-
} else {
20-
return c.Redirect(path)
21-
}
12+
c.Set("HX-Redirect", path)
13+
return nil
2214
}
2315

2416
func Error(c *fiber.Ctx, err error, codes ...int) error {
25-
if IsHTMXRequest(c) {
26-
c.Set("HX-Redirect", "/something-went-wrong")
27-
}
28-
29-
return Render(c, "something-went-wrong", fiber.Map{"Error": err})
17+
c.Set("HX-Redirect", "/something-went-wrong")
18+
log.Err(err).Msg("Request failed")
19+
return nil
3020
}
3121

3222
func NotFound(c *fiber.Ctx) error {

‎web/http/http.go

+15-12
Original file line numberDiff line numberDiff line change
@@ -23,25 +23,18 @@ type HTTP struct {
2323
}
2424

2525
func New(app core.App, shutdown context.CancelFunc, address string, bodyLimit int, sessionsPath string) HTTP {
26-
store := session.New(session.Config{
27-
Storage: bbolt.New(bbolt.Config{
28-
Database: sessionsPath,
29-
}),
30-
})
31-
32-
// Fiber
26+
// Views
3327
views := web.Engine()
3428
views.AddFuncMap(h.Map)
29+
30+
// Fiber
3531
http := fiber.New(fiber.Config{
3632
DisableStartupMessage: true,
3733
Views: views,
3834
ViewsLayout: "layouts/index",
3935
BodyLimit: bodyLimit,
4036
ErrorHandler: func(c *fiber.Ctx, err error) error {
41-
if h.IsHTMXRequest(c) {
42-
c.Set("HX-Redirect", "/something-went-wrong")
43-
}
44-
37+
c.Set("HX-Redirect", "/something-went-wrong")
4538
return fiber.DefaultErrorHandler(c, err)
4639
},
4740
})
@@ -52,10 +45,20 @@ func New(app core.App, shutdown context.CancelFunc, address string, bodyLimit in
5245
http.Use(csrf.New(csrf.Config{
5346
ContextKey: h.CSRFContextKey,
5447
Extractor: csrfExtractor(),
48+
ErrorHandler: func(c *fiber.Ctx, err error) error {
49+
c.Set("HX-Redirect", "/something-went-wrong")
50+
return fiber.DefaultErrorHandler(c, err)
51+
},
5552
}))
5653

54+
sessionStore := session.New(session.Config{
55+
Storage: bbolt.New(bbolt.Config{
56+
Database: sessionsPath,
57+
}),
58+
})
59+
5760
route(app,
58-
store,
61+
sessionStore,
5962
http,
6063
)
6164

‎web/views/index.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ <h2>Storage</h2>
6565
{{csrf $}}
6666
<button hx-post="/trim" data-loading-path="/trim" data-loading-disable data-loading-aria-busy
6767
title="Apply retention policy against data.">
68-
Trim Data
68+
Run Retention Policy
6969
</button>
7070
</form>
7171
<button hx-delete="/envelopes" data-loading-path="/envelopes" data-loading-disable data-loading-aria-busy

0 commit comments

Comments
 (0)
Please sign in to comment.