camper/pkg/app/app.go
jordi fita mas e128680e9a Split templates and handlers into admin and public
I need to check that the user is an employee (or admin) in
administration handlers, but i do not want to do it for each handler,
because i am bound to forget it.  Thus, i added the /admin sub-path for
these resources.

The public-facing web is the rest of the resources outside /admin, but
for now there is only home, to test whether it works as expected or not.

The public-facing web can not relay on the user’s language settings, as
the guest user has no way to set that.  I would be happy to just use the
Accept-Language header for that, but apparently Google does not use that
header[0], and they give four alternatives: a country-specific domain,
a subdomain with a generic top-level domain (gTLD), subdirectories with
a gTLD, or URL parameters (e.g., site.com?loc=de).

Of the four, Google does not recommend URL parameters, and the customer
is already using subdirectories with the current site, therefor that’s
what i have chosen.

Google also tells me that it is a very good idea to have links between
localized version of the same resources, either with <link> elements,
Link HTTP response headers, or a sitemap file[1]; they are all
equivalent in the eyes of Google.

I have choosen the Link response headers way, because for that i can
simply “augment” ResponseHeader to automatically add these headers when
the response status is 2xx, otherwise i would need to pass down the
original URL path until it reaches the template.

Even though Camper is supposed to be a “generic”, multi-company
application, i think i will stick to the easiest route and write the
templates for just the “first” customer.

[0]: https://developers.google.com/search/docs/specialty/international/managing-multi-regional-sites
[1]: https://developers.google.com/search/docs/specialty/international/localized-versions
2023-08-05 03:42:37 +02:00

119 lines
2.9 KiB
Go

/*
* SPDX-FileCopyrightText: 2023 jordi fita mas <jfita@peritasoft.com>
* SPDX-License-Identifier: AGPL-3.0-only
*/
package app
import (
"net/http"
"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"
)
type App struct {
db *database.DB
fileHandler http.Handler
profile *profileHandler
admin *adminHandler
public *publicHandler
locales locale.Locales
defaultLocale *locale.Locale
languageMatcher language.Matcher
}
func New(db *database.DB, avatarsDir string) (http.Handler, error) {
locales := locale.MustGetAll(db)
static := http.FileServer(http.Dir("web/static"))
profile, err := newProfileHandler(static, avatarsDir)
if err != nil {
return nil, err
}
app := &App{
db: db,
fileHandler: static,
profile: profile,
admin: newAdminHandler(),
public: newPublicHandler(),
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, nil
}
func (h *App) ServeHTTP(w http.ResponseWriter, r *http.Request) {
requestPath := r.URL.Path
var head string
head, r.URL.Path = httplib.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)
}
company, err := auth.CompanyByHost(r.Context(), conn, r.Host)
if database.ErrorIsNotFound(err) {
http.NotFound(w, r)
return
} else if err != nil {
panic(err)
}
switch head {
case "":
http.Redirect(w, r, "/"+user.Locale.Language.String()+"/", http.StatusFound)
case "admin":
h.admin.Handle(user, company, conn, requestPath).ServeHTTP(w, r)
case "login":
switch r.Method {
case http.MethodGet:
serveLoginForm(w, r, user, company, "/admin")
case http.MethodPost:
handleLogin(w, r, user, company, conn)
default:
httplib.MethodNotAllowed(w, r, http.MethodPost, http.MethodGet)
}
case "me":
h.profile.Handler(user, company, conn, requestPath).ServeHTTP(w, r)
default:
langTag, err := language.Parse(head)
if err != nil {
http.NotFound(w, r)
return
}
urlLocale := h.locales[langTag]
if urlLocale == nil {
http.NotFound(w, r)
return
}
user.Locale = urlLocale
if r.Method == http.MethodGet || r.Method == http.MethodHead {
w = httplib.LanguageLinks(w, false, r.Host, r.URL.Path, h.locales)
}
h.public.Handler(user, company, conn).ServeHTTP(w, r)
}
}
}