Files
uptimesebi/internal/api/seo.go
T
Sebastian UnterschützandClaude Opus 5.5 3baeb98ee2
ci / lint (push) Skipped
ci / test (push) Skipped
ci / build (push) Skipped
ci / image (push) Skipped
ci / lint (pull_request) Successful in 6m23s
ci / test (pull_request) Successful in 12m44s
ci / build (pull_request) Successful in 16m9s
ci / image (pull_request) Successful in 3m24s
feat(seo): metadata, previews and sitemaps for status pages and the app
Status pages describe themselves to search engines and link previews:
description (from the page's text, else its status), canonical address,
language versions via ?lang= with hreflang and x-default, Open Graph and
Twitter tags, and a 1200x630 preview image per page and language showing
its name and current status (/og.png). Each page has a sitemap.xml, and
robots.txt keeps crawlers off feeds, the API and subscription forms.
Component, legal and subscription pages are noindex.

In the cloud edition the app's sign-in, sign-up and legal pages get the
same tags with a preview image of their own, plus robots.txt and a
sitemap. Everything signed in stays noindex, and self-hosted
installations keep the whole app out of search engines.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017GpWAskjmYtZpuQ5JwYTX3
2026-11-28 22:50:34 +01:00

122 lines
3.5 KiB
Go

package api
import (
"net/http"
"net/url"
"strings"
"sync"
"time"
"github.com/gin-gonic/gin"
"git.zb-server.de/ZB-Server/uptimesebi/internal/i18n"
"git.zb-server.de/ZB-Server/uptimesebi/internal/ogimage"
"git.zb-server.de/ZB-Server/uptimesebi/internal/web"
)
// indexable reports whether search engines may list the public pages.
// Self-hosted installations keep their sign-in out of search results.
func (s *Server) indexable() bool { return s.cloud() }
// publicMeta describes a public page at path, such as the sign-in, for
// search engines and link previews; nil where pages are not indexed.
func (s *Server) publicMeta(c *gin.Context, path, titleID string) *web.PageMeta {
if !s.indexable() {
return nil
}
ctx := c.Request.Context()
lang := i18n.Lang(ctx)
chosen := s.catalog.IsSupported(c.Query("lang"))
title := i18n.T(ctx, titleID) + " · UptimeSebi"
image := web.LangURL(s.cfg.PublicURL+"/og.png", lang)
return web.NewPageMeta(title, i18n.T(ctx, "meta.app_description"), s.cfg.PublicURL+path, image, "UptimeSebi", lang, chosen, s.catalog.Supported())
}
// appImages keeps the app's preview image per language.
type appImages struct {
mu sync.Mutex
images map[string][]byte
}
// appOGImage serves the preview image of the app's public pages.
func (s *Server) appOGImage(c *gin.Context) {
l := s.catalog.Match(c.Query("lang"), c.GetHeader("Accept-Language"))
ctx := i18n.NewContext(c.Request.Context(), l)
s.ogImages.mu.Lock()
defer s.ogImages.mu.Unlock()
png, ok := s.ogImages.images[l.Lang()]
if !ok {
host := s.cfg.PublicURL
if u, err := url.Parse(host); err == nil {
host = u.Host
}
var err error
png, err = ogimage.Render(ogimage.Card{
Title: i18n.T(ctx, "meta.app_card_title"),
Subtitle: i18n.T(ctx, "meta.app_card_subtitle"),
Footer: host,
Accent: ogimage.Up,
})
if err != nil {
internalError(c, err)
return
}
if s.ogImages.images == nil {
s.ogImages.images = map[string][]byte{}
}
s.ogImages.images[l.Lang()] = png
}
c.Header("Cache-Control", "public, max-age=86400")
c.Data(http.StatusOK, "image/png", png)
}
// robotsTxt lets crawlers read the sign-in, sign-up and legal pages and
// nothing else; self-hosted installations turn them away entirely.
func (s *Server) robotsTxt(c *gin.Context) {
var b strings.Builder
b.WriteString("User-agent: *\n")
if s.indexable() {
for _, p := range []string{"/login", "/signup", "/legal/", "/og.png", "/static/"} {
b.WriteString("Allow: " + p + "\n")
}
}
b.WriteString("Disallow: /\n")
if s.indexable() {
b.WriteString("\nSitemap: " + s.cfg.PublicURL + "/sitemap.xml\n")
}
c.Header("Cache-Control", "public, max-age=3600")
c.Data(http.StatusOK, "text/plain; charset=utf-8", []byte(b.String()))
}
// sitemapXML lists the public pages with their language versions.
func (s *Server) sitemapXML(c *gin.Context) {
if !s.indexable() {
c.Status(http.StatusNotFound)
return
}
ctx := c.Request.Context()
paths := []string{"/login"}
if email, providers, err := s.signupOptions(ctx); err != nil {
internalError(c, err)
return
} else if email || len(providers) > 0 {
paths = append(paths, "/signup")
}
for _, d := range s.platformLegalDocs(ctx) {
if d.URL == "" {
paths = append(paths, "/legal/"+d.Kind)
}
}
var urls []string
for _, p := range paths {
urls = append(urls, s.cfg.PublicURL+p)
}
body, err := web.Sitemap(urls, s.catalog.Supported(), time.Time{})
if err != nil {
internalError(c, err)
return
}
c.Header("Cache-Control", "public, max-age=3600")
c.Data(http.StatusOK, "application/xml; charset=utf-8", body)
}