81 lines
1.8 KiB
Go
81 lines
1.8 KiB
Go
/*
|
|
* SPDX-FileCopyrightText: 2023 jordi fita mas <jfita@peritasoft.com>
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
|
*/
|
|
|
|
package app
|
|
|
|
import (
|
|
"golang.org/x/text/language"
|
|
"net/http"
|
|
"path"
|
|
"strings"
|
|
|
|
"dev.tandem.ws/tandem/camper/pkg/database"
|
|
httplib "dev.tandem.ws/tandem/camper/pkg/http"
|
|
"dev.tandem.ws/tandem/camper/pkg/locale"
|
|
)
|
|
|
|
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) {
|
|
var head string
|
|
head, r.URL.Path = shiftPath(r.URL.Path)
|
|
switch head {
|
|
case "static":
|
|
h.fileHandler.ServeHTTP(w, r)
|
|
case "login":
|
|
switch r.Method {
|
|
case http.MethodPost:
|
|
h.handleLogin(w, r)
|
|
default:
|
|
methodNotAllowed(w, r, http.MethodPost)
|
|
}
|
|
case "":
|
|
switch r.Method {
|
|
case http.MethodGet:
|
|
h.handleGet(w, r)
|
|
default:
|
|
methodNotAllowed(w, r, http.MethodGet)
|
|
}
|
|
default:
|
|
http.NotFound(w, r)
|
|
}
|
|
}
|