Use RSVP 0.14.0 and refactor for middleware

This commit is contained in:
Teajey 2025-12-11 19:55:01 +09:00
parent 8c4334e063
commit f50446d04a
Signed by: Teajey
GPG Key ID: 970E790FE834A713
24 changed files with 184 additions and 151 deletions

View File

@ -5,7 +5,7 @@ go 1.23.3
toolchain go1.24.5
require (
github.com/Teajey/rsvp v0.13.1
github.com/Teajey/rsvp v0.14.0
github.com/gorilla/sessions v1.4.0
golang.org/x/crypto v0.39.0
)

View File

@ -1,5 +1,5 @@
github.com/Teajey/rsvp v0.13.1 h1:0lw+JosaWmdjSmXoKQYBRS9nptSZPInm60Y5GQ3llEU=
github.com/Teajey/rsvp v0.13.1/go.mod h1:z0L20VphVg+Ec2+hnpLFTG2MZTrWYFprav1kpxDba0Q=
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/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=

19
http/handler/public.go Normal file
View File

@ -0,0 +1,19 @@
package handler
import (
"net/http"
"github.com/Teajey/rsvp"
"lishwist/http/session"
)
type Public interface {
ServeHTTP(*session.Session, http.Header, *http.Request) rsvp.Response
}
type PublicFunc func(*session.Session, http.Header, *http.Request) rsvp.Response
func (f PublicFunc) ServeHTTP(s *session.Session, h http.Header, r *http.Request) rsvp.Response {
return f(s, h, r)
}

58
http/mux/private.go Normal file
View File

@ -0,0 +1,58 @@
package mux
import (
"log"
"net/http"
"github.com/Teajey/rsvp"
"lishwist/http/handler"
"lishwist/http/session"
"lishwist/http/templates"
"lishwist/http/templates/text"
)
type Private struct {
inner *rsvp.ServeMux
store *session.Store
}
func newPrivate(store *session.Store) *Private {
mux := rsvp.NewServeMux()
mux.Config.HtmlTemplate = templates.Template
mux.Config.TextTemplate = text.Template
return &Private{
inner: mux,
store: store,
}
}
func (m *Private) ServeHTTP(w http.ResponseWriter, r *http.Request) {
m.inner.ServeHTTP(w, r)
}
func (m *Private) GetSession(r *http.Request) *session.Session {
innerSession, _ := m.store.Get(r, "lishwist_user")
return session.New(innerSession)
}
func (m *Private) Handle(pattern string, handler handler.Public) {
m.inner.Std.HandleFunc(pattern, rsvp.MiddlewareBeforeWriteResponse(m.inner.Config, rsvp.MiddleHandlerFunc(func(w http.ResponseWriter, r *http.Request) rsvp.Response {
session := m.GetSession(r)
response := handler.ServeHTTP(session, w.Header(), r)
if session.WasWritten() {
err := session.Save(r, w)
if err != nil {
log.Printf("Failed to save session: %s\n", err)
}
}
return response
})))
}
func (m *Private) HandleFunc(pattern string, handler handler.PublicFunc) {
m.Handle(pattern, handler)
}

38
http/mux/serve.go Normal file
View File

@ -0,0 +1,38 @@
package mux
import (
"net/http"
"lishwist/http/handler"
"lishwist/http/session"
)
type Serve struct {
store *session.Store
Public *Private
Private *Private
}
func (s *Serve) ServeHTTP(w http.ResponseWriter, r *http.Request) {
session, _ := s.store.Get(r, "lishwist_user")
_, inSession := session.Values["sessionKey"]
if inSession {
s.Private.ServeHTTP(w, r)
} else {
s.Public.ServeHTTP(w, r)
}
}
func New(store *session.Store) *Serve {
return &Serve{
store: store,
Public: newPrivate(store),
Private: newPrivate(store),
}
}
func (r *Serve) HandleFunc(pattern string, handler handler.PublicFunc) {
r.Public.HandleFunc(pattern, handler)
r.Private.HandleFunc(pattern, handler)
}

View File

