Files
uptimesebi/internal/api/publicregions.go
T
Sebastian UnterschützandClaude Opus 5.5 23717afec9
ci / lint (push) Skipped
ci / test (push) Skipped
ci / build (push) Skipped
ci / image (push) Skipped
ci / lint (pull_request) Successful in 11m21s
ci / test (pull_request) Successful in 13m31s
ci / build (pull_request) Successful in 18m20s
ci / image (pull_request) Successful in 5m47s
feat(regions): public region list and touch-friendly maps
GET /api/v1/public/regions lists the platform regions with their
location and whether a checker is online, without authentication and to
any origin, for the website's front page. Each instance answers from
memory for 30 seconds; organizations' own regions are never listed.

On touch screens one finger scrolls the page and two move the map, the
pins are larger, and a tap no longer leaves rows highlighted. The
gesture hints are translated, and the regions table drops the key and
owner columns on phones so the location fits.

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

83 lines
2.3 KiB
Go

package api
import (
"encoding/json"
"net/http"
"sync"
"time"
"github.com/gin-gonic/gin"
"git.zb-server.de/ZB-Server/uptimesebi/internal/store"
"git.zb-server.de/ZB-Server/uptimesebi/internal/tenant"
)
// publicRegionsTTL is how long an instance serves the platform regions
// from memory, so the endpoint costs the database next to nothing however
// often a front page asks.
const publicRegionsTTL = 30 * time.Second
// publicRegion is a platform region as anyone may see it. Organizations'
// own regions are never listed.
type publicRegion struct {
Key string `json:"key"`
Name string `json:"name"`
// Latitude and longitude are absent for regions without a location.
Latitude *float64 `json:"latitude,omitempty"`
Longitude *float64 `json:"longitude,omitempty"`
// Online is set while at least one checker of the region is connected.
Online bool `json:"online"`
}
type publicRegionsCache struct {
mu sync.Mutex
body []byte
expires time.Time
}
// listPublicRegions answers without authentication and to any origin, for
// a website showing where the platform checks from.
func (s *Server) listPublicRegions(c *gin.Context) {
c.Header("Access-Control-Allow-Origin", "*")
c.Header("Cache-Control", "public, max-age=60")
body, err := s.publicRegionsBody(c)
if err != nil {
internalError(c, err)
return
}
c.Data(http.StatusOK, "application/json; charset=utf-8", body)
}
func (s *Server) publicRegionsBody(c *gin.Context) ([]byte, error) {
cache := &s.publicRegions
cache.mu.Lock()
defer cache.mu.Unlock()
if cache.body != nil && time.Now().Before(cache.expires) {
return cache.body, nil
}
ctx := c.Request.Context()
var rows []store.Region
err := s.db.Tx(ctx, tenant.System(), func(q *store.Queries) error {
var err error
rows, err = q.ListPlatformRegions(ctx)
return err
})
if err != nil {
return nil, err
}
out := make([]publicRegion, len(rows))
for i, r := range rows {
n, err := s.sched.Instances(ctx, r.ID.String())
if err != nil {
return nil, err
}
out[i] = publicRegion{Key: r.Key, Name: r.Name, Latitude: r.Latitude, Longitude: r.Longitude, Online: n > 0}
}
body, err := json.Marshal(gin.H{"regions": out})
if err != nil {
return nil, err
}
cache.body, cache.expires = body, time.Now().Add(publicRegionsTTL)
return body, nil
}