Let the owner change regions by place name, without a restart

The p2p node can now add and drop regions while running (SetRegions):
topics, validators, subscriptions and per-region peer discovery start and
stop with them, and a newly followed region catches up by sync.

PUT /api/v1/regions (owner token) applies a change and the node saves it in
regions.json, which wins over --region from then on. The owner's browser
gets a "Regions" page: search a place by name (GeoNames cities, offline,
CC BY 4.0), follow its cell plus any cell within about 40 km.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01AeFqV96dVeBX94inzcgZKw
This commit is contained in:
2026-11-18 10:28:16 +01:00
co-authored by Claude Sonnet 5.5
parent e7bec4fb63
commit fd439710df
24 changed files with 70080 additions and 42 deletions
+60 -1
View File
@@ -14,6 +14,7 @@ import (
"fmt"
"io"
"net/http"
"slices"
"strconv"
"strings"
"time"
@@ -49,6 +50,26 @@ type Config struct {
// Inbox is the owner's conversations. Without it the owner endpoints are
// absent; relaying messages for buyers still works.
Inbox *inbox.Inbox
// RegionKeeper lets the owner change the regions the node follows while it
// runs. Without it Regions is fixed and the endpoint is absent.
RegionKeeper RegionKeeper
}
// RegionKeeper holds the regions a node follows: it applies and remembers a change.
type RegionKeeper interface {
Regions() []string
SetRegions([]string) error
}
// MaxRegions is how many regions one node may follow: it replicates everything
// in them, so this keeps "follow the world" from being one click.
const MaxRegions = 20
func (s *server) regions() []string {
if s.RegionKeeper != nil {
return s.RegionKeeper.Regions()
}
return s.Regions
}
type server struct {
@@ -72,6 +93,9 @@ func New(cfg Config) http.Handler {
mux.HandleFunc("POST /api/v1/listings", s.admin(s.create))
mux.HandleFunc("PUT /api/v1/listings/{id}", s.admin(s.update))
mux.HandleFunc("DELETE /api/v1/listings/{id}", s.admin(s.remove))
if cfg.RegionKeeper != nil {
mux.HandleFunc("PUT /api/v1/regions", s.admin(s.putRegions))
}
// Messaging: public relay endpoints for buyers' browsers...
mux.HandleFunc("POST /api/v1/messages", s.postMessage)
@@ -131,7 +155,7 @@ func (s *server) status(w http.ResponseWriter, r *http.Request) {
"addrs": s.Node.Addrs(),
"author": s.Device.Cert.Author,
"device": s.Device.Cert.Device,
"regions": s.Regions,
"regions": s.regions(),
"peers": s.Node.PeerCount(),
"listings": n,
// Lets the browser report how much anti-spam work a listing carries.
@@ -266,6 +290,41 @@ func (s *server) admin(next http.HandlerFunc) http.HandlerFunc {
}
}
// putRegions changes the regions the node follows, at once and for good.
func (s *server) putRegions(w http.ResponseWriter, r *http.Request) {
var body struct {
Regions []string `json:"regions"`
}
if !decodeBody(w, r, &body) {
return
}
var regions []string
for _, raw := range body.Regions {
region := strings.ToLower(strings.TrimSpace(raw))
if !listing.ValidRegion(region) {
fail(w, http.StatusBadRequest, fmt.Sprintf(
"%q is not a region: use %d geohash characters, e.g. u1j for Bonn", raw, listing.RegionPrecision))
return
}
if !slices.Contains(regions, region) {
regions = append(regions, region)
}
}
switch {
case len(regions) == 0:
fail(w, http.StatusBadRequest, "choose at least one region")
return
case len(regions) > MaxRegions:
fail(w, http.StatusBadRequest, fmt.Sprintf("that is too many regions (at most %d)", MaxRegions))
return
}
if err := s.RegionKeeper.SetRegions(regions); err != nil {
fail(w, http.StatusInternalServerError, "could not change the regions: "+err.Error())
return
}
writeJSON(w, http.StatusOK, map[string]any{"regions": s.RegionKeeper.Regions()})
}
func (s *server) readDraft(w http.ResponseWriter, r *http.Request) (*draft, bool) {
dec := json.NewDecoder(http.MaxBytesReader(w, r.Body, maxBody))
dec.DisallowUnknownFields()