From eacf3365c861b21b20f45e15279e94b35d830551 Mon Sep 17 00:00:00 2001 From: Teajey <21069848+Teajey@users.noreply.github.com> Date: Tue, 16 Dec 2025 00:27:43 +0900 Subject: [PATCH] URL file extension support --- http/file/file.go | 16 +++++++++++ http/routing/foreign_wishlist.go | 5 ++-- http/routing/groups.go | 14 +++++---- http/routing/not_found.go | 4 +-- http/routing/users.go | 5 ++-- http/routing/wishlist.go | 3 +- http/server/server.go | 49 +++++++++++++++++++++++++------- 7 files changed, 72 insertions(+), 24 deletions(-) create mode 100644 http/file/file.go diff --git a/http/file/file.go b/http/file/file.go new file mode 100644 index 0000000..88a3c5b --- /dev/null +++ b/http/file/file.go @@ -0,0 +1,16 @@ +package file + +import ( + "net/http" + "path/filepath" +) + +func Stem(path string) string { + ext := filepath.Ext(path) + return path[:len(path)-len(ext)] +} + +func StemPathValue(r *http.Request, name string) string { + return Stem(r.PathValue(name)) +} + diff --git a/http/routing/foreign_wishlist.go b/http/routing/foreign_wishlist.go index 632fab5..759e6c0 100644 --- a/http/routing/foreign_wishlist.go +++ b/http/routing/foreign_wishlist.go @@ -2,6 +2,7 @@ package routing import ( lishwist "lishwist/core" + "lishwist/http/file" "lishwist/http/response" "lishwist/http/session" "lishwist/http/templates" @@ -20,7 +21,7 @@ type foreignWishlistProps struct { } func ForeignWishlist(app *lishwist.Session, session *session.Session, h http.Header, r *http.Request) rsvp.Response { - userReference := r.PathValue("userReference") + userReference := file.StemPathValue(r, "userReference") user := app.User() if user.Reference == userReference { return rsvp.Found("/", "You're not allowed to view your own wishlist!") @@ -49,7 +50,7 @@ type publicWishlistProps struct { } func PublicWishlist(s *session.Session, h http.Header, r *http.Request) rsvp.Response { - userReference := r.PathValue("userReference") + 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) diff --git a/http/routing/groups.go b/http/routing/groups.go index d5de652..becbaab 100644 --- a/http/routing/groups.go +++ b/http/routing/groups.go @@ -7,6 +7,7 @@ import ( "slices" lishwist "lishwist/core" + "lishwist/http/file" "lishwist/http/response" "lishwist/http/session" "lishwist/http/templates" @@ -15,12 +16,13 @@ import ( ) type GroupProps struct { - Navbar templates.NavCollapse - Group *lishwist.Group + Navbar templates.NavCollapse + Group *lishwist.Group + Referrer string } func AdminGroup(app *lishwist.Session, h http.Header, r *http.Request) rsvp.Response { - reference := r.PathValue("groupReference") + reference := file.StemPathValue(r, "groupReference") group, err := lishwist.GetGroupByReference(reference) if err != nil { return response.Error(http.StatusInternalServerError, "Couldn't get group: %s", err) @@ -45,7 +47,7 @@ func Group(app *lishwist.Session, session *session.Session, h http.Header, r *ht if user.IsAdmin { return AdminGroup(app, h, r) } - groupReference := r.PathValue("groupReference") + groupReference := file.StemPathValue(r, "groupReference") group, err := app.GetGroupByReference(groupReference) if err != nil { log.Printf("Couldn't get group: %s\n", err) @@ -64,7 +66,7 @@ func Group(app *lishwist.Session, session *session.Session, h http.Header, r *ht } func PublicGroup(s *session.Session, h http.Header, r *http.Request) rsvp.Response { - groupReference := r.PathValue("groupReference") + groupReference := file.StemPathValue(r, "groupReference") group, err := lishwist.GetGroupByReference(groupReference) if err != nil { log.Printf("Couldn't get group: %s\n", err) @@ -93,7 +95,7 @@ func GroupPost(app *lishwist.Session, session *session.Session, h http.Header, r var group *lishwist.Group - reference := r.PathValue("groupReference") + reference := file.StemPathValue(r, "groupReference") name := r.Form.Get("name") addUsers := r.Form["addUser"] removeUsers := r.Form["removeUser"] diff --git a/http/routing/not_found.go b/http/routing/not_found.go index c9a1f75..b65754c 100644 --- a/http/routing/not_found.go +++ b/http/routing/not_found.go @@ -3,13 +3,11 @@ package routing import ( "net/http" - lishwist "lishwist/core" "lishwist/http/response" - "lishwist/http/session" "github.com/Teajey/rsvp" ) -func NotFound(app *lishwist.Session, s *session.Session, h http.Header, r *http.Request) rsvp.Response { +func NotFound(h http.Header, r *http.Request) rsvp.Response { return response.Error(http.StatusNotFound, "Page not found") } diff --git a/http/routing/users.go b/http/routing/users.go index e1f4aa4..7f4730c 100644 --- a/http/routing/users.go +++ b/http/routing/users.go @@ -3,6 +3,7 @@ package routing import ( "encoding/xml" lishwist "lishwist/core" + "lishwist/http/file" "lishwist/http/response" "lishwist/http/session" "net/http" @@ -35,7 +36,7 @@ func User(app *lishwist.Session, session *session.Session, h http.Header, r *htt return response.NotFound() } - reference := r.PathValue("userReference") + reference := file.StemPathValue(r, "userReference") user, err := lishwist.GetUserByReference(reference) if err != nil { @@ -59,7 +60,7 @@ func UserPost(app *lishwist.Session, session *session.Session, h http.Header, r return response.Error(http.StatusBadRequest, "Failed to parse form") } - reference := r.PathValue("userReference") + reference := file.StemPathValue(r, "userReference") intent := r.Form.Get("intent") diff --git a/http/routing/wishlist.go b/http/routing/wishlist.go index 02926a2..66ba1fa 100644 --- a/http/routing/wishlist.go +++ b/http/routing/wishlist.go @@ -7,6 +7,7 @@ import ( "github.com/Teajey/rsvp" lishwist "lishwist/core" + "lishwist/http/file" "lishwist/http/response" "lishwist/http/session" ) @@ -47,7 +48,7 @@ func ForeignWishlistPost(app *lishwist.Session, session *session.Session, h http return response.Error(http.StatusBadRequest, "Failed to parse form") } - userReference := r.PathValue("userReference") + userReference := file.StemPathValue(r, "userReference") resp := rsvp.SeeOther("/lists/"+userReference, "Update successful") intent := r.Form.Get("intent") switch intent { diff --git a/http/server/server.go b/http/server/server.go index b0cf247..5083a0b 100644 --- a/http/server/server.go +++ b/http/server/server.go @@ -4,12 +4,16 @@ import ( "encoding/gob" "net/http" + lishwist "lishwist/core" "lishwist/http/env" + "lishwist/http/file" "lishwist/http/middleware" "lishwist/http/mux" "lishwist/http/props" "lishwist/http/routing" "lishwist/http/session" + + "github.com/Teajey/rsvp" ) func Create(useSecureCookies bool) *mux.Serve { @@ -26,25 +30,50 @@ func Create(useSecureCookies bool) *mux.Serve { r := mux.New(store) - r.Public.HandleFunc("GET /", routing.Login) r.Public.HandleFunc("GET /groups/{groupReference}", routing.PublicGroup) r.Public.HandleFunc("GET /lists/{userReference}", routing.PublicWishlist) - r.Public.HandleFunc("GET /register", routing.Register) - r.Public.HandleFunc("POST /", routing.LoginPost) - r.Public.HandleFunc("POST /register", routing.RegisterPost) + 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 { + stem := file.StemPathValue(r, "file") + switch stem { + case "login": + return routing.Login(s, h, r) + case "register": + return routing.Register(s, h, r) + } + return routing.LoginReferrer(s, h, r) + }) + + r.Public.HandleFunc("POST /register", routing.RegisterPost) + r.Public.HandleFunc("POST /login", routing.LoginPost) + r.Public.HandleFunc("POST /{$}", routing.LoginPost) - r.Private.HandleFunc("GET /events", routing.EventList) - r.Private.HandleFunc("GET /account", routing.Account) - r.Private.HandleFunc("GET /health", routing.Health) - r.Private.HandleFunc("GET /", routing.NotFound) - r.Private.HandleFunc("GET /groups", routing.Groups) r.Private.HandleFunc("GET /groups/{groupReference}", routing.Group) r.Private.HandleFunc("GET /lists/{userReference}", routing.ForeignWishlist) - r.Private.HandleFunc("GET /users", routing.Users) 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 { + switch file.StemPathValue(r, "file") { + case "account": + return routing.Account(app, s, h, r) + case "events": + return routing.EventList(app, s, h, r) + case "groups": + return routing.Groups(app, s, h, r) + case "health": + return routing.Health(app, s, h, r) + case "home": + return routing.Home(app, s, h, r) + case "users": + return routing.Users(app, s, h, r) + } + return routing.NotFound(h, r) + }) + r.Private.HandleFunc("POST /account", routing.AccountPost) r.Private.HandleFunc("POST /groups/{groupReference}", routing.GroupPost) + r.Private.HandleFunc("POST /home", routing.HomePost) r.Private.HandleFunc("POST /lists/{userReference}", routing.ForeignWishlistPost) r.Private.HandleFunc("POST /logout", routing.LogoutPost) r.Private.HandleFunc("POST /users/{userReference}", routing.UserPost)