81 lines
1.9 KiB
Go
81 lines
1.9 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/database"
|
||
|
middleware "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 = middleware.RecoverPanic(handler)
|
||
|
handler = middleware.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 "":
|
||
|
switch r.Method {
|
||
|
case http.MethodGet:
|
||
|
h.handleGet(w, r)
|
||
|
default:
|
||
|
methodNotAllowed(w, r, http.MethodGet)
|
||
|
}
|
||
|
default:
|
||
|
http.NotFound(w, r)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func (h *App) handleGet(w http.ResponseWriter, r *http.Request) {
|
||
|
l := locale.Match(r, h.locales, h.defaultLocale, h.languageMatcher)
|
||
|
template.MustRender(w, l, "login.gohtml", nil)
|
||
|
}
|