From f09d61509f385fd63be2c5c8558b5de5c3330d07 Mon Sep 17 00:00:00 2001 From: Teajey <21069848+Teajey@users.noreply.github.com> Date: Fri, 13 Feb 2026 10:36:24 +0900 Subject: [PATCH] Update to rsvp 0.19 --- http/go.mod | 2 +- http/go.sum | 4 +-- http/handler/private.go | 8 ++--- http/handler/public.go | 8 ++--- http/middleware/prefix.go | 8 ++--- http/mux/private.go | 60 +++++++++++++++++++++++--------- http/mux/public.go | 2 +- http/mux/serve.go | 34 ++++++++++-------- http/response/response.go | 18 +++++----- http/routing/account.go | 14 ++++---- http/routing/config.go | 6 ++-- http/routing/events.go | 4 +-- http/routing/foreign_wishlist.go | 18 +++++----- http/routing/groups.go | 44 +++++++++++------------ http/routing/home.go | 18 +++++----- http/routing/login.go | 24 ++++++------- http/routing/logout.go | 4 +-- http/routing/not_found.go | 4 +-- http/routing/register.go | 14 ++++---- http/routing/todo.go | 12 +++---- http/routing/users.go | 26 +++++++------- http/routing/wishlist.go | 42 +++++++++++----------- http/server/server.go | 24 ++++++------- http/session/session.go | 17 --------- 24 files changed, 215 insertions(+), 200 deletions(-) diff --git a/http/go.mod b/http/go.mod index d58d3a4..8cb913f 100644 --- a/http/go.mod +++ b/http/go.mod @@ -5,7 +5,7 @@ go 1.23.3 toolchain go1.24.5 require ( - github.com/Teajey/rsvp v0.14.0 + github.com/Teajey/rsvp v0.19.0-rc.2 github.com/gorilla/sessions v1.4.0 golang.org/x/crypto v0.39.0 ) diff --git a/http/go.sum b/http/go.sum index 88ffc99..041439d 100644 --- a/http/go.sum +++ b/http/go.sum @@ -1,5 +1,5 @@ -github.com/Teajey/rsvp v0.14.0 h1:BkmMRQhmOtzSbWLaW4wbg73VNzK/FXlVDCgxRqkTNNs= -github.com/Teajey/rsvp v0.14.0/go.mod h1:z0L20VphVg+Ec2+hnpLFTG2MZTrWYFprav1kpxDba0Q= +github.com/Teajey/rsvp v0.19.0-rc.2 h1:h4V7c3uGlSKajkWKt3fldFV+XTMEI8A4o05PMY8xv9A= +github.com/Teajey/rsvp v0.19.0-rc.2/go.mod h1:z0L20VphVg+Ec2+hnpLFTG2MZTrWYFprav1kpxDba0Q= github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/google/gofuzz v1.2.0 h1:xRy4A+RhZaiKjJ1bPfwQ8sedCA+YS2YcCHW6ec7JMi0= diff --git a/http/handler/private.go b/http/handler/private.go index aa11f33..e08d43d 100644 --- a/http/handler/private.go +++ b/http/handler/private.go @@ -10,11 +10,11 @@ import ( ) type Private interface { - ServeHTTP(*lishwist.Session, *session.Session, http.Header, *http.Request) rsvp.Response + ServeHTTP(*lishwist.Session, *session.Session, rsvp.ResponseWriter, *http.Request) rsvp.Body } -type PrivateFunc func(*lishwist.Session, *session.Session, http.Header, *http.Request) rsvp.Response +type PrivateFunc func(*lishwist.Session, *session.Session, rsvp.ResponseWriter, *http.Request) rsvp.Body -func (f PrivateFunc) ServeHTTP(app *lishwist.Session, s *session.Session, h http.Header, r *http.Request) rsvp.Response { - return f(app, s, h, r) +func (f PrivateFunc) ServeHTTP(app *lishwist.Session, s *session.Session, w rsvp.ResponseWriter, r *http.Request) rsvp.Body { + return f(app, s, w, r) } diff --git a/http/handler/public.go b/http/handler/public.go index 864cdce..583c870 100644 --- a/http/handler/public.go +++ b/http/handler/public.go @@ -9,11 +9,11 @@ import ( ) type Public interface { - ServeHTTP(*session.Session, http.Header, *http.Request) rsvp.Response + ServeHTTP(*session.Session, rsvp.ResponseWriter, *http.Request) rsvp.Body } -type PublicFunc func(*session.Session, http.Header, *http.Request) rsvp.Response +type PublicFunc func(*session.Session, rsvp.ResponseWriter, *http.Request) rsvp.Body -func (f PublicFunc) ServeHTTP(s *session.Session, h http.Header, r *http.Request) rsvp.Response { - return f(s, h, r) +func (f PublicFunc) ServeHTTP(s *session.Session, w rsvp.ResponseWriter, r *http.Request) rsvp.Body { + return f(s, w, r) } diff --git a/http/middleware/prefix.go b/http/middleware/prefix.go index ccc7fde..f1159b5 100644 --- a/http/middleware/prefix.go +++ b/http/middleware/prefix.go @@ -8,15 +8,15 @@ import ( ) func PrefixMovedPermanently(before, after string) rsvp.HandlerFunc { - return func(h http.Header, r *http.Request) rsvp.Response { + return func(w rsvp.ResponseWriter, r *http.Request) rsvp.Body { suffix := strings.TrimPrefix(r.RequestURI, before) - return rsvp.MovedPermanently(after + suffix) + return rsvp.Blank().StatusMovedPermanently(after + suffix) } } func PrefixPermanentRedirect(before, after string) rsvp.HandlerFunc { - return func(h http.Header, r *http.Request) rsvp.Response { + return func(w rsvp.ResponseWriter, r *http.Request) rsvp.Body { suffix := strings.TrimPrefix(r.RequestURI, before) - return rsvp.PermanentRedirect(after + suffix) + return rsvp.Blank().StatusPermanentRedirect(after + suffix) } } diff --git a/http/mux/private.go b/http/mux/private.go index 686e72b..173af81 100644 --- a/http/mux/private.go +++ b/http/mux/private.go @@ -13,8 +13,9 @@ import ( ) type Private struct { - inner *rsvp.ServeMux + inner *http.ServeMux store *session.Store + rsvp rsvp.Config } func (m *Private) GetSession(r *http.Request) *session.Session { @@ -22,12 +23,12 @@ func (m *Private) GetSession(r *http.Request) *session.Session { return session.New(innerSession) } -func newPrivate(store *session.Store, cfg *rsvp.Config) Private { - mux := rsvp.NewServeMux() - mux.Config = cfg +func newPrivate(store *session.Store, cfg rsvp.Config) Private { + mux := http.NewServeMux() return Private{ inner: mux, store: store, + rsvp: cfg, } } @@ -36,35 +37,62 @@ func (m *Private) ServeHTTP(w http.ResponseWriter, r *http.Request) { } func expectAppSession(next handler.Private) handler.PublicFunc { - return func(session *session.Session, h http.Header, r *http.Request) rsvp.Response { + return func(session *session.Session, w rsvp.ResponseWriter, r *http.Request) rsvp.Body { sessionKey, ok := session.GetString("sessionKey") if !ok { log.Printf("Failed to get key from session\n") - return response.Error(http.StatusInternalServerError, "Something went wrong.") + return response.Error("Something went wrong.").StatusInternalServerError() } appSession, err := lishwist.SessionFromKey(sessionKey) if err != nil { log.Printf("Failed to get session by key %v: %s\n", sessionKey, err) - return response.Error(http.StatusInternalServerError, "Something went wrong.") + return response.Error("Something went wrong.").StatusInternalServerError() } if appSession == nil { log.Printf("Session not found under key: %s\n", sessionKey) - return response.Error(http.StatusInternalServerError, "Something went wrong.") + return response.Error("Something went wrong.").StatusInternalServerError() } - return next.ServeHTTP(appSession, session, h, r) + return next.ServeHTTP(appSession, session, w, r) } } -func (m *Private) handleWithSession(pattern string, hnd handler.PublicFunc) { - getSessionMiddleware := rsvp.MiddleHandlerFunc(func(w http.ResponseWriter, r *http.Request) rsvp.Response { +type sessionResponseWriter struct { + http.ResponseWriter + s *session.Session + r *http.Request +} + +func (w sessionResponseWriter) WriteHeader(statusCode int) { + if w.s.WasWritten() { + err := w.s.Save(w.r, w) + if err != nil { + log.Printf("Failed to save session: %s", err) + } + } + + w.ResponseWriter.WriteHeader(statusCode) +} + +func (m *Private) handleWithSession(pattern string, next handler.PublicFunc) { + getSessionMiddleware := func(w http.ResponseWriter, r *http.Request) { s := m.GetSession(r) - saveIfWritten := s.SaveIfWrittenMiddleware(hnd) - return saveIfWritten(w, r) - }) - beforeWrite := rsvp.MiddlewareBeforeWriteResponse(m.inner.Config, getSessionMiddleware) - m.inner.Std.HandleFunc(pattern, beforeWrite) + + rw := sessionResponseWriter{ + ResponseWriter: w, + s: s, + r: r, + } + + err := rsvp.Write(rw, m.rsvp, r, func(w rsvp.ResponseWriter, r *http.Request) rsvp.Body { + return next(s, w, r) + }) + if err != nil { + log.Printf("rsvp failed to write a response: %s", err) + } + } + m.inner.HandleFunc(pattern, getSessionMiddleware) } func (m *Private) Handle(pattern string, handler handler.Private) { diff --git a/http/mux/public.go b/http/mux/public.go index 17f0cd9..ed19855 100644 --- a/http/mux/public.go +++ b/http/mux/public.go @@ -11,7 +11,7 @@ type Public struct { Private } -func newPublic(store *session.Store, cfg *rsvp.Config) Public { +func newPublic(store *session.Store, cfg rsvp.Config) Public { return Public{ newPrivate(store, cfg), } diff --git a/http/mux/serve.go b/http/mux/serve.go index 5d33028..4e6c168 100644 --- a/http/mux/serve.go +++ b/http/mux/serve.go @@ -40,35 +40,41 @@ func (m *Serve) ServeHTTP(w http.ResponseWriter, r *http.Request) { return } - handler := rsvp.MiddlewareBeforeWriteResponse(&m.rsvpCfg, rsvp.MiddleHandlerFunc(func(w http.ResponseWriter, r *http.Request) rsvp.Response { - sessionWriter := s.SaveIfWrittenMiddleware(func(s *session.Session, h http.Header, r *http.Request) rsvp.Response { - return routing.LoginWithCreds(username, password, s, h, r) - }) + loginHandler := func(s *session.Session, r *http.Request) rsvp.Body { + return routing.LoginWithCreds(username, password, s, r.URL.Path) + } - return sessionWriter(w, r) - })) + rw := sessionResponseWriter{ + ResponseWriter: w, + s: s, + r: r, + } - handler(w, r) + handler := rsvp.AdaptHandlerFunc(m.rsvpCfg, func(w rsvp.ResponseWriter, r *http.Request) rsvp.Body { + return loginHandler(s, r) + }) + + handler(rw, r) } func New(store *session.Store) *Serve { - cfg := *rsvp.DefaultConfig() + cfg := rsvp.Config{} cfg.HtmlTemplate = templates.Template cfg.TextTemplate = text.Template sv := Serve{ store: store, rsvpCfg: cfg, - Public: newPublic(store, &cfg), - Private: newPrivate(store, &cfg), + Public: newPublic(store, cfg), + Private: newPrivate(store, cfg), } return &sv } func (m *Serve) HandleFunc(pattern string, hnd rsvp.HandlerFunc) { - m.Public.HandleFunc(pattern, func(s *session.Session, h http.Header, r *http.Request) rsvp.Response { - return hnd(h, r) + m.Public.HandleFunc(pattern, func(s *session.Session, w rsvp.ResponseWriter, r *http.Request) rsvp.Body { + return hnd(w, r) }) - m.Private.HandleFunc(pattern, func(app *lishwist.Session, s *session.Session, h http.Header, r *http.Request) rsvp.Response { - return hnd(h, r) + m.Private.HandleFunc(pattern, func(app *lishwist.Session, s *session.Session, w rsvp.ResponseWriter, r *http.Request) rsvp.Body { + return hnd(w, r) }) } diff --git a/http/response/response.go b/http/response/response.go index 5d53bf9..fec25b9 100644 --- a/http/response/response.go +++ b/http/response/response.go @@ -3,13 +3,12 @@ package response import ( "fmt" "lishwist/http/templates" - "net/http" "github.com/Teajey/rsvp" ) -func NotFound() rsvp.Response { - return Error(http.StatusNotFound, "Page not found") +func NotFound() rsvp.Body { + return Error("Page not found").StatusNotFound() } type errorProps struct { @@ -17,20 +16,19 @@ type errorProps struct { Message string } -func Error(status int, format string, a ...any) rsvp.Response { - return rsvp.Response{ - Body: errorProps{ +func Error(format string, a ...any) rsvp.Body { + return rsvp.Body{ + Data: errorProps{ Message: fmt.Sprintf(format, a...), Navbar: templates.DefaultNavCollapse(), }, - Status: status, TemplateName: "error_page.gotmpl", } } -func Data(templateName string, body any) rsvp.Response { - return rsvp.Response{ - Body: body, +func Data(templateName string, body any) rsvp.Body { + return rsvp.Body{ + Data: body, TemplateName: templateName, } } diff --git a/http/routing/account.go b/http/routing/account.go index cf3771f..2f0a3ef 100644 --- a/http/routing/account.go +++ b/http/routing/account.go @@ -64,7 +64,7 @@ func NewAccountProps(username string, passwordFromAdmin bool, passwordVal, confi } } -func Account(app *lishwist.Session, session *session.Session, h http.Header, r *http.Request) rsvp.Response { +func Account(app *lishwist.Session, session *session.Session, w rsvp.ResponseWriter, r *http.Request) rsvp.Body { user := app.User() props := NewAccountProps(user.Name, user.PasswordFromAdmin, "", "") @@ -79,17 +79,17 @@ func Account(app *lishwist.Session, session *session.Session, h http.Header, r * return response.Data("account.gotmpl", props) } -func AccountPost(app *lishwist.Session, session *session.Session, h http.Header, r *http.Request) rsvp.Response { +func AccountPost(app *lishwist.Session, session *session.Session, w rsvp.ResponseWriter, r *http.Request) rsvp.Body { user := app.User() err := r.ParseForm() if err != nil { - return response.Error(http.StatusBadRequest, "Failed to parse form") + return response.Error("Failed to parse form").StatusBadRequest() } intent := r.Form.Get("intent") if intent != "set_password" { - return response.Error(http.StatusBadRequest, "Invalid intent %q", intent) + return response.Error("Invalid intent %q", intent).StatusBadRequest() } newPassword := r.Form.Get("new_password") @@ -103,7 +103,7 @@ func AccountPost(app *lishwist.Session, session *session.Session, h http.Header, if !valid { log.Printf("Invalid account submission: %#v\n", account) session.FlashSet(&account) - return rsvp.SeeOther("/account", account) + return rsvp.Data(account).StatusSeeOther("/account") } err = user.SetPassword(newPassword) @@ -111,11 +111,11 @@ func AccountPost(app *lishwist.Session, session *session.Session, h http.Header, account.GeneralError = "Something went wrong." log.Printf("Set password failed: %s\n", err) session.FlashSet(&account) - return rsvp.SeeOther("/account", account) + return rsvp.Data(account).StatusSeeOther("/account") } session.RemoveValue("sessionKey") session.FlashSet(&props.Login{SuccessfulSetPassword: true}) - return rsvp.SeeOther("/", "Set password successful!") + return rsvp.Data("Set password successful!").StatusSeeOther("/") } diff --git a/http/routing/config.go b/http/routing/config.go index 7ffd50e..a651cd2 100644 --- a/http/routing/config.go +++ b/http/routing/config.go @@ -14,12 +14,12 @@ type HealthProps struct { Config env.Config } -func Health(app *lishwist.Session, session *session.Session, h http.Header, r *http.Request) rsvp.Response { +func Health(app *lishwist.Session, session *session.Session, w rsvp.ResponseWriter, r *http.Request) rsvp.Body { if app.Admin() == nil { - return rsvp.Ok() + return rsvp.Blank() } - return rsvp.Response{Body: HealthProps{ + return rsvp.Body{Data: HealthProps{ GitVersion: env.GitVersion, Config: env.Configuration, }} diff --git a/http/routing/events.go b/http/routing/events.go index 3473be1..b5ba0cf 100644 --- a/http/routing/events.go +++ b/http/routing/events.go @@ -14,7 +14,7 @@ type Events struct { Events []lishwist.Event `xml:"Event"` } -func EventList(app *lishwist.Session, session *session.Session, h http.Header, r *http.Request) rsvp.Response { +func EventList(app *lishwist.Session, session *session.Session, w rsvp.ResponseWriter, r *http.Request) rsvp.Body { admin := app.Admin() if admin == nil { log.Println("Attempt to access EventList by non-admin. Responding 404 Not Found.") @@ -24,7 +24,7 @@ func EventList(app *lishwist.Session, session *session.Session, h http.Header, r events, err := admin.ListEvents() if err != nil { log.Printf("Admin failed to ListEvents: %s\n", err) - return response.Error(http.StatusInternalServerError, "Failed to get events: %s", err) + return response.Error("Failed to get events: %s", err).StatusInternalServerError() } return response.Data("", Events{Events: events}) diff --git a/http/routing/foreign_wishlist.go b/http/routing/foreign_wishlist.go index d5e7cd9..21332d2 100644 --- a/http/routing/foreign_wishlist.go +++ b/http/routing/foreign_wishlist.go @@ -20,24 +20,24 @@ type foreignWishlistProps struct { Gifts []lishwist.Wish } -func ForeignWishlist(app *lishwist.Session, session *session.Session, h http.Header, r *http.Request) rsvp.Response { +func ForeignWishlist(app *lishwist.Session, session *session.Session, w rsvp.ResponseWriter, r *http.Request) rsvp.Body { userReference := file.StemPathValue(r, "userReference") user := app.User() if user.Reference == userReference { - return rsvp.Found("/", "You're not allowed to view your own wishlist!") + return rsvp.Data("You're not allowed to view your own wishlist!").StatusFound("/") } otherUser, err := lishwist.GetUserByReference(userReference) if err != nil { log.Printf("Couldn't get user by reference %q: %s\n", userReference, err) - return response.Error(http.StatusInternalServerError, "An error occurred while fetching this user :(") + return response.Error("An error occurred while fetching this user :(").StatusInternalServerError() } if otherUser == nil { - return response.Error(http.StatusInternalServerError, "User not found") + return response.Error("User not found").StatusInternalServerError() } wishes, err := app.GetOthersWishes(userReference) if err != nil { log.Printf("%q couldn't get wishes of other user %q: %s\n", user.Name, otherUser.Name, err) - return response.Error(http.StatusInternalServerError, "An error occurred while fetching this user :(") + return response.Error("An error occurred while fetching this user :(").StatusInternalServerError() } p := foreignWishlistProps{Navbar: templates.UserNavCollapse(user.Name, user.PasswordFromAdmin), CurrentUserId: user.Id, Username: otherUser.Name, Gifts: wishes} return response.Data("foreign_wishlist.gotmpl", p) @@ -50,20 +50,20 @@ type publicWishlistProps struct { Referrer string } -func PublicWishlist(s *session.Session, h http.Header, r *http.Request) rsvp.Response { +func PublicWishlist(s *session.Session, w rsvp.ResponseWriter, r *http.Request) rsvp.Body { userReference := file.StemPathValue(r, "userReference") otherUser, err := lishwist.GetUserByReference(userReference) if err != nil { log.Printf("Couldn't get user by reference %q on public wishlist: %s\n", userReference, err) - return response.Error(http.StatusInternalServerError, "An error occurred while fetching this user :(") + return response.Error("An error occurred while fetching this user :(").StatusInternalServerError() } if otherUser == nil { - return response.Error(http.StatusInternalServerError, "User not found") + return response.Error("User not found").StatusInternalServerError() } giftCount, err := otherUser.WishCount() if err != nil { log.Printf("Couldn't get wishes of user %q on public wishlist: %s\n", otherUser.Name, err) - return response.Error(http.StatusInternalServerError, "An error occurred while fetching this user :(") + return response.Error("An error occurred while fetching this user :(").StatusInternalServerError() } p := publicWishlistProps{Navbar: templates.DefaultNavCollapse(), Username: otherUser.Name, GiftCount: giftCount, Referrer: r.URL.Path} return response.Data("public_foreign_wishlist.gotmpl", p) diff --git a/http/routing/groups.go b/http/routing/groups.go index e0bffd0..6ed0af2 100644 --- a/http/routing/groups.go +++ b/http/routing/groups.go @@ -21,14 +21,14 @@ type GroupProps struct { Referrer string } -func AdminGroup(app *lishwist.Session, h http.Header, r *http.Request) rsvp.Response { +func AdminGroup(app *lishwist.Session, w rsvp.ResponseWriter, r *http.Request) rsvp.Body { reference := file.StemPathValue(r, "groupReference") group, err := lishwist.GetGroupByReference(reference) if err != nil { - return response.Error(http.StatusInternalServerError, "Couldn't get group: %s", err) + return response.Error("Couldn't get group: %s", err).StatusInternalServerError() } if group == nil { - return response.Error(http.StatusNotFound, "Group not found") + return response.Error("Group not found").StatusNotFound() } user := app.User() if !user.IsAdmin { @@ -42,19 +42,19 @@ func AdminGroup(app *lishwist.Session, h http.Header, r *http.Request) rsvp.Resp return response.Data("group_page.gotmpl", p) } -func Group(app *lishwist.Session, session *session.Session, h http.Header, r *http.Request) rsvp.Response { +func Group(app *lishwist.Session, session *session.Session, w rsvp.ResponseWriter, r *http.Request) rsvp.Body { user := app.User() if user.IsAdmin { - return AdminGroup(app, h, r) + return AdminGroup(app, w, r) } groupReference := file.StemPathValue(r, "groupReference") group, err := app.GetGroupByReference(groupReference) if err != nil { log.Printf("Couldn't get group: %s\n", err) - return response.Error(http.StatusInternalServerError, "An error occurred while fetching this group :(") + return response.Error("An error occurred while fetching this group :(").StatusInternalServerError() } if group == nil { - return response.Error(http.StatusNotFound, "Group not found. (It might be because you're not a member)") + return response.Error("Group not found. (It might be because you're not a member)").StatusNotFound() } index := group.MemberIndex(user.Id) group.Members = slices.Delete(group.Members, index, index+1) @@ -65,15 +65,15 @@ func Group(app *lishwist.Session, session *session.Session, h http.Header, r *ht return response.Data("group_page.gotmpl", p) } -func PublicGroup(s *session.Session, h http.Header, r *http.Request) rsvp.Response { +func PublicGroup(s *session.Session, w rsvp.ResponseWriter, r *http.Request) rsvp.Body { groupReference := file.StemPathValue(r, "groupReference") group, err := lishwist.GetGroupByReference(groupReference) if err != nil { log.Printf("Couldn't get group: %s\n", err) - return response.Error(http.StatusInternalServerError, "An error occurred while fetching this group :(") + return response.Error("An error occurred while fetching this group :(").StatusInternalServerError() } if group == nil { - return response.Error(http.StatusNotFound, "Group not found") + return response.Error("Group not found").StatusNotFound() } p := GroupProps{ Navbar: templates.DefaultNavCollapse(), @@ -83,7 +83,7 @@ func PublicGroup(s *session.Session, h http.Header, r *http.Request) rsvp.Respon return response.Data("public_group_page.gotmpl", p) } -func GroupPost(app *lishwist.Session, session *session.Session, h http.Header, r *http.Request) rsvp.Response { +func GroupPost(app *lishwist.Session, session *session.Session, w rsvp.ResponseWriter, r *http.Request) rsvp.Body { admin := app.Admin() if admin == nil { return response.NotFound() @@ -91,7 +91,7 @@ func GroupPost(app *lishwist.Session, session *session.Session, h http.Header, r err := r.ParseForm() if err != nil { - return response.Error(http.StatusBadRequest, "Failed to parse form") + return response.Error("Failed to parse form").StatusBadRequest() } var group *lishwist.Group @@ -104,27 +104,27 @@ func GroupPost(app *lishwist.Session, session *session.Session, h http.Header, r if name != "" { createdGroup, err := admin.CreateGroup(name, reference) if err != nil { - return response.Error(http.StatusInternalServerError, "Failed to create group: %s", err) + return response.Error("Failed to create group: %s", err).StatusInternalServerError() } group = createdGroup } else { existingGroup, err := lishwist.GetGroupByReference(reference) if err != nil { - return response.Error(http.StatusInternalServerError, "Failed to get group: %s", err) + return response.Error("Failed to get group: %s", err).StatusInternalServerError() } if existingGroup == nil { - return response.Error(http.StatusNotFound, "Group not found: %s", err) + return response.Error("Group not found: %s", err).StatusNotFound() } group = existingGroup for _, userId := range removeUsers { index := group.MemberIndex(userId) if index == -1 { - return response.Error(http.StatusBadRequest, "Group %q does not contain a user with id %s", reference, userId) + return response.Error("Group %q does not contain a user with id %s", reference, userId).StatusBadRequest() } err = admin.RemoveUserFromGroup(userId, group.Id) if err != nil { - return response.Error(http.StatusInternalServerError, "On group %q failed to remove user with id %s: %s", reference, userId, err) + return response.Error("On group %q failed to remove user with id %s: %s", reference, userId, err).StatusInternalServerError() } group.Members = slices.Delete(group.Members, index, index+1) } @@ -133,14 +133,14 @@ func GroupPost(app *lishwist.Session, session *session.Session, h http.Header, r for _, userId := range addUsers { user, err := admin.GetUser(userId) if err != nil { - return response.Error(http.StatusInternalServerError, "Groups exists, but a user with id %s could not be fetched: %s", userId, err) + return response.Error("Groups exists, but a user with id %s could not be fetched: %s", userId, err).StatusInternalServerError() } if user == nil { - return response.Error(http.StatusInternalServerError, "Groups exists, but a user with id %s does not exist", userId) + return response.Error("Groups exists, but a user with id %s does not exist", userId).StatusInternalServerError() } err = admin.AddUserToGroup(user.Id, group.Id) if err != nil { - return response.Error(http.StatusInternalServerError, "Groups exists, but failed to add user with id %s: %s", userId, err) + return response.Error("Groups exists, but failed to add user with id %s: %s", userId, err).StatusInternalServerError() } group.Members = append(group.Members, *user) } @@ -153,7 +153,7 @@ type GroupList struct { Groups []lishwist.Group `xml:"Group"` } -func Groups(app *lishwist.Session, session *session.Session, h http.Header, r *http.Request) rsvp.Response { +func Groups(app *lishwist.Session, session *session.Session, w rsvp.ResponseWriter, r *http.Request) rsvp.Body { admin := app.Admin() if admin == nil { return response.NotFound() @@ -161,7 +161,7 @@ func Groups(app *lishwist.Session, session *session.Session, h http.Header, r *h groups, err := admin.ListGroups() if err != nil { - return response.Error(http.StatusInternalServerError, "Failed to get groups: %s", err) + return response.Error("Failed to get groups: %s", err).StatusInternalServerError() } return response.Data("", GroupList{Groups: groups}) diff --git a/http/routing/home.go b/http/routing/home.go index 22e652d..eb5a71d 100644 --- a/http/routing/home.go +++ b/http/routing/home.go @@ -20,22 +20,22 @@ type HomeProps struct { Groups []lishwist.Group } -func Home(app *lishwist.Session, session *session.Session, h http.Header, r *http.Request) rsvp.Response { +func Home(app *lishwist.Session, session *session.Session, w rsvp.ResponseWriter, r *http.Request) rsvp.Body { gifts, err := app.GetWishes() if err != nil { log.Printf("Failed to get gifts: %s\n", err) - return response.Error(http.StatusInternalServerError, "An error occurred while fetching your wishlist :(") + return response.Error("An error occurred while fetching your wishlist :(").StatusInternalServerError() } user := app.User() todo, err := user.GetTodo() if err != nil { log.Printf("Failed to get todo: %s\n", err) - return response.Error(http.StatusInternalServerError, "An error occurred while fetching your wishlist :(") + return response.Error("An error occurred while fetching your wishlist :(").StatusInternalServerError() } groups, err := app.GetGroups() if err != nil { log.Printf("Failed to get groups: %s\n", err) - return response.Error(http.StatusInternalServerError, "An error occurred while fetching your wishlist :(") + return response.Error("An error occurred while fetching your wishlist :(").StatusInternalServerError() } p := HomeProps{Navbar: templates.NavCollapse{ User: &templates.User{Name: user.Name, CopyList: &templates.CopyList{Domain: env.Configuration.HostUrl, Reference: user.Reference}}, @@ -44,18 +44,18 @@ func Home(app *lishwist.Session, session *session.Session, h http.Header, r *htt return response.Data("home.gotmpl", p) } -func HomePost(app *lishwist.Session, session *session.Session, h http.Header, r *http.Request) rsvp.Response { +func HomePost(app *lishwist.Session, session *session.Session, w rsvp.ResponseWriter, r *http.Request) rsvp.Body { err := r.ParseForm() if err != nil { - return response.Error(http.StatusBadRequest, "Failed to parse form") + return response.Error("Failed to parse form").StatusBadRequest() } switch r.Form.Get("intent") { case "add_idea": - return WishlistAdd(app, h, r) + return WishlistAdd(app, w, r) case "delete_idea": - return WishlistDelete(app, h, r) + return WishlistDelete(app, w, r) default: - return TodoUpdate(app, h, r) + return TodoUpdate(app, w, r) } } diff --git a/http/routing/login.go b/http/routing/login.go index d4d8506..cd3925c 100644 --- a/http/routing/login.go +++ b/http/routing/login.go @@ -16,7 +16,7 @@ import ( "github.com/Teajey/rsvp" ) -func Login(s *session.Session, h http.Header, r *http.Request) rsvp.Response { +func Login(s *session.Session, w rsvp.ResponseWriter, r *http.Request) rsvp.Body { login := props.NewLogin("", "") flash := s.FlashGet() @@ -33,13 +33,13 @@ func Login(s *session.Session, h http.Header, r *http.Request) rsvp.Response { } body := htmlx.Form[*props.Login]{Method: "POST", Elements: login} - return rsvp.Response{TemplateName: "login.gotmpl", Body: body} + return rsvp.Body{TemplateName: "login.gotmpl", Data: body} } -func LoginPost(session *session.Session, h http.Header, r *http.Request) rsvp.Response { +func LoginPost(session *session.Session, w rsvp.ResponseWriter, r *http.Request) rsvp.Body { err := r.ParseForm() if err != nil { - return response.Error(http.StatusBadRequest, "Failed to parse form") + return response.Error("Failed to parse form").StatusBadRequest() } username := r.Form.Get("username") @@ -53,17 +53,17 @@ func LoginPost(session *session.Session, h http.Header, r *http.Request) rsvp.Re } } -func LoginWithCreds(username, password string, session *session.Session, referrer string) rsvp.Response { +func LoginWithCreds(username, password string, session *session.Session, referrer string) rsvp.Body { login := props.NewLogin(username, password) - resp := rsvp.SeeOther(referrer, login) + resp := rsvp.Data(login).StatusSeeOther(referrer) valid := login.Validate() login.Password.Value = "" if !valid { session.FlashSet(&login) log.Printf("Invalid login submission: %#v\n", login) - resp.Body = htmlx.Form[*props.Login]{Method: "POST", Elements: login} + resp.Data = htmlx.Form[*props.Login]{Method: "POST", Elements: login} return resp } @@ -75,13 +75,13 @@ func LoginWithCreds(username, password string, session *session.Session, referre login.GeneralError = "Username or password invalid. If you're having trouble accessing your account, you may want to consider asking the System Admin (Thomas) to reset your password" session.FlashSet(&login) log.Printf("Invalid credentials: %s: %#v\n", err, login) - resp.Body = htmlx.Form[*props.Login]{Method: "POST", Elements: login} + resp.Data = htmlx.Form[*props.Login]{Method: "POST", Elements: login} return resp default: login.GeneralError = "Something went wrong." session.FlashSet(&login) log.Printf("Login error: %s\n", err) - resp.Body = htmlx.Form[*props.Login]{Method: "POST", Elements: login} + resp.Data = htmlx.Form[*props.Login]{Method: "POST", Elements: login} return resp } } @@ -89,9 +89,9 @@ func LoginWithCreds(username, password string, session *session.Session, referre session.SetID("") session.SetValue("sessionKey", appSession.Key) - return rsvp.SeeOther(referrer, "Login successful!") + return rsvp.Data("Login successful!").StatusSeeOther(referrer) } -func LoginReferrer(s *session.Session, h http.Header, r *http.Request) rsvp.Response { - return rsvp.Found(fmt.Sprintf("/login?referrer=%s", r.URL.Path), nil) +func LoginReferrer(s *session.Session, w rsvp.ResponseWriter, r *http.Request) rsvp.Body { + return rsvp.Blank().StatusFound(fmt.Sprintf("/login?referrer=%s", r.URL.Path)) } diff --git a/http/routing/logout.go b/http/routing/logout.go index 16f92df..39a07c2 100644 --- a/http/routing/logout.go +++ b/http/routing/logout.go @@ -8,9 +8,9 @@ import ( "github.com/Teajey/rsvp" ) -func LogoutPost(app *lishwist.Session, s *session.Session, h http.Header, r *http.Request) rsvp.Response { +func LogoutPost(app *lishwist.Session, s *session.Session, w rsvp.ResponseWriter, r *http.Request) rsvp.Body { s.Options().MaxAge = 0 s.ClearValues() - return rsvp.SeeOther("/", "Logout successful") + return rsvp.Data("Logout successful").StatusSeeOther("/") } diff --git a/http/routing/not_found.go b/http/routing/not_found.go index b65754c..03312ff 100644 --- a/http/routing/not_found.go +++ b/http/routing/not_found.go @@ -8,6 +8,6 @@ import ( "github.com/Teajey/rsvp" ) -func NotFound(h http.Header, r *http.Request) rsvp.Response { - return response.Error(http.StatusNotFound, "Page not found") +func NotFound(w rsvp.ResponseWriter, r *http.Request) rsvp.Body { + return response.Error("Page not found").StatusNotFound() } diff --git a/http/routing/register.go b/http/routing/register.go index c776eac..9562e13 100644 --- a/http/routing/register.go +++ b/http/routing/register.go @@ -12,7 +12,7 @@ import ( "github.com/Teajey/rsvp" ) -func Register(session *session.Session, h http.Header, r *http.Request) rsvp.Response { +func Register(session *session.Session, w rsvp.ResponseWriter, r *http.Request) rsvp.Body { register := props.NewRegister("", "", "") flash := session.FlashGet() @@ -29,7 +29,7 @@ func Register(session *session.Session, h http.Header, r *http.Request) rsvp.Res return response.Data("register.gotmpl", register) } -func RegisterPost(s *session.Session, h http.Header, r *http.Request) rsvp.Response { +func RegisterPost(s *session.Session, w rsvp.ResponseWriter, r *http.Request) rsvp.Body { var confirmPassword string username, newPassword, ok := r.BasicAuth() if ok { @@ -37,7 +37,7 @@ func RegisterPost(s *session.Session, h http.Header, r *http.Request) rsvp.Respo } else { err := r.ParseForm() if err != nil { - return response.Error(http.StatusBadRequest, "Failed to parse form") + return response.Error("Failed to parse form").StatusBadRequest() } username = r.Form.Get("username") @@ -53,7 +53,7 @@ func RegisterPost(s *session.Session, h http.Header, r *http.Request) rsvp.Respo if !valid { s.FlashSet(®ister) log.Printf("Invalid register submission: %#v\n", register) - return rsvp.SeeOther(r.URL.Path, register) + return rsvp.Data(register).StatusSeeOther(r.URL.Path) } _, err := lishwist.Register(username, newPassword) @@ -65,14 +65,14 @@ func RegisterPost(s *session.Session, h http.Header, r *http.Request) rsvp.Respo } s.FlashSet(®ister) log.Printf("Registration failed: %s\n", err) - return rsvp.SeeOther(r.URL.Path, register) + return rsvp.Data(register).StatusSeeOther(r.URL.Path) } s.FlashSet(&props.Login{SuccessfulRegistration: true}) referrer := r.URL.Query().Get("referrer") if referrer != "" { - return rsvp.SeeOther("/login?="+referrer, "Registration successful!") + return rsvp.Data("Registration successful!").StatusSeeOther("/login?=" + referrer) } else { - return rsvp.SeeOther("/", "Registration successful!") + return rsvp.Data("Registration successful!").StatusSeeOther("/") } } diff --git a/http/routing/todo.go b/http/routing/todo.go index 91a7823..6ee193e 100644 --- a/http/routing/todo.go +++ b/http/routing/todo.go @@ -10,10 +10,10 @@ import ( "lishwist/http/response" ) -func TodoUpdate(app *lishwist.Session, h http.Header, r *http.Request) rsvp.Response { +func TodoUpdate(app *lishwist.Session, w rsvp.ResponseWriter, r *http.Request) rsvp.Body { err := r.ParseForm() if err != nil { - return response.Error(http.StatusBadRequest, "Failed to parse form") + return response.Error("Failed to parse form").StatusBadRequest() } switch r.Form.Get("intent") { @@ -22,17 +22,17 @@ func TodoUpdate(app *lishwist.Session, h http.Header, r *http.Request) rsvp.Resp err := app.ClaimWishes([]string{}, unclaims) if err != nil { log.Printf("%s\n", err) - return response.Error(http.StatusInternalServerError, "Failed to update claim...") + return response.Error("Failed to update claim...").StatusInternalServerError() } case "complete_todo": claims := r.Form["gift"] err := app.CompleteWishes(claims) if err != nil { log.Printf("%s\n", err) - return response.Error(http.StatusInternalServerError, "Failed to complete gifts...") + return response.Error("Failed to complete gifts...").StatusInternalServerError() } default: - return response.Error(http.StatusBadRequest, "Invalid intent") + return response.Error("Invalid intent").StatusBadRequest() } - return rsvp.SeeOther("/", "Update successful") + return rsvp.Data("Update successful").StatusSeeOther("/") } diff --git a/http/routing/users.go b/http/routing/users.go index 7f4730c..315486d 100644 --- a/http/routing/users.go +++ b/http/routing/users.go @@ -16,7 +16,7 @@ type UserList struct { Users []lishwist.User `xml:"User"` } -func Users(app *lishwist.Session, session *session.Session, h http.Header, r *http.Request) rsvp.Response { +func Users(app *lishwist.Session, session *session.Session, w rsvp.ResponseWriter, r *http.Request) rsvp.Body { admin := app.Admin() if admin == nil { return response.NotFound() @@ -24,13 +24,13 @@ func Users(app *lishwist.Session, session *session.Session, h http.Header, r *ht users, err := admin.ListUsers() if err != nil { - return response.Error(http.StatusInternalServerError, "Failed to get users: %s", err) + return response.Error("Failed to get users: %s", err).StatusInternalServerError() } return response.Data("", UserList{Users: users}) } -func User(app *lishwist.Session, session *session.Session, h http.Header, r *http.Request) rsvp.Response { +func User(app *lishwist.Session, session *session.Session, w rsvp.ResponseWriter, r *http.Request) rsvp.Body { admin := app.Admin() if admin == nil { return response.NotFound() @@ -40,16 +40,16 @@ func User(app *lishwist.Session, session *session.Session, h http.Header, r *htt user, err := lishwist.GetUserByReference(reference) if err != nil { - return response.Error(http.StatusInternalServerError, "Failed to get user: %s", err) + return response.Error("Failed to get user: %s", err).StatusInternalServerError() } if user == nil { - return response.Error(http.StatusNotFound, "User not found") + return response.Error("User not found").StatusNotFound() } return response.Data("", user) } -func UserPost(app *lishwist.Session, session *session.Session, h http.Header, r *http.Request) rsvp.Response { +func UserPost(app *lishwist.Session, session *session.Session, w rsvp.ResponseWriter, r *http.Request) rsvp.Body { admin := app.Admin() if admin == nil { return response.NotFound() @@ -57,7 +57,7 @@ func UserPost(app *lishwist.Session, session *session.Session, h http.Header, r err := r.ParseForm() if err != nil { - return response.Error(http.StatusBadRequest, "Failed to parse form") + return response.Error("Failed to parse form").StatusBadRequest() } reference := file.StemPathValue(r, "userReference") @@ -68,32 +68,32 @@ func UserPost(app *lishwist.Session, session *session.Session, h http.Header, r switch intent { case "delete": if reference == app.User().Reference { - return response.Error(http.StatusForbidden, "You cannot delete yourself.") + return response.Error("You cannot delete yourself.").StatusForbidden() } err = admin.UserSetLive(reference, false) if err != nil { - return response.Error(http.StatusInternalServerError, "Failed to delete user: %s", err) + return response.Error("Failed to delete user: %s", err).StatusInternalServerError() } case "rename": name := r.Form.Get("display_name") err = admin.RenameUser(reference, name) if err != nil { - return response.Error(http.StatusInternalServerError, "Failed to rename user: %s", err) + return response.Error("Failed to rename user: %s", err).StatusInternalServerError() } case "set_password": newPassword := r.Form.Get("new_password") err = admin.SetUserPassword(reference, newPassword) if err != nil { - return response.Error(http.StatusInternalServerError, "Failed to set new password: %s", err) + return response.Error("Failed to set new password: %s", err).StatusInternalServerError() } default: - return response.Error(http.StatusBadRequest, "Invalid intent %q", intent) + return response.Error("Invalid intent %q", intent).StatusBadRequest() } } user, err := lishwist.GetUserByReference(reference) if err != nil { - return response.Error(http.StatusInternalServerError, "Failed to get user: %s", err) + return response.Error("Failed to get user: %s", err).StatusInternalServerError() } return response.Data("", user) diff --git a/http/routing/wishlist.go b/http/routing/wishlist.go index 66ba1fa..cf98725 100644 --- a/http/routing/wishlist.go +++ b/http/routing/wishlist.go @@ -12,44 +12,44 @@ import ( "lishwist/http/session" ) -func WishlistAdd(app *lishwist.Session, h http.Header, r *http.Request) rsvp.Response { +func WishlistAdd(app *lishwist.Session, w rsvp.ResponseWriter, r *http.Request) rsvp.Body { err := r.ParseForm() if err != nil { - return response.Error(http.StatusBadRequest, "Failed to parse form") + return response.Error("Failed to parse form").StatusBadRequest() } newGiftName := r.Form.Get("gift_name") _, err = app.MakeWish(newGiftName) if err != nil { log.Printf("%s\n", err) - return response.Error(http.StatusInternalServerError, "Failed to add gift.") + return response.Error("Failed to add gift.").StatusInternalServerError() } - return rsvp.SeeOther("/", "Wish added!") + return rsvp.Data("Wish added!").StatusSeeOther("/") } -func WishlistDelete(app *lishwist.Session, h http.Header, r *http.Request) rsvp.Response { +func WishlistDelete(app *lishwist.Session, w rsvp.ResponseWriter, r *http.Request) rsvp.Body { err := r.ParseForm() if err != nil { - return response.Error(http.StatusBadRequest, "Failed to parse form") + return response.Error("Failed to parse form").StatusBadRequest() } targets := r.Form["gift"] err = app.RevokeWishes(targets...) if err != nil { log.Printf("%s\n", err) - return response.Error(http.StatusInternalServerError, "Failed to remove gifts.") + return response.Error("Failed to remove gifts.").StatusInternalServerError() } - return rsvp.SeeOther("/", "Wish deleted") + return rsvp.Data("Wish deleted").StatusSeeOther("/") } -func ForeignWishlistPost(app *lishwist.Session, session *session.Session, h http.Header, r *http.Request) rsvp.Response { +func ForeignWishlistPost(app *lishwist.Session, session *session.Session, w rsvp.ResponseWriter, r *http.Request) rsvp.Body { err := r.ParseForm() if err != nil { - return response.Error(http.StatusBadRequest, "Failed to parse form") + return response.Error("Failed to parse form").StatusBadRequest() } userReference := file.StemPathValue(r, "userReference") - resp := rsvp.SeeOther("/lists/"+userReference, "Update successful") + resp := rsvp.Data("Update successful").StatusSeeOther("/lists/" + userReference) intent := r.Form.Get("intent") switch intent { case "claim": @@ -58,28 +58,28 @@ func ForeignWishlistPost(app *lishwist.Session, session *session.Session, h http err := app.ClaimWishes(claims, unclaims) if err != nil { log.Printf("%s\n", err) - return response.Error(http.StatusInternalServerError, "Failed to update claim...") + return response.Error("Failed to update claim...").StatusInternalServerError() } - resp.Body = "Successfully claimed wishes" + resp.Data = "Successfully claimed wishes" case "complete": claims := r.Form["claimed"] err := app.CompleteWishes(claims) if err != nil { log.Printf("%s\n", err) - return response.Error(http.StatusInternalServerError, "Failed to complete gifts...") + return response.Error("Failed to complete gifts...").StatusInternalServerError() } - resp.Body = "Successfully completed wishes" + resp.Data = "Successfully completed wishes" case "add": wishName := r.Form.Get("gift_name") if wishName == "" { - return response.Error(http.StatusBadRequest, "Gift name not provided") + return response.Error("Gift name not provided").StatusBadRequest() } err := app.SuggestWishForUser(userReference, wishName) if err != nil { log.Printf("%s\n", err) - return response.Error(http.StatusInternalServerError, "Failed to add gift idea to other user...") + return response.Error("Failed to add gift idea to other user...").StatusInternalServerError() } - resp.Body = "Successfully added wishes" + resp.Data = "Successfully added wishes" case "delete": claims := r.Form["unclaimed"] unclaims := r.Form["claimed"] @@ -87,11 +87,11 @@ func ForeignWishlistPost(app *lishwist.Session, session *session.Session, h http err := app.RecindWishesForUser(gifts...) if err != nil { log.Printf("%s\n", err) - return response.Error(http.StatusInternalServerError, "Failed to remove gift idea for other user...") + return response.Error("Failed to remove gift idea for other user...").StatusInternalServerError() } - resp.Body = "Successfully removed wishes" + resp.Data = "Successfully removed wishes" default: - return response.Error(http.StatusBadRequest, "Invalid intent %q", intent) + return response.Error("Invalid intent %q", intent).StatusBadRequest() } return resp } diff --git a/http/server/server.go b/http/server/server.go index 5083a0b..2c6aec5 100644 --- a/http/server/server.go +++ b/http/server/server.go @@ -34,15 +34,15 @@ func Create(useSecureCookies bool) *mux.Serve { r.Public.HandleFunc("GET /lists/{userReference}", routing.PublicWishlist) r.Public.HandleFunc("GET /{$}", routing.Login) r.Public.HandleFunc("GET /", routing.LoginReferrer) - r.Public.HandleFunc("GET /{file}", func(s *session.Session, h http.Header, r *http.Request) rsvp.Response { + r.Public.HandleFunc("GET /{file}", func(s *session.Session, w rsvp.ResponseWriter, r *http.Request) rsvp.Body { stem := file.StemPathValue(r, "file") switch stem { case "login": - return routing.Login(s, h, r) + return routing.Login(s, w, r) case "register": - return routing.Register(s, h, r) + return routing.Register(s, w, r) } - return routing.LoginReferrer(s, h, r) + return routing.LoginReferrer(s, w, r) }) r.Public.HandleFunc("POST /register", routing.RegisterPost) @@ -53,22 +53,22 @@ func Create(useSecureCookies bool) *mux.Serve { r.Private.HandleFunc("GET /lists/{userReference}", routing.ForeignWishlist) r.Private.HandleFunc("GET /users/{userReference}", routing.User) r.Private.HandleFunc("GET /{$}", routing.Home) - r.Private.HandleFunc("GET /{file}", func(app *lishwist.Session, s *session.Session, h http.Header, r *http.Request) rsvp.Response { + r.Private.HandleFunc("GET /{file}", func(app *lishwist.Session, s *session.Session, w rsvp.ResponseWriter, r *http.Request) rsvp.Body { switch file.StemPathValue(r, "file") { case "account": - return routing.Account(app, s, h, r) + return routing.Account(app, s, w, r) case "events": - return routing.EventList(app, s, h, r) + return routing.EventList(app, s, w, r) case "groups": - return routing.Groups(app, s, h, r) + return routing.Groups(app, s, w, r) case "health": - return routing.Health(app, s, h, r) + return routing.Health(app, s, w, r) case "home": - return routing.Home(app, s, h, r) + return routing.Home(app, s, w, r) case "users": - return routing.Users(app, s, h, r) + return routing.Users(app, s, w, r) } - return routing.NotFound(h, r) + return routing.NotFound(w, r) }) r.Private.HandleFunc("POST /account", routing.AccountPost) diff --git a/http/session/session.go b/http/session/session.go index 98ee472..28c9984 100644 --- a/http/session/session.go +++ b/http/session/session.go @@ -1,10 +1,8 @@ package session import ( - "log" "net/http" - "github.com/Teajey/rsvp" "github.com/gorilla/sessions" ) @@ -17,21 +15,6 @@ func New(inner *sessions.Session) *Session { return &Session{inner: inner} } -func (s *Session) SaveIfWrittenMiddleware(next func(*Session, http.Header, *http.Request) rsvp.Response) rsvp.MiddleHandlerFunc { - return func(w http.ResponseWriter, r *http.Request) rsvp.Response { - response := next(s, w.Header(), r) - - if s.WasWritten() { - err := s.Save(r, w) - if err != nil { - log.Printf("Failed to save session: %s\n", err) - } - } - - return response - } -} - const flashKey = "_flash" func (s *Session) FlashGet() any {