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
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
144 lines
4.4 KiB
Go
144 lines
4.4 KiB
Go
package statuspage
|
|
|
|
import (
|
|
"image/color"
|
|
"net/http"
|
|
"strings"
|
|
"sync"
|
|
"time"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
"git.zb-server.de/ZB-Server/uptimesebi/internal/agentapi"
|
|
"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"
|
|
)
|
|
|
|
// maxDescription bounds a page's description, about what search results
|
|
// show.
|
|
const maxDescription = 160
|
|
|
|
// origin is the scheme and host the visitor reached the server at.
|
|
func origin(c *gin.Context) string {
|
|
scheme := "http"
|
|
if c.Request.TLS != nil || strings.EqualFold(c.GetHeader("X-Forwarded-Proto"), "https") {
|
|
scheme = "https"
|
|
}
|
|
return scheme + "://" + c.Request.Host
|
|
}
|
|
|
|
var statusAccent = map[string]color.Color{
|
|
agentapi.PageOperational: ogimage.Up,
|
|
agentapi.PageDegraded: ogimage.Degraded,
|
|
agentapi.PageOutage: ogimage.Down,
|
|
agentapi.PageUnderMaintenance: ogimage.Maintenance,
|
|
agentapi.PageUnknown: ogimage.Unknown,
|
|
}
|
|
|
|
// pageMeta describes the status page at base for search engines and link
|
|
// previews, in the language the request was localized to.
|
|
func (s *Server) pageMeta(c *gin.Context, snap agentapi.PageSnapshot, base string) *web.PageMeta {
|
|
ctx := c.Request.Context()
|
|
lang := i18n.Lang(ctx)
|
|
desc := web.PlainText(snap.Description, maxDescription)
|
|
if desc == "" {
|
|
desc = i18n.T(ctx, "page.meta_description", "Title", snap.Title, "Status", i18n.T(ctx, "page.status."+snap.Status))
|
|
}
|
|
abs := origin(c) + base
|
|
image := web.LangURL(abs+"og.png", lang)
|
|
_, chosen := c.GetQuery("lang")
|
|
return web.NewPageMeta(snap.Title, desc, abs, image, snap.Title, lang, chosen && s.catalog.IsSupported(c.Query("lang")), s.catalog.Supported())
|
|
}
|
|
|
|
// ogCache keeps each page's preview image until its title, status or
|
|
// language changes.
|
|
type ogCache struct {
|
|
mu sync.Mutex
|
|
images map[string]ogEntry
|
|
}
|
|
|
|
type ogEntry struct {
|
|
sig string
|
|
png []byte
|
|
}
|
|
|
|
// serveOG draws the page's preview image: its name and current status.
|
|
func (s *Server) serveOG(c *gin.Context, e entry, base string) {
|
|
s.localizeRequest(c)
|
|
ctx := c.Request.Context()
|
|
snap := e.Snap.WithMaintenance(s.now())
|
|
card := ogimage.Card{
|
|
Title: snap.Title,
|
|
Subtitle: i18n.T(ctx, "page.status."+snap.Status),
|
|
Footer: strings.TrimSuffix(c.Request.Host+base, "/"),
|
|
Accent: statusAccent[snap.Status],
|
|
}
|
|
sig := strings.Join([]string{card.Title, card.Subtitle, card.Footer, snap.Status}, "\x00")
|
|
key := e.Key + "\x00" + i18n.Lang(ctx)
|
|
s.og.mu.Lock()
|
|
cached, ok := s.og.images[key]
|
|
s.og.mu.Unlock()
|
|
if !ok || cached.sig != sig {
|
|
png, err := ogimage.Render(card)
|
|
if err != nil {
|
|
s.log.Warn("drawing preview image failed", "page", e.Key, "error", err)
|
|
c.Status(http.StatusInternalServerError)
|
|
return
|
|
}
|
|
cached = ogEntry{sig: sig, png: png}
|
|
s.og.mu.Lock()
|
|
if s.og.images == nil {
|
|
s.og.images = map[string]ogEntry{}
|
|
}
|
|
s.og.images[key] = cached
|
|
s.og.mu.Unlock()
|
|
}
|
|
c.Header("Cache-Control", "public, max-age=300")
|
|
c.Data(http.StatusOK, "image/png", cached.png)
|
|
}
|
|
|
|
func (s *Server) forgetOG(key string) {
|
|
s.og.mu.Lock()
|
|
defer s.og.mu.Unlock()
|
|
for k := range s.og.images {
|
|
if strings.HasPrefix(k, key+"\x00") {
|
|
delete(s.og.images, k)
|
|
}
|
|
}
|
|
}
|
|
|
|
func (s *Server) serveSitemap(c *gin.Context, base string) {
|
|
// No lastmod: a snapshot is regenerated every minute, which says
|
|
// nothing about whether the page changed.
|
|
body, err := web.Sitemap([]string{origin(c) + base}, s.catalog.Supported(), time.Time{})
|
|
if err != nil {
|
|
c.Status(http.StatusInternalServerError)
|
|
return
|
|
}
|
|
c.Header("Cache-Control", "public, max-age=3600")
|
|
c.Data(http.StatusOK, "application/xml; charset=utf-8", body)
|
|
}
|
|
|
|
// serveRobots lets crawlers read the pages but not the feeds, the API or
|
|
// the subscription forms. A custom domain serves one page, so its sitemap
|
|
// can be named; the shared hosts serve many under their slugs.
|
|
func (s *Server) serveRobots(c *gin.Context) {
|
|
var b strings.Builder
|
|
b.WriteString("User-agent: *\n")
|
|
prefix := "/*"
|
|
custom := s.IsCustomDomain(hostOnly(c.Request.Host))
|
|
if custom {
|
|
prefix = ""
|
|
}
|
|
for _, p := range []string{"/api/", "/subscribe", "/unsubscribe", "/feed.rss"} {
|
|
b.WriteString("Disallow: " + prefix + p + "\n")
|
|
}
|
|
b.WriteString("Allow: /\n")
|
|
if custom {
|
|
b.WriteString("\nSitemap: " + origin(c) + "/sitemap.xml\n")
|
|
}
|
|
c.Header("Cache-Control", "public, max-age=3600")
|
|
c.Data(http.StatusOK, "text/plain; charset=utf-8", []byte(b.String()))
|
|
}
|