package server import ( "encoding/gob" "net/http" "strings" "lishwist/http/api" "lishwist/http/env" "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 { 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 { suffix := strings.TrimPrefix(r.RequestURI, before) return rsvp.PermanentRedirect(after + suffix) } } func Create(useSecureCookies bool) *router.VisibilityRouter { gob.Register(&api.RegisterProps{}) gob.Register(&api.LoginProps{}) 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 := router.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.Private.HandleFunc("GET /health", routing.ExpectAppSession(routing.Health)) r.Private.HandleFunc("GET /", routing.NotFound) r.Private.HandleFunc("GET /groups", routing.ExpectAppSession(routing.Groups)) r.Private.HandleFunc("GET /groups/{groupReference}", routing.ExpectAppSession(routing.Group)) r.Private.HandleFunc("GET /lists/{userReference}", routing.ExpectAppSession(routing.ForeignWishlist)) r.Private.HandleFunc("GET /users", routing.ExpectAppSession(routing.Users)) r.Private.HandleFunc("GET /users/{userReference}", routing.ExpectAppSession(routing.User)) r.Private.HandleFunc("GET /{$}", routing.ExpectAppSession(routing.Home)) r.Private.HandleFunc("POST /groups/{groupReference}", routing.ExpectAppSession(routing.GroupPost)) r.Private.HandleFunc("POST /lists/{userReference}", routing.ExpectAppSession(routing.ForeignWishlistPost)) r.Private.HandleFunc("POST /logout", routing.LogoutPost) r.Private.HandleFunc("POST /users/{userReference}", routing.ExpectAppSession(routing.UserPost)) r.Private.HandleFunc("POST /{$}", routing.ExpectAppSession(routing.HomePost)) // Deprecated r.HandleFunc("GET /group/{groupReference}", prefixMovedPermanently("/group/", "/groups/")) r.HandleFunc("GET /list/{groupReference}", prefixMovedPermanently("/list/", "/lists/")) r.HandleFunc("POST /group/{groupReference}", prefixPermanentRedirect("/group/", "/groups/")) r.HandleFunc("POST /list/{groupReference}", prefixPermanentRedirect("/list/", "/lists/")) return r }