URL file extension support

This commit is contained in:
Teajey 2025-12-16 00:27:43 +09:00
parent f945e9eb0f
commit eacf3365c8
Signed by: Teajey
GPG Key ID: 970E790FE834A713
7 changed files with 72 additions and 24 deletions

16
http/file/file.go Normal file
View File

@ -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))
}

View File

@ -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)

View File

@ -7,6 +7,7 @@ import (
"slices"
lishwist "lishwist/core"
"lishwist/http/file"
"lishwist/http/response"
"lishwist/http/session"
"lishwist/http/templates"
@ -17,10 +18,11 @@ import (
type GroupProps struct {
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"]

View File

@ -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")
}

View File

@ -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")

View File

@ -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 {

View File

@ -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)