Add the mock-up form for new campsite type

It does nothing, but i need it to discuss with Oriol.

Now there are more than a single package that requires shiftPath, so
i moved it to http and an exported function, ShiftPath.

Part of #25.
This commit is contained in:
jordi fita mas 2023-07-31 14:22:57 +02:00
parent ab0cfb4100
commit cfa330bce4
6 changed files with 118 additions and 12 deletions

View File

@ -7,27 +7,18 @@ package app
import ( import (
"net/http" "net/http"
"path"
"strings" "strings"
"golang.org/x/text/language" "golang.org/x/text/language"
"dev.tandem.ws/tandem/camper/pkg/auth" "dev.tandem.ws/tandem/camper/pkg/auth"
"dev.tandem.ws/tandem/camper/pkg/campsite"
"dev.tandem.ws/tandem/camper/pkg/database" "dev.tandem.ws/tandem/camper/pkg/database"
httplib "dev.tandem.ws/tandem/camper/pkg/http" httplib "dev.tandem.ws/tandem/camper/pkg/http"
"dev.tandem.ws/tandem/camper/pkg/locale" "dev.tandem.ws/tandem/camper/pkg/locale"
"dev.tandem.ws/tandem/camper/pkg/template" "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) { func methodNotAllowed(w http.ResponseWriter, _ *http.Request, allowed ...string) {
w.Header().Set("Allow", strings.Join(allowed, ", ")) w.Header().Set("Allow", strings.Join(allowed, ", "))
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed) http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
@ -37,6 +28,7 @@ type App struct {
db *database.DB db *database.DB
fileHandler http.Handler fileHandler http.Handler
profile *profileHandler profile *profileHandler
campsite *campsite.Handler
locales locale.Locales locales locale.Locales
defaultLocale *locale.Locale defaultLocale *locale.Locale
languageMatcher language.Matcher languageMatcher language.Matcher
@ -53,6 +45,7 @@ func New(db *database.DB, avatarsDir string) (http.Handler, error) {
db: db, db: db,
fileHandler: static, fileHandler: static,
profile: profile, profile: profile,
campsite: campsite.NewHandler(),
locales: locales, locales: locales,
defaultLocale: locales[language.Catalan], defaultLocale: locales[language.Catalan],
languageMatcher: language.NewMatcher(locales.Tags()), 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) { func (h *App) ServeHTTP(w http.ResponseWriter, r *http.Request) {
requestPath := r.URL.Path requestPath := r.URL.Path
var head string var head string
head, r.URL.Path = shiftPath(r.URL.Path) head, r.URL.Path = httplib.ShiftPath(r.URL.Path)
switch head { switch head {
case "static": case "static":
h.fileHandler.ServeHTTP(w, r) h.fileHandler.ServeHTTP(w, r)
@ -105,6 +98,8 @@ func (h *App) ServeHTTP(w http.ResponseWriter, r *http.Request) {
switch head { switch head {
case "me": case "me":
h.profile.Handler(user, conn).ServeHTTP(w, r) h.profile.Handler(user, conn).ServeHTTP(w, r)
case "campsites":
h.campsite.Handler(user, conn).ServeHTTP(w, r)
case "": case "":
switch r.Method { switch r.Method {
case http.MethodGet: case http.MethodGet:

View File

@ -83,7 +83,7 @@ func newProfileHandler(static http.Handler, avatarsDir string) (*profileHandler,
func (h *profileHandler) Handler(user *auth.User, conn *database.Conn) http.HandlerFunc { func (h *profileHandler) Handler(user *auth.User, conn *database.Conn) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) { return func(w http.ResponseWriter, r *http.Request) {
var head string var head string
head, r.URL.Path = shiftPath(r.URL.Path) head, r.URL.Path = httplib.ShiftPath(r.URL.Path)
switch head { switch head {
case "avatar": case "avatar":

38
pkg/campsite/handler.go Normal file
View File

@ -0,0 +1,38 @@
/*
* SPDX-FileCopyrightText: 2023 jordi fita mas <jfita@peritasoft.com>
* 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)
}
}
}

32
pkg/campsite/type.go Normal file
View File

@ -0,0 +1,32 @@
/*
* SPDX-FileCopyrightText: 2023 jordi fita mas <jfita@peritasoft.com>
* 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)
}
}
}

View File

@ -8,6 +8,7 @@ package http
import ( import (
"net" "net"
"net/http" "net/http"
"path"
"strings" "strings"
) )
@ -27,3 +28,12 @@ func RemoteAddr(r *http.Request) string {
} }
return forwarded 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:]
}
}

View File

@ -0,0 +1,31 @@
<!--
SPDX-FileCopyrightText: 2023 jordi fita mas <jordi@tandem.blog>
SPDX-License-Identifier: AGPL-3.0-only
-->
{{ define "title" -}}
{{( pgettext "New Campsite Type" "title" )}}
{{- end }}
{{ define "content" -}}
<form method="post">
<h2>{{( pgettext "New Campsite Type" "title" )}}</h2>
{{ CSRFInput }}
<fieldset>
<label>{{( pgettext "Name" "input")}}<br>
<input type="text"><br>
</label>
<label>{{( pgettext "Description" "input")}}<br>
<textarea></textarea><br>
</label>
<label>{{( pgettext "Rules" "input")}}<br>
<textarea></textarea><br>
</label>
<label>{{( pgettext "Booking Terms and Conditions" "input")}}<br>
<textarea></textarea><br>
</label>
</fieldset>
<footer>
<button type="submit">{{( pgettext "Add" "action" )}}</button>
</footer>
</form>
{{- end }}