@ -1,58 +0,0 @@
package response
import (
"log"
"net/http"
"lishwist/http/session"
"lishwist/http/templates"
"lishwist/http/templates/text"
"github.com/Teajey/rsvp"
)
type ServeMux struct {
inner *rsvp.ServeMux
store *session.Store
}
func NewServeMux(store *session.Store) *ServeMux {
mux := rsvp.NewServeMux()
mux.Config.HtmlTemplate = templates.Template
mux.Config.TextTemplate = text.Template
return &ServeMux{
inner: mux,
store: store,
}
}
func (m *ServeMux) ServeHTTP(w http.ResponseWriter, r *http.Request) {
m.inner.ServeHTTP(w, r)
}
type Handler interface {
ServeHTTP(*Session, http.Header, *http.Request) rsvp.Response
}
type HandlerFunc func(*Session, http.Header, *http.Request) rsvp.Response
func (m *ServeMux) HandleFunc(pattern string, handler HandlerFunc) {
m.inner.MiddleHandleFunc(pattern, func(w http.ResponseWriter, r *http.Request) rsvp.Response {
session := m.GetSession(r)
response := handler(session, w.Header(), r)
if session.written {
err := session.inner.Save(r, w)
if err != nil {
log.Printf("Failed to save session: %s\n", err)
}
}
return response
})
}
func (m *ServeMux) Handle(pattern string, handler Handler) {
m.HandleFunc(pattern, handler.ServeHTTP)
}

View File

@ -1,10 +0,0 @@
package response
import (
"net/http"
)
func (m *ServeMux) GetSession(r *http.Request) *Session {
session, _ := m.store.Get(r, "lishwist_user")
return &Session{inner: session}
}

View File

@ -1,38 +0,0 @@
package router
import (
"net/http"
"lishwist/http/response"
"lishwist/http/session"
)
type VisibilityRouter struct {
store *session.Store
Public *response.ServeMux
Private *response.ServeMux
}
func (s *VisibilityRouter) ServeHTTP(w http.ResponseWriter, r *http.Request) {
session, _ := s.store.Get(r, "lishwist_user")
_, inSession := session.Values["sessionKey"]
if inSession {
s.Private.ServeHTTP(w, r)
} else {
s.Public.ServeHTTP(w, r)
}
}
func New(store *session.Store) *VisibilityRouter {
return &VisibilityRouter{
store: store,
Public: response.NewServeMux(store),
Private: response.NewServeMux(store),
}
}
func (r *VisibilityRouter) HandleFunc(pattern string, handler response.HandlerFunc) {
r.Public.HandleFunc(pattern, handler)
r.Private.HandleFunc(pattern, handler)
}

View File

