Files
beacon-server/internal/api/info.go
T

83 lines
3.0 KiB
Go

// Copyright 2026 Beacon Contributors
// SPDX-License-Identifier: AGPL-3.0-or-later
package api
import (
"strconv"
"strings"
)
// Client floors this server code needs. Bump the matching one when a release breaks older
// clients; config can raise them, never lower them. Empty means no requirement.
const (
MinAppVersion = "2.1.0" // BEACON Mobile: exact page cursors and waiting out busy-server 503s
MinWebVersion = "2.1.0" // Beacon Web: exact page cursors and waiting out read-model 503s
// BEACON Mobile: the first release that sends a /ws ticket, required while websocket.require_origin is on
TicketAppVersion = "2.1.1"
)
// AppFloor is the oldest BEACON Mobile release this server accepts, given its config.
func AppFloor(configured string, requireOrigin bool) string {
floor := HigherVersion(MinAppVersion, configured)
if requireOrigin {
floor = HigherVersion(floor, TicketAppVersion)
}
return floor
}
// Info is the public server description clients check before using a server.
type Info struct {
// MinAppVersion is the oldest allowed app version (X.Y.Z); null means no requirement.
MinAppVersion *string `json:"minAppVersion" example:"0.1.1"`
// MinWebVersion is the oldest allowed Beacon Web version (X.Y.Z); null means no requirement.
MinWebVersion *string `json:"minWebVersion" example:"2.0.3"`
ServerVersion string `json:"serverVersion" example:"2.0.2"`
// Features tells clients which optional parts of the API this server offers.
Features InfoFeatures `json:"features"`
}
// InfoFeatures lists the optional features a server offers. Clients hide what is off.
type InfoFeatures struct {
Polling bool `json:"polling" example:"true"`
Accounts string `json:"accounts" example:"off"`
// Registration is true when new sign-ups are open; only meaningful when accounts is "local".
Registration bool `json:"registration" example:"false"`
TelemetryRequests bool `json:"telemetryRequests" example:"false"`
Alerts bool `json:"alerts" example:"false"`
// AlertDelivery is "push" or "email" while alerts is true.
AlertDelivery string `json:"alertDelivery,omitempty" enums:"push,email"`
// PortalURL is the sign-in portal's base URL; set only when accounts is "portal".
PortalURL string `json:"portalUrl,omitempty" example:"https://portal.example.org"`
}
// WSTicket lets a client that sends no Origin open /ws, in the X-Beacon-Ticket header.
type WSTicket struct {
Ticket string `json:"ticket" example:"AAABmZ1s4CgLBvU1n1yN3v8hQm9YtVq2"`
// ExpiresAt is when the ticket stops opening /ws, in epoch ms.
ExpiresAt int64 `json:"expiresAt" example:"1793470167000"`
}
// HigherVersion returns the newer of two X.Y.Z versions; empty sorts oldest.
func HigherVersion(a, b string) string {
if versionLess(a, b) {
return b
}
return a
}
func versionLess(a, b string) bool {
if a == "" || b == "" {
return a == "" && b != ""
}
pa, pb := strings.Split(a, "."), strings.Split(b, ".")
for i := 0; i < len(pa) && i < len(pb); i++ {
x, _ := strconv.Atoi(pa[i])
y, _ := strconv.Atoi(pb[i])
if x != y {
return x < y
}
}
return len(pa) < len(pb)
}