camper/pkg/app/app.go

118 lines
2.6 KiB
Go

/*
* SPDX-FileCopyrightText: 2023 jordi fita mas <jfita@peritasoft.com>
* SPDX-License-Identifier: AGPL-3.0-only
*/
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/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)
}
type App struct {
db *database.DB
fileHandler http.Handler
locales locale.Locales
defaultLocale *locale.Locale
languageMatcher language.Matcher
}
func New(db *database.DB) http.Handler {
locales := locale.MustGetAll(db)
app := &App{
db: db,
fileHandler: http.FileServer(http.Dir("web/static")),
locales: locales,
defaultLocale: locales[language.Catalan],
languageMatcher: language.NewMatcher(locales.Tags()),
}
var handler http.Handler = app
handler = httplib.RecoverPanic(handler)
handler = httplib.LogRequest(handler)
return handler
}
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)
switch head {
case "static":
h.fileHandler.ServeHTTP(w, r)
case "favicon.ico":
r.URL.Path = requestPath
h.fileHandler.ServeHTTP(w, r)
default:
conn, err := h.db.Acquire(r.Context())
if err != nil {
panic(err)
}
defer conn.Release()
user, err := h.getUser(r, conn)
if err != nil {
panic(err)
}
if head == "login" {
switch r.Method {
case http.MethodGet:
serveLoginForm(w, r, user, "/")
case http.MethodPost:
handleLogin(w, r, user, conn)
default:
methodNotAllowed(w, r, http.MethodPost, http.MethodGet)
}
} else {
if !user.LoggedIn {
w.WriteHeader(http.StatusUnauthorized)
serveLoginForm(w, r, user, requestPath)
return
}
switch head {
case "me":
profileHandler(user, conn)(w, r)
case "":
switch r.Method {
case http.MethodGet:
h.serveDashboard(w, r, user)
default:
methodNotAllowed(w, r, http.MethodGet)
}
default:
http.NotFound(w, r)
}
}
}
}
func (h *App) serveDashboard(w http.ResponseWriter, r *http.Request, user *auth.User) {
template.MustRender(w, r, user, "dashboard.gohtml", nil)
}