lishwist/http/server/server.go
2026-02-13 10:36:24 +09:00

90 lines
3.1 KiB
Go

package server
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 {
gob.Register(&props.Register{})
gob.Register(&props.Login{})
gob.Register(&routing.AccountProps{})
store := session.NewInMemoryStore([]byte(env.Configuration.SessionSecret))
store.Options.MaxAge = 86_400 // 24 hours in seconds
store.Options.Secure = useSecureCookies
store.Options.HttpOnly = true
store.Options.Path = "/"
store.Options.SameSite = http.SameSiteLaxMode
r := mux.New(store)
r.Public.HandleFunc("GET /groups/{groupReference}", routing.PublicGroup)
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, w rsvp.ResponseWriter, r *http.Request) rsvp.Body {
stem := file.StemPathValue(r, "file")
switch stem {
case "login":
return routing.Login(s, w, r)
case "register":
return routing.Register(s, w, r)
}
return routing.LoginReferrer(s, w, 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 /groups/{groupReference}", routing.Group)
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, w rsvp.ResponseWriter, r *http.Request) rsvp.Body {
switch file.StemPathValue(r, "file") {
case "account":
return routing.Account(app, s, w, r)
case "events":
return routing.EventList(app, s, w, r)
case "groups":
return routing.Groups(app, s, w, r)
case "health":
return routing.Health(app, s, w, r)
case "home":
return routing.Home(app, s, w, r)
case "users":
return routing.Users(app, s, w, r)
}
return routing.NotFound(w, 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)
r.Private.HandleFunc("POST /{$}", routing.HomePost)
// Deprecated
r.HandleFunc("GET /group/{groupReference}", middleware.PrefixMovedPermanently("/group/", "/groups/"))
r.HandleFunc("GET /list/{groupReference}", middleware.PrefixMovedPermanently("/list/", "/lists/"))
r.HandleFunc("POST /group/{groupReference}", middleware.PrefixPermanentRedirect("/group/", "/groups/"))
r.HandleFunc("POST /list/{groupReference}", middleware.PrefixPermanentRedirect("/list/", "/lists/"))
return r
}