Move the locales parameter inside Company struct.
I realized that locales should be company-dependent: we could have two companies that show pages in a different subset of the application locales. It is not the case now, because despite being a “multicompany application”, it is intended for a single customer, but still makes sense to include it in Company, even if the subset is the same set as the application’s.
This commit is contained in:
parent
9a8ef8ce9f
commit
e59e6a2a42
|
@ -76,7 +76,7 @@ func (h *App) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
company, err := auth.CompanyByHost(r.Context(), conn, r.Host)
|
company, err := auth.CompanyByHost(r.Context(), conn, r.Host, h.locales)
|
||||||
if database.ErrorIsNotFound(err) {
|
if database.ErrorIsNotFound(err) {
|
||||||
http.NotFound(w, r)
|
http.NotFound(w, r)
|
||||||
return
|
return
|
||||||
|
@ -116,7 +116,7 @@ func (h *App) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
user.Locale = urlLocale
|
user.Locale = urlLocale
|
||||||
h.public.Handler(user, company, h.locales, conn).ServeHTTP(w, r)
|
h.public.Handler(user, company, conn).ServeHTTP(w, r)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -12,7 +12,6 @@ import (
|
||||||
"dev.tandem.ws/tandem/camper/pkg/auth"
|
"dev.tandem.ws/tandem/camper/pkg/auth"
|
||||||
"dev.tandem.ws/tandem/camper/pkg/database"
|
"dev.tandem.ws/tandem/camper/pkg/database"
|
||||||
httplib "dev.tandem.ws/tandem/camper/pkg/http"
|
httplib "dev.tandem.ws/tandem/camper/pkg/http"
|
||||||
"dev.tandem.ws/tandem/camper/pkg/locale"
|
|
||||||
"dev.tandem.ws/tandem/camper/pkg/template"
|
"dev.tandem.ws/tandem/camper/pkg/template"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -22,14 +21,14 @@ func newPublicHandler() *publicHandler {
|
||||||
return &publicHandler{}
|
return &publicHandler{}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *publicHandler) Handler(user *auth.User, company *auth.Company, locales locale.Locales, conn *database.Conn) http.Handler {
|
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) {
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
var head string
|
var head string
|
||||||
head, r.URL.Path = httplib.ShiftPath(r.URL.Path)
|
head, r.URL.Path = httplib.ShiftPath(r.URL.Path)
|
||||||
switch head {
|
switch head {
|
||||||
case "":
|
case "":
|
||||||
page := newHomePage()
|
page := newHomePage()
|
||||||
page.MustRender(w, r, user, company, locales)
|
page.MustRender(w, r, user, company)
|
||||||
default:
|
default:
|
||||||
http.NotFound(w, r)
|
http.NotFound(w, r)
|
||||||
}
|
}
|
||||||
|
@ -55,11 +54,11 @@ func newPublicPage(template string) *PublicPage {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *PublicPage) MustRender(w http.ResponseWriter, r *http.Request, user *auth.User, company *auth.Company, locales locale.Locales) {
|
func (p *PublicPage) MustRender(w http.ResponseWriter, r *http.Request, user *auth.User, company *auth.Company) {
|
||||||
schema := httplib.Protocol(r)
|
schema := httplib.Protocol(r)
|
||||||
authority := httplib.Host(r)
|
authority := httplib.Host(r)
|
||||||
_, path := httplib.ShiftPath(r.RequestURI)
|
_, path := httplib.ShiftPath(r.RequestURI)
|
||||||
for _, l := range locales {
|
for _, l := range company.Locales {
|
||||||
p.LocalizedAlternates = append(p.LocalizedAlternates, &LocalizedAlternate{
|
p.LocalizedAlternates = append(p.LocalizedAlternates, &LocalizedAlternate{
|
||||||
Lang: l.Language.String(),
|
Lang: l.Language.String(),
|
||||||
Endonym: l.Endonym,
|
Endonym: l.Endonym,
|
||||||
|
|
|
@ -9,14 +9,18 @@ import (
|
||||||
"context"
|
"context"
|
||||||
|
|
||||||
"dev.tandem.ws/tandem/camper/pkg/database"
|
"dev.tandem.ws/tandem/camper/pkg/database"
|
||||||
|
"dev.tandem.ws/tandem/camper/pkg/locale"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Company struct {
|
type Company struct {
|
||||||
ID int
|
ID int
|
||||||
|
Locales locale.Locales
|
||||||
}
|
}
|
||||||
|
|
||||||
func CompanyByHost(ctx context.Context, conn *database.Conn, host string) (*Company, error) {
|
func CompanyByHost(ctx context.Context, conn *database.Conn, host string, allLocales locale.Locales) (*Company, error) {
|
||||||
company := &Company{}
|
company := &Company{
|
||||||
|
Locales: allLocales,
|
||||||
|
}
|
||||||
if err := conn.QueryRow(ctx, `
|
if err := conn.QueryRow(ctx, `
|
||||||
select company_id
|
select company_id
|
||||||
from company_host
|
from company_host
|
||||||
|
|
Loading…
Reference in New Issue