/* * SPDX-FileCopyrightText: 2023 jordi fita mas * SPDX-License-Identifier: AGPL-3.0-only */ package app import ( "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" "fmt" "net/http" "sort" ) type publicHandler struct{} func newPublicHandler() *publicHandler { return &publicHandler{} } func (h *publicHandler) Handler(user *auth.User, company *auth.Company, conn *database.Conn) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { var head string head, r.URL.Path = httplib.ShiftPath(r.URL.Path) switch head { case "": page := newHomePage() page.MustRender(w, r, user, company) default: http.NotFound(w, r) } }) } type homePage struct { *PublicPage } func newHomePage() *homePage { return &homePage{newPublicPage("home.gohtml")} } type PublicPage struct { template string LocalizedAlternates []*LocalizedAlternate } func newPublicPage(template string) *PublicPage { return &PublicPage{ template: template, } } func (p *PublicPage) MustRender(w http.ResponseWriter, r *http.Request, user *auth.User, company *auth.Company) { schema := httplib.Protocol(r) authority := httplib.Host(r) _, path := httplib.ShiftPath(r.RequestURI) for _, l := range company.Locales { p.LocalizedAlternates = append(p.LocalizedAlternates, &LocalizedAlternate{ Lang: l.Language.String(), Endonym: l.Endonym, HRef: fmt.Sprintf("%s://%s/%s%s", schema, authority, l.Language, path), }) } sort.Slice(p.LocalizedAlternates, func(i, j int) bool { return p.LocalizedAlternates[i].Lang < p.LocalizedAlternates[j].Lang }) template.MustRenderPublic(w, r, user, company, p.template, p) } type LocalizedAlternate struct { Lang string HRef string Endonym string }