OAuth continue-URL rewriting shares path/locale utilities with SEO, so keep those in locale.go and leave seo.go for SEOFromRequest only. Signed-off-by: Émile Ré <emile@probo.com>
71 lines
2.5 KiB
Go
71 lines
2.5 KiB
Go
// Copyright (c) 2026 Probo Inc <hello@probo.com>.
|
|
//
|
|
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
// of this software and associated documentation files (the "Software"), to deal
|
|
// in the Software without restriction, including without limitation the rights
|
|
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
// copies of the Software, and to permit persons to whom the Software is
|
|
// furnished to do so, subject to the following conditions:
|
|
//
|
|
// The above copyright notice and this permission notice shall be included in
|
|
// all copies or substantial portions of the Software.
|
|
//
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
// SOFTWARE.
|
|
|
|
package complianceportal_v1
|
|
|
|
import (
|
|
"net/http"
|
|
|
|
"go.gearno.de/x/ref"
|
|
"go.probo.inc/probo/pkg/iam"
|
|
"go.probo.inc/probo/pkg/server/api/complianceportal"
|
|
)
|
|
|
|
// SEOFromRequest derives html lang, a self-referencing canonical URL, and
|
|
// hreflang alternates (including x-default → English) for the SPA shell.
|
|
// Portals are host-routed (slug subdomain / custom domain); the request path
|
|
// is already relative to the portal root. The portal origin comes from
|
|
// request context (set by SNI middleware as scheme://host). When missing,
|
|
// canonical and hreflang are left empty so callers do not emit relative SEO
|
|
// links.
|
|
func SEOFromRequest(r *http.Request) (htmlLang, canonical string, hreflang []HreflangLink) {
|
|
pathname := r.URL.Path
|
|
if pathname == "" {
|
|
pathname = "/"
|
|
}
|
|
|
|
locale, rest := splitLocaleFromAppPath(pathname)
|
|
htmlLang = locale
|
|
|
|
origin := ref.UnrefOrZero(complianceportal.CompliancePortalBaseURLFromContext(r.Context()))
|
|
if origin == "" {
|
|
return htmlLang, "", nil
|
|
}
|
|
|
|
canonical = localizedPageURL(origin, locale, rest)
|
|
|
|
locales := iam.SupportedIdentityLocales
|
|
|
|
hreflang = make([]HreflangLink, 0, len(locales)+1)
|
|
for _, loc := range locales {
|
|
hreflang = append(hreflang, HreflangLink{
|
|
Lang: loc,
|
|
Href: localizedPageURL(origin, loc, rest),
|
|
})
|
|
}
|
|
|
|
hreflang = append(hreflang, HreflangLink{
|
|
Lang: "x-default",
|
|
Href: localizedPageURL(origin, defaultCompliancePortalLocale, rest),
|
|
})
|
|
|
|
return htmlLang, canonical, hreflang
|
|
}
|