diff --git a/pkg/app/app.go b/pkg/app/app.go index 4c1009f..a22ddb7 100644 --- a/pkg/app/app.go +++ b/pkg/app/app.go @@ -7,27 +7,18 @@ package app import ( "net/http" - "path" "strings" "golang.org/x/text/language" "dev.tandem.ws/tandem/camper/pkg/auth" + "dev.tandem.ws/tandem/camper/pkg/campsite" "dev.tandem.ws/tandem/camper/pkg/database" httplib "dev.tandem.ws/tandem/camper/pkg/http" "dev.tandem.ws/tandem/camper/pkg/locale" "dev.tandem.ws/tandem/camper/pkg/template" ) -func shiftPath(p string) (head, tail string) { - p = path.Clean("/" + p) - if i := strings.IndexByte(p[1:], '/') + 1; i <= 0 { - return p[1:], "/" - } else { - return p[1:i], p[i:] - } -} - func methodNotAllowed(w http.ResponseWriter, _ *http.Request, allowed ...string) { w.Header().Set("Allow", strings.Join(allowed, ", ")) http.Error(w, "Method not allowed", http.StatusMethodNotAllowed) @@ -37,6 +28,7 @@ type App struct { db *database.DB fileHandler http.Handler profile *profileHandler + campsite *campsite.Handler locales locale.Locales defaultLocale *locale.Locale languageMatcher language.Matcher @@ -53,6 +45,7 @@ func New(db *database.DB, avatarsDir string) (http.Handler, error) { db: db, fileHandler: static, profile: profile, + campsite: campsite.NewHandler(), locales: locales, defaultLocale: locales[language.Catalan], languageMatcher: language.NewMatcher(locales.Tags()), @@ -67,7 +60,7 @@ func New(db *database.DB, avatarsDir string) (http.Handler, error) { func (h *App) ServeHTTP(w http.ResponseWriter, r *http.Request) { requestPath := r.URL.Path var head string - head, r.URL.Path = shiftPath(r.URL.Path) + head, r.URL.Path = httplib.ShiftPath(r.URL.Path) switch head { case "static": h.fileHandler.ServeHTTP(w, r) @@ -105,6 +98,8 @@ func (h *App) ServeHTTP(w http.ResponseWriter, r *http.Request) { switch head { case "me": h.profile.Handler(user, conn).ServeHTTP(w, r) + case "campsites": + h.campsite.Handler(user, conn).ServeHTTP(w, r) case "": switch r.Method { case http.MethodGet: diff --git a/pkg/app/user.go b/pkg/app/user.go index e4fa953..a0248ad 100644 --- a/pkg/app/user.go +++ b/pkg/app/user.go @@ -83,7 +83,7 @@ func newProfileHandler(static http.Handler, avatarsDir string) (*profileHandler, func (h *profileHandler) Handler(user *auth.User, conn *database.Conn) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { var head string - head, r.URL.Path = shiftPath(r.URL.Path) + head, r.URL.Path = httplib.ShiftPath(r.URL.Path) switch head { case "avatar": diff --git a/pkg/campsite/handler.go b/pkg/campsite/handler.go new file mode 100644 index 0000000..ca7463f --- /dev/null +++ b/pkg/campsite/handler.go @@ -0,0 +1,38 @@ +/* + * SPDX-FileCopyrightText: 2023 jordi fita mas + * SPDX-License-Identifier: AGPL-3.0-only + */ + +package campsite + +import ( + "net/http" + + "dev.tandem.ws/tandem/camper/pkg/auth" + "dev.tandem.ws/tandem/camper/pkg/database" + httplib "dev.tandem.ws/tandem/camper/pkg/http" +) + +type Handler struct { + types *typeHandler +} + +func NewHandler() *Handler { + return &Handler{ + types: &typeHandler{}, + } +} + +func (h *Handler) Handler(user *auth.User, conn *database.Conn) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + var head string + head, r.URL.Path = httplib.ShiftPath(r.URL.Path) + + switch head { + case "types": + h.types.Handler(user, conn).ServeHTTP(w, r) + default: + http.NotFound(w, r) + } + } +} diff --git a/pkg/campsite/type.go b/pkg/campsite/type.go new file mode 100644 index 0000000..5900d04 --- /dev/null +++ b/pkg/campsite/type.go @@ -0,0 +1,32 @@ +/* + * SPDX-FileCopyrightText: 2023 jordi fita mas + * SPDX-License-Identifier: AGPL-3.0-only + */ + +package campsite + +import ( + "net/http" + + "dev.tandem.ws/tandem/camper/pkg/auth" + "dev.tandem.ws/tandem/camper/pkg/database" + httplib "dev.tandem.ws/tandem/camper/pkg/http" + "dev.tandem.ws/tandem/camper/pkg/template" +) + +type typeHandler struct { +} + +func (h *typeHandler) Handler(user *auth.User, conn *database.Conn) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + var head string + head, r.URL.Path = httplib.ShiftPath(r.URL.Path) + + switch head { + case "new": + template.MustRender(w, r, user, "campsite/type/new.gohtml", nil) + default: + http.NotFound(w, r) + } + } +} diff --git a/pkg/http/request.go b/pkg/http/request.go index 2a9f7e1..acae37e 100644 --- a/pkg/http/request.go +++ b/pkg/http/request.go @@ -8,6 +8,7 @@ package http import ( "net" "net/http" + "path" "strings" ) @@ -27,3 +28,12 @@ func RemoteAddr(r *http.Request) string { } return forwarded } + +func ShiftPath(p string) (head, tail string) { + p = path.Clean("/" + p) + if i := strings.IndexByte(p[1:], '/') + 1; i <= 0 { + return p[1:], "/" + } else { + return p[1:i], p[i:] + } +} diff --git a/web/templates/campsite/type/new.gohtml b/web/templates/campsite/type/new.gohtml new file mode 100644 index 0000000..c50a7fd --- /dev/null +++ b/web/templates/campsite/type/new.gohtml @@ -0,0 +1,31 @@ + +{{ define "title" -}} + {{( pgettext "New Campsite Type" "title" )}} +{{- end }} + +{{ define "content" -}} +
+

{{( pgettext "New Campsite Type" "title" )}}

+ {{ CSRFInput }} +
+ + + + +
+ +
+{{- end }}