Skip to content

Commit 92c0896

Browse files
refactor(web): reroute actions
1 parent 3650b92 commit 92c0896

File tree

3 files changed

+34
-30
lines changed

3 files changed

+34
-30
lines changed

web/helpers/helpers.go

+6
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
"github.com/ItsNotGoodName/smtpbridge/internal/endpoint"
1616
"github.com/ItsNotGoodName/smtpbridge/internal/models"
1717
"github.com/ItsNotGoodName/smtpbridge/internal/trace"
18+
"github.com/ItsNotGoodName/smtpbridge/pkg/htmx"
1819
"github.com/ItsNotGoodName/smtpbridge/pkg/pagination"
1920
"github.com/dustin/go-humanize"
2021
"github.com/gorilla/schema"
@@ -142,3 +143,8 @@ func DecodeForm(w http.ResponseWriter, r *http.Request, form any) error {
142143
func ParseMultipartForm(r *http.Request) error {
143144
return r.ParseMultipartForm(32 << 20)
144145
}
146+
147+
func Reroute(w http.ResponseWriter, r *http.Request, view http.Handler) {
148+
htmx.SetRetarget(w, "body")
149+
view.ServeHTTP(w, r)
150+
}

web/http/router.go

+24-26
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@ import (
1616
"github.com/gorilla/sessions"
1717
)
1818

19-
const paramID = "{id}"
20-
2119
func NewRouter(ct pages.Controller, app core.App, fileFS fs.FS, csrfSecret []byte, ss sessions.Store) *chi.Mux {
2220
r := chi.NewRouter()
2321

@@ -62,74 +60,74 @@ func NewRouter(ct pages.Controller, app core.App, fileFS fs.FS, csrfSecret []byt
6260
pages.EnvelopeCreateView(ct, app))
6361
r.Post(routes.EnvelopeCreate().String(),
6462
pages.EnvelopeCreate(ct, app))
65-
r.Get(routes.Envelope(paramID).String(),
63+
r.Get(routes.Envelope(pages.ParamID).String(),
6664
pages.EnvelopeView(ct, app))
67-
r.Delete(routes.Envelope(paramID).String(),
65+
r.Delete(routes.Envelope(pages.ParamID).String(),
6866
pages.EnvelopeDelete(ct, app))
69-
r.Get(routes.EnvelopeHTML(paramID).String(),
67+
r.Get(routes.EnvelopeHTML(pages.ParamID).String(),
7068
pages.EnvelopeHTMLView(ct, app))
71-
r.Post(routes.EnvelopeEndpointSend(paramID).String(),
69+
r.Post(routes.EnvelopeEndpointSend(pages.ParamID).String(),
7270
pages.EnvelopeEndpointSend(ct, app))
7371
{
74-
envelopeListView := pages.EnvelopeListView(ct, app)
72+
view := pages.EnvelopeListView(ct, app)
7573
r.Get(routes.EnvelopeList().String(),
76-
envelopeListView)
74+
view)
7775
r.Delete(routes.EnvelopeList().String(),
78-
pages.EnvelopeListDrop(ct, app, envelopeListView))
76+
pages.EnvelopeListDrop(ct, app, view))
7977
}
8078

8179
// Attachment
8280
r.Get(routes.AttachmentFile("*").String(),
8381
pages.Files(ct, app, fileFS))
8482
{
85-
attachmentListView := pages.AttachmentListView(ct, app)
83+
view := pages.AttachmentListView(ct, app)
8684
r.Get(routes.AttachmentList().String(),
87-
attachmentListView)
85+
view)
8886
r.Post(routes.AttachmentTrim().String(),
89-
pages.AttachmentTrim(ct, app, attachmentListView))
87+
pages.AttachmentTrim(ct, app, view))
9088
}
9189

9290
// Endpoint
93-
r.Get(routes.Endpoint(paramID).String(),
91+
r.Get(routes.Endpoint(pages.ParamID).String(),
9492
pages.EndpointView(ct, app))
95-
r.Post(routes.Endpoint(paramID).String(),
93+
r.Post(routes.Endpoint(pages.ParamID).String(),
9694
pages.EndpointUpdate(ct, app))
9795
r.Get(routes.EndpointList().String(),
9896
pages.EndpointListView(ct, app))
99-
r.Post(routes.EndpointTest(paramID).String(),
97+
r.Post(routes.EndpointTest(pages.ParamID).String(),
10098
pages.EndpointTest(ct, app))
10199
r.Get(routes.EndpointCreate().String(),
102100
pages.EndpointCreateView(ct, app))
103101
r.Post(routes.EndpointCreate().String(),
104102
pages.EndpointCreate(ct, app))
105-
r.Delete(routes.Endpoint(paramID).String(),
103+
r.Delete(routes.Endpoint(pages.ParamID).String(),
106104
pages.EndpointDelete(ct, app))
107105

108106
// Traces
109107
{
110-
traceListView := pages.TraceListView(ct, app)
108+
view := pages.TraceListView(ct, app)
111109
r.Get(routes.TraceList().String(),
112-
traceListView)
110+
view)
113111
r.Delete(routes.TraceList().String(),
114-
pages.TraceListDrop(ct, app, traceListView))
112+
pages.TraceListDrop(ct, app, view))
115113
}
116114

117115
// Rules
118116
r.Get(routes.RuleList().String(),
119117
pages.RuleListView(ct, app))
120-
r.Get(routes.Rule(paramID).String(),
118+
r.Get(routes.Rule(pages.ParamID).String(),
121119
pages.RuleView(ct, app))
122-
r.Delete(routes.Rule(paramID).String(),
120+
r.Delete(routes.Rule(pages.ParamID).String(),
123121
pages.RuleDelete(ct, app))
124-
r.Post(routes.Rule(paramID).String(),
122+
r.Post(routes.Rule(pages.ParamID).String(),
125123
pages.RuleUpdate(ct, app))
126124
r.Post(routes.RuleExpressionCheck().String(),
127125
pages.RuleExpressionCheck(ct, app))
128126
r.Get(routes.RuleCreate().String(),
129127
pages.RuleCreateView(ct, app))
130128
r.Post(routes.RuleCreate().String(),
131129
pages.RuleCreate(ct, app))
132-
r.Post(routes.RuleToggle(paramID).String(),
130+
r.Post(routes.RuleToggle(pages.ParamID).String(),
133131
pages.RuleToggle(ct, app))
134132

135133
// Retention Policy
@@ -141,11 +139,11 @@ func NewRouter(ct pages.Controller, app core.App, fileFS fs.FS, csrfSecret []byt
141139
pages.EndpointFormConfigComponent(ct, app))
142140
r.Get(routes.StorageStatsComponent().String(),
143141
pages.StorageStatsComponent(ct, app))
144-
r.Get(routes.EnvelopeTabComponent(paramID, routes.EnvelopeTabText).String(),
142+
r.Get(routes.EnvelopeTabComponent(pages.ParamID, routes.EnvelopeTabText).String(),
145143
pages.EnvelopeTabComponent(ct, app, routes.EnvelopeTabText))
146-
r.Get(routes.EnvelopeTabComponent(paramID, routes.EnvelopeTabHTML).String(),
144+
r.Get(routes.EnvelopeTabComponent(pages.ParamID, routes.EnvelopeTabHTML).String(),
147145
pages.EnvelopeTabComponent(ct, app, routes.EnvelopeTabHTML))
148-
r.Get(routes.EnvelopeTabComponent(paramID, routes.EnvelopeTabAttachments).String(),
146+
r.Get(routes.EnvelopeTabComponent(pages.ParamID, routes.EnvelopeTabAttachments).String(),
149147
pages.EnvelopeTabComponent(ct, app, routes.EnvelopeTabAttachments))
150148
r.Get(routes.RecentEnvelopeListComponent().String(),
151149
pages.RecentEnvelopeListComponent(ct, app))

web/pages/pages.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ import (
3030

3131
// utils
3232

33+
const ParamID = "{id}"
34+
3335
func withID(ct Controller, fn func(w http.ResponseWriter, r *http.Request, id int64)) http.HandlerFunc {
3436
return func(w http.ResponseWriter, r *http.Request) {
3537
id, err := strconv.ParseInt(chi.URLParam(r, "id"), 10, 64)
@@ -459,8 +461,7 @@ func TraceListDrop(ct Controller, app core.App, view http.Handler) http.HandlerF
459461
return
460462
}
461463

462-
htmx.SetRetarget(w, "body")
463-
view.ServeHTTP(w, r)
464+
helpers.Reroute(w, r, view)
464465
}
465466
}
466467
func RuleListView(ct Controller, app core.App) http.HandlerFunc {
@@ -490,8 +491,7 @@ func AttachmentTrim(ct Controller, app core.App, view http.Handler) http.Handler
490491
return
491492
}
492493

493-
htmx.SetRetarget(w, "body")
494-
view.ServeHTTP(w, r)
494+
helpers.Reroute(w, r, view)
495495
}
496496
}
497497

0 commit comments

Comments
 (0)