@ -8,6 +8,7 @@ import (
"lishwist/http/props"
"lishwist/http/props/htmlx"
"lishwist/http/response"
"lishwist/http/session"
"lishwist/http/templates"
"github.com/Teajey/rsvp"
@ -63,7 +64,7 @@ func NewAccountProps(username string, passwordFromAdmin bool, passwordVal, confi
}
}
func Account(app *lishwist.Session, session *response.Session, h http.Header, r *http.Request) rsvp.Response {
func Account(app *lishwist.Session, session *session.Session, h http.Header, r *http.Request) rsvp.Response {
user := app.User()
props := NewAccountProps(user.Name, user.PasswordFromAdmin, "", "")
@ -78,7 +79,7 @@ func Account(app *lishwist.Session, session *response.Session, h http.Header, r
return response.Data("account.gotmpl", props)
}
func AccountPost(app *lishwist.Session, session *response.Session, h http.Header, r *http.Request) rsvp.Response {
func AccountPost(app *lishwist.Session, session *session.Session, h http.Header, r *http.Request) rsvp.Response {
user := app.User()
err := r.ParseForm()

View File

@ -3,7 +3,7 @@ package routing
import (
lishwist "lishwist/core"
"lishwist/http/env"
"lishwist/http/response"
"lishwist/http/session"
"net/http"
"github.com/Teajey/rsvp"
@ -14,7 +14,7 @@ type HealthProps struct {
Config env.Config
}
func Health(app *lishwist.Session, session *response.Session, h http.Header, r *http.Request) rsvp.Response {
func Health(app *lishwist.Session, session *session.Session, h http.Header, r *http.Request) rsvp.Response {
if app.Admin() == nil {
return rsvp.Ok()
}

View File

@ -4,15 +4,16 @@ import (
"log"
"net/http"
lishwist "lishwist/core"
"lishwist/http/response"
"github.com/Teajey/rsvp"
lishwist "lishwist/core"
"lishwist/http/handler"
"lishwist/http/response"
"lishwist/http/session"
)
func ExpectAppSession(next func(*lishwist.Session, *response.Session, http.Header, *http.Request) rsvp.Response) response.HandlerFunc {
return func(session *response.Session, h http.Header, r *http.Request) rsvp.Response {
func ExpectAppSession(next func(*lishwist.Session, *session.Session, http.Header, *http.Request) rsvp.Response) handler.PublicFunc {
return func(session *session.Session, h http.Header, r *http.Request) rsvp.Response {
sessionKey, ok := session.GetValue("sessionKey").(string)
if !ok {
log.Printf("Failed to get key from session\n")

View File

@ -3,6 +3,7 @@ package routing
import (
lishwist "lishwist/core"
"lishwist/http/response"
"lishwist/http/session"
"log"
"net/http"
@ -13,7 +14,7 @@ type Events struct {
Events []lishwist.Event `xml:"Event"`
}
func EventList(app *lishwist.Session, session *response.Session, h http.Header, r *http.Request) rsvp.Response {
func EventList(app *lishwist.Session, session *session.Session, h http.Header, r *http.Request) rsvp.Response {
admin := app.Admin()
if admin == nil {
log.Println("Attempt to access EventList by non-admin. Responding 404 Not Found.")

View File

@ -3,6 +3,7 @@ package routing
import (
lishwist "lishwist/core"
"lishwist/http/response"
"lishwist/http/session"
"lishwist/http/templates"
"log"
"net/http"
@ -18,7 +19,7 @@ type foreignWishlistProps struct {
Gifts []lishwist.Wish
}
func ForeignWishlist(app *lishwist.Session, session *response.Session, h http.Header, r *http.Request) rsvp.Response {
func ForeignWishlist(app *lishwist.Session, session *session.Session, h http.Header, r *http.Request) rsvp.Response {
userReference := r.PathValue("userReference")
user := app.User()
if user.Reference == userReference {
@ -47,7 +48,7 @@ type publicWishlistProps struct {
GiftCount int
}
func PublicWishlist(s *response.Session, h http.Header, r *http.Request) rsvp.Response {
func PublicWishlist(s *session.Session, h http.Header, r *http.Request) rsvp.Response {
userReference := r.PathValue("userReference")
otherUser, err := lishwist.GetUserByReference(userReference)
if err != nil {

View File

@ -8,6 +8,7 @@ import (
lishwist "lishwist/core"
"lishwist/http/response"
"lishwist/http/session"
"lishwist/http/templates"
"github.com/Teajey/rsvp"
@ -39,7 +40,7 @@ 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 *response.Session, h http.Header, r *http.Request) rsvp.Response {
func Group(app *lishwist.Session, session *session.Session, h http.Header, r *http.Request) rsvp.Response {
user := app.User()
if user.IsAdmin {
return AdminGroup(app, h, r)
@ -62,7 +63,7 @@ func Group(app *lishwist.Session, session *response.Session, h http.Header, r *h
return response.Data("group_page.gotmpl", p)
}
func PublicGroup(s *response.Session, h http.Header, r *http.Request) rsvp.Response {
func PublicGroup(s *session.Session, h http.Header, r *http.Request) rsvp.Response {
groupReference := r.PathValue("groupReference")
group, err := lishwist.GetGroupByReference(groupReference)
if err != nil {
@ -79,7 +80,7 @@ func PublicGroup(s *response.Session, h http.Header, r *http.Request) rsvp.Respo
return response.Data("public_group_page.gotmpl", p)
}
func GroupPost(app *lishwist.Session, session *response.Session, h http.Header, r *http.Request) rsvp.Response {
func GroupPost(app *lishwist.Session, session *session.Session, h http.Header, r *http.Request) rsvp.Response {
admin := app.Admin()
if admin == nil {
return response.NotFound()
@ -149,7 +150,7 @@ type GroupList struct {
Groups []lishwist.Group `xml:"Group"`
}
func Groups(app *lishwist.Session, session *response.Session, h http.Header, r *http.Request) rsvp.Response {
func Groups(app *lishwist.Session, session *session.Session, h http.Header, r *http.Request) rsvp.Response {
admin := app.Admin()
if admin == nil {
return response.NotFound()

View File

@ -7,6 +7,7 @@ import (
lishwist "lishwist/core"
"lishwist/http/env"
"lishwist/http/response"
"lishwist/http/session"
"lishwist/http/templates"
"github.com/Teajey/rsvp"
@ -19,7 +20,7 @@ type HomeProps struct {
Groups []lishwist.Group
}
func Home(app *lishwist.Session, session *response.Session, h http.Header, r *http.Request) rsvp.Response {
func Home(app *lishwist.Session, session *session.Session, h http.Header, r *http.Request) rsvp.Response {
gifts, err := app.GetWishes()
if err != nil {
log.Printf("Failed to get gifts: %s\n", err)
@ -43,7 +44,7 @@ func Home(app *lishwist.Session, session *response.Session, h http.Header, r *ht
return response.Data("home.gotmpl", p)
}
func HomePost(app *lishwist.Session, session *response.Session, h http.Header, r *http.Request) rsvp.Response {
func HomePost(app *lishwist.Session, session *session.Session, h http.Header, r *http.Request) rsvp.Response {
err := r.ParseForm()
if err != nil {
return response.Error(http.StatusBadRequest, "Failed to parse form")

View File

@ -10,11 +10,12 @@ import (
"lishwist/http/props"
"lishwist/http/props/htmlx"
"lishwist/http/response"
"lishwist/http/session"
"github.com/Teajey/rsvp"
)
func Login(s *response.Session, h http.Header, r *http.Request) rsvp.Response {
func Login(s *session.Session, h http.Header, r *http.Request) rsvp.Response {
login := props.NewLogin("", "")
flash := s.FlashGet()
@ -34,7 +35,7 @@ func Login(s *response.Session, h http.Header, r *http.Request) rsvp.Response {
return rsvp.Response{TemplateName: "login.gotmpl", Body: body}
}
func LoginPost(session *response.Session, h http.Header, r *http.Request) rsvp.Response {
func LoginPost(session *session.Session, h http.Header, r *http.Request) rsvp.Response {
username, password, ok := r.BasicAuth()
if !ok {
err := r.ParseForm()

View File

@ -1,13 +1,13 @@
package routing
import (
"lishwist/http/response"
"lishwist/http/session"
"net/http"
"github.com/Teajey/rsvp"
)
func LogoutPost(session *response.Session, h http.Header, r *http.Request) rsvp.Response {
func LogoutPost(session *session.Session, h http.Header, r *http.Request) rsvp.Response {
session.Options().MaxAge = 0
session.ClearValues()

View File

@ -4,10 +4,11 @@ import (
"net/http"
"lishwist/http/response"
"lishwist/http/session"
"github.com/Teajey/rsvp"
)
func NotFound(s *response.Session, h http.Header, r *http.Request) rsvp.Response {
func NotFound(s *session.Session, h http.Header, r *http.Request) rsvp.Response {
return response.Error(http.StatusNotFound, "Page not found")
}

View File

@ -5,13 +5,14 @@ import (
lishwist "lishwist/core"
"lishwist/http/props"
"lishwist/http/response"
"lishwist/http/session"
"log"
"net/http"
"github.com/Teajey/rsvp"
)
func Register(session *response.Session, h http.Header, r *http.Request) rsvp.Response {
func Register(session *session.Session, h http.Header, r *http.Request) rsvp.Response {
register := props.NewRegister("", "", "")
flash := session.FlashGet()
@ -28,7 +29,7 @@ func Register(session *response.Session, h http.Header, r *http.Request) rsvp.Re
return response.Data("register.gotmpl", register)
}
func RegisterPost(s *response.Session, h http.Header, r *http.Request) rsvp.Response {
func RegisterPost(s *session.Session, h http.Header, r *http.Request) rsvp.Response {
var confirmPassword string
username, newPassword, ok := r.BasicAuth()
if ok {

View File

@ -4,6 +4,7 @@ import (
"encoding/xml"
lishwist "lishwist/core"
"lishwist/http/response"
"lishwist/http/session"
"net/http"
"github.com/Teajey/rsvp"
@ -14,7 +15,7 @@ type UserList struct {
Users []lishwist.User `xml:"User"`
}
func Users(app *lishwist.Session, session *response.Session, h http.Header, r *http.Request) rsvp.Response {
func Users(app *lishwist.Session, session *session.Session, h http.Header, r *http.Request) rsvp.Response {
admin := app.Admin()
if admin == nil {
return response.NotFound()
@ -28,7 +29,7 @@ func Users(app *lishwist.Session, session *response.Session, h http.Header, r *h
return response.Data("", UserList{Users: users})
}
func User(app *lishwist.Session, session *response.Session, h http.Header, r *http.Request) rsvp.Response {
func User(app *lishwist.Session, session *session.Session, h http.Header, r *http.Request) rsvp.Response {
admin := app.Admin()
if admin == nil {
return response.NotFound()
@ -47,7 +48,7 @@ func User(app *lishwist.Session, session *response.Session, h http.Header, r *ht
return response.Data("", user)
}
func UserPost(app *lishwist.Session, session *response.Session, h http.Header, r *http.Request) rsvp.Response {
func UserPost(app *lishwist.Session, session *session.Session, h http.Header, r *http.Request) rsvp.Response {
admin := app.Admin()
if admin == nil {
return response.NotFound()

View File

@ -8,6 +8,7 @@ import (
lishwist "lishwist/core"
"lishwist/http/response"
"lishwist/http/session"
)
func WishlistAdd(app *lishwist.Session, h http.Header, r *http.Request) rsvp.Response {
@ -40,7 +41,7 @@ func WishlistDelete(app *lishwist.Session, h http.Header, r *http.Request) rsvp.
return rsvp.SeeOther("/", "Wish deleted")
}
func ForeignWishlistPost(app *lishwist.Session, session *response.Session, h http.Header, r *http.Request) rsvp.Response {
func ForeignWishlistPost(app *lishwist.Session, session *session.Session, h http.Header, r *http.Request) rsvp.Response {
err := r.ParseForm()
if err != nil {
return response.Error(http.StatusBadRequest, "Failed to parse form")

View File

@ -5,31 +5,31 @@ import (
"net/http"
"strings"
"github.com/Teajey/rsvp"
"lishwist/http/env"
"lishwist/http/handler"
"lishwist/http/mux"
"lishwist/http/props"
"lishwist/http/response"
"lishwist/http/router"
"lishwist/http/routing"
"lishwist/http/session"
"github.com/Teajey/rsvp"
)
func prefixMovedPermanently(before, after string) response.HandlerFunc {
return func(s *response.Session, h http.Header, r *http.Request) rsvp.Response {
func prefixMovedPermanently(before, after string) handler.PublicFunc {
return func(s *session.Session, h http.Header, r *http.Request) rsvp.Response {
suffix := strings.TrimPrefix(r.RequestURI, before)
return rsvp.MovedPermanently(after + suffix)
}
}
func prefixPermanentRedirect(before, after string) response.HandlerFunc {
return func(s *response.Session, h http.Header, r *http.Request) rsvp.Response {
func prefixPermanentRedirect(before, after string) handler.PublicFunc {
return func(s *session.Session, h http.Header, r *http.Request) rsvp.Response {
suffix := strings.TrimPrefix(r.RequestURI, before)
return rsvp.PermanentRedirect(after + suffix)
}
}
func Create(useSecureCookies bool) *router.VisibilityRouter {
func Create(useSecureCookies bool) *mux.Serve {
gob.Register(&props.Register{})
gob.Register(&props.Login{})
gob.Register(&routing.AccountProps{})
@ -41,7 +41,7 @@ func Create(useSecureCookies bool) *router.VisibilityRouter {
store.Options.Path = "/"
store.Options.SameSite = http.SameSiteLaxMode
r := router.New(store)
r := mux.New(store)
r.Public.HandleFunc("GET /", routing.Login)
r.Public.HandleFunc("GET /groups/{groupReference}", routing.PublicGroup)

View File

@ -1,6 +1,8 @@
package response
package session
import (
"net/http"
"github.com/gorilla/sessions"
)
@ -9,6 +11,10 @@ type Session struct {
written bool
}
func New(inner *sessions.Session) *Session {
return &Session{inner: inner}
}
const flashKey = "_flash"
func (s *Session) FlashGet() any {
@ -61,3 +67,11 @@ func (s *Session) ClearValues() {
func (s *Session) Options() *sessions.Options {
return s.inner.Options
}
func (s *Session) WasWritten() bool {
return s.written
}
func (s *Session) Save(r *http.Request, w http.ResponseWriter) error {
return s.inner.Save(r, w)
}

View File

@ -14,4 +14,3 @@ func load() *template.Template {
t := template.Must(template.ParseGlob("templates/text/*.gotmpl"))
return t
}