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
210 lines
5.4 KiB
Go
210 lines
5.4 KiB
Go
// Package ogimage draws the preview images link previews show for status
|
||
// pages and the app (Open Graph and Twitter cards): 1200×630 PNGs in the
|
||
// style of the console.
|
||
package ogimage
|
||
|
||
import (
|
||
"bytes"
|
||
"image"
|
||
"image/color"
|
||
"image/draw"
|
||
"image/png"
|
||
"math"
|
||
"strings"
|
||
"sync"
|
||
|
||
"golang.org/x/image/font"
|
||
"golang.org/x/image/font/gofont/gobold"
|
||
"golang.org/x/image/font/gofont/goregular"
|
||
"golang.org/x/image/font/opentype"
|
||
"golang.org/x/image/math/fixed"
|
||
)
|
||
|
||
// Width and Height are the size link previews expect.
|
||
const (
|
||
Width = 1200
|
||
Height = 630
|
||
)
|
||
|
||
// Colors of the console's palette.
|
||
var (
|
||
Paper = color.RGBA{0xf4, 0xf2, 0xee, 0xff}
|
||
Ink = color.RGBA{0x1c, 0x1d, 0x1f, 0xff}
|
||
Muted = color.RGBA{0x6b, 0x67, 0x5f, 0xff}
|
||
Rule = color.RGBA{0xd9, 0xd5, 0xcc, 0xff}
|
||
Up = color.RGBA{0x2f, 0x7d, 0x4f, 0xff}
|
||
Down = color.RGBA{0xb3, 0x36, 0x1f, 0xff}
|
||
Degraded = color.RGBA{0xb5, 0x7b, 0x14, 0xff}
|
||
Unknown = color.RGBA{0x8a, 0x8a, 0x86, 0xff}
|
||
Maintenance = color.RGBA{0x3d, 0x6f, 0xa6, 0xff}
|
||
)
|
||
|
||
// Card is what an image shows: a title, a line below it marked with a dot
|
||
// in Accent, and a footer such as the page's address.
|
||
type Card struct {
|
||
Title string
|
||
Subtitle string
|
||
Footer string
|
||
Accent color.Color
|
||
}
|
||
|
||
var (
|
||
fontsOnce sync.Once
|
||
regular *opentype.Font
|
||
bold *opentype.Font
|
||
fontsErr error
|
||
)
|
||
|
||
func loadFonts() error {
|
||
fontsOnce.Do(func() {
|
||
if regular, fontsErr = opentype.Parse(goregular.TTF); fontsErr != nil {
|
||
return
|
||
}
|
||
bold, fontsErr = opentype.Parse(gobold.TTF)
|
||
})
|
||
return fontsErr
|
||
}
|
||
|
||
func face(f *opentype.Font, size float64) (font.Face, error) {
|
||
return opentype.NewFace(f, &opentype.FaceOptions{Size: size, DPI: 72, Hinting: font.HintingFull})
|
||
}
|
||
|
||
const margin = 80
|
||
|
||
// Render draws c as a PNG.
|
||
func Render(c Card) ([]byte, error) {
|
||
if err := loadFonts(); err != nil {
|
||
return nil, err
|
||
}
|
||
accent := c.Accent
|
||
if accent == nil {
|
||
accent = Ink
|
||
}
|
||
img := image.NewRGBA(image.Rect(0, 0, Width, Height))
|
||
draw.Draw(img, img.Bounds(), image.NewUniform(Paper), image.Point{}, draw.Src)
|
||
// A band in the accent colour along the top, and a rule above the footer.
|
||
fill(img, image.Rect(0, 0, Width, 14), accent)
|
||
fill(img, image.Rect(margin, Height-130, Width-margin, Height-128), Rule)
|
||
|
||
brand, err := face(bold, 30)
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
drawText(img, brand, Ink, margin, 104, "UptimeSebi")
|
||
|
||
// The title shrinks for long names and wraps onto at most two lines.
|
||
y := 230
|
||
for _, size := range []float64{76, 64, 54} {
|
||
title, err := face(bold, size)
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
lines := wrap(title, c.Title, Width-2*margin, 2)
|
||
if len(lines) == 1 || size == 54 {
|
||
lh := int(size * 1.15)
|
||
for _, l := range lines {
|
||
drawText(img, title, Ink, margin, y, l)
|
||
y += lh
|
||
}
|
||
break
|
||
}
|
||
}
|
||
|
||
if c.Subtitle != "" {
|
||
sub, err := face(regular, 38)
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
y += 30
|
||
dot(img, margin+14, y-13, 14, accent)
|
||
lines := wrap(sub, c.Subtitle, Width-2*margin-48, 1)
|
||
drawText(img, sub, Ink, margin+48, y, lines[0])
|
||
}
|
||
|
||
if c.Footer != "" {
|
||
foot, err := face(regular, 28)
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
drawText(img, foot, Muted, margin, Height-72, wrap(foot, c.Footer, Width-2*margin, 1)[0])
|
||
}
|
||
|
||
var buf bytes.Buffer
|
||
if err := png.Encode(&buf, img); err != nil {
|
||
return nil, err
|
||
}
|
||
return buf.Bytes(), nil
|
||
}
|
||
|
||
func fill(img *image.RGBA, r image.Rectangle, c color.Color) {
|
||
draw.Draw(img, r, image.NewUniform(c), image.Point{}, draw.Src)
|
||
}
|
||
|
||
// dot draws a filled circle of radius r around (cx, cy), smoothing its edge.
|
||
func dot(img *image.RGBA, cx, cy, r int, c color.Color) {
|
||
cr, cg, cb, _ := c.RGBA()
|
||
for y := cy - r - 1; y <= cy+r+1; y++ {
|
||
for x := cx - r - 1; x <= cx+r+1; x++ {
|
||
// Coverage from the distance of the pixel centre to the edge.
|
||
dx, dy := float64(x-cx)+0.5, float64(y-cy)+0.5
|
||
d := float64(r) - math.Sqrt(dx*dx+dy*dy) + 0.5
|
||
if d <= 0 {
|
||
continue
|
||
}
|
||
if d > 1 {
|
||
d = 1
|
||
}
|
||
bg := img.RGBAAt(x, y)
|
||
mix := func(fg uint32, b uint8) uint8 { return uint8(float64(fg>>8)*d + float64(b)*(1-d)) }
|
||
img.SetRGBA(x, y, color.RGBA{mix(cr, bg.R), mix(cg, bg.G), mix(cb, bg.B), 0xff})
|
||
}
|
||
}
|
||
}
|
||
|
||
func drawText(img *image.RGBA, f font.Face, c color.Color, x, y int, s string) {
|
||
d := font.Drawer{Dst: img, Src: image.NewUniform(c), Face: f, Dot: fixed.P(x, y)}
|
||
d.DrawString(s)
|
||
}
|
||
|
||
// wrap breaks s into lines no wider than width, at most max of them; the
|
||
// last one ends in an ellipsis when s does not fit.
|
||
func wrap(f font.Face, s string, width, max int) []string {
|
||
words := strings.Fields(s)
|
||
if len(words) == 0 {
|
||
return []string{""}
|
||
}
|
||
limit := fixed.I(width)
|
||
var lines []string
|
||
line := ""
|
||
for i, w := range words {
|
||
next := strings.TrimSpace(line + " " + w)
|
||
if font.MeasureString(f, next) <= limit {
|
||
line = next
|
||
continue
|
||
}
|
||
if line != "" {
|
||
lines = append(lines, line)
|
||
}
|
||
line = w
|
||
if len(lines) == max {
|
||
// Out of lines: finish the last one with what is left.
|
||
rest := strings.Join(words[i:], " ")
|
||
lines[max-1] = ellipsize(f, lines[max-1]+" "+rest, limit)
|
||
return lines
|
||
}
|
||
}
|
||
return append(lines, ellipsize(f, line, limit))
|
||
}
|
||
|
||
// ellipsize shortens s until it fits limit, marking the cut with "…".
|
||
func ellipsize(f font.Face, s string, limit fixed.Int26_6) string {
|
||
if font.MeasureString(f, s) <= limit {
|
||
return s
|
||
}
|
||
r := []rune(s)
|
||
for len(r) > 0 && font.MeasureString(f, string(r)+"…") > limit {
|
||
r = r[:len(r)-1]
|
||
}
|
||
return strings.TrimSpace(string(r)) + "…"
|
||
}
|