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:
+60
-1
@@ -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()
|
||||
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"io"
|
||||
"log/slog"
|
||||
"net/http"
|
||||
@@ -300,3 +301,75 @@ func TestStatus(t *testing.T) {
|
||||
t.Fatal("status must never expose the admin token")
|
||||
}
|
||||
}
|
||||
|
||||
// keeper is a RegionKeeper that follows the node's real regions.
|
||||
type keeper struct {
|
||||
node *p2p.Node
|
||||
fail error
|
||||
}
|
||||
|
||||
func (k *keeper) Regions() []string { return k.node.Regions() }
|
||||
func (k *keeper) SetRegions(r []string) error {
|
||||
if k.fail != nil {
|
||||
return k.fail
|
||||
}
|
||||
return k.node.SetRegions(r)
|
||||
}
|
||||
|
||||
func TestOwnerChangesTheRegions(t *testing.T) {
|
||||
k := &keeper{}
|
||||
f := newFixture(t, token, func(c *Config) {
|
||||
k.node = c.Node
|
||||
c.RegionKeeper = k
|
||||
})
|
||||
regions := func() []string {
|
||||
_, body := f.do(t, "GET", "/api/v1/status", "", false)
|
||||
var st struct {
|
||||
Regions []string `json:"regions"`
|
||||
}
|
||||
_ = json.Unmarshal(body, &st)
|
||||
return st.Regions
|
||||
}
|
||||
if got := regions(); len(got) != 1 || got[0] != "u1x" {
|
||||
t.Fatalf("regions = %v", got)
|
||||
}
|
||||
|
||||
if code, _ := f.do(t, "PUT", "/api/v1/regions", `{"regions":["u1j"]}`, false); code != 401 {
|
||||
t.Fatalf("without the token: %d, want 401", code)
|
||||
}
|
||||
code, body := f.do(t, "PUT", "/api/v1/regions", `{"regions":[" U1J ","u1x","u1j"]}`, true)
|
||||
if code != 200 {
|
||||
t.Fatalf("PUT: %d %s", code, body)
|
||||
}
|
||||
if got := regions(); len(got) != 2 || got[0] != "u1x" || got[1] != "u1j" {
|
||||
t.Fatalf("regions after the change (case and repeats tidied) = %v", got)
|
||||
}
|
||||
|
||||
for name, tc := range map[string]struct{ body, want string }{
|
||||
"not a region": {`{"regions":["u1j","nope"]}`, "not a region"},
|
||||
"none": {`{"regions":[]}`, "at least one"},
|
||||
"unknown field": {`{"regions":["u1j"],"x":1}`, "invalid JSON"},
|
||||
"too many": {`{"regions":["u00","u01","u02","u03","u04","u05","u06","u07","u08","u09","u0b",` +
|
||||
`"u0c","u0d","u0e","u0f","u0g","u0h","u0j","u0k","u0m","u0n"]}`, "too many"},
|
||||
} {
|
||||
code, body := f.do(t, "PUT", "/api/v1/regions", tc.body, true)
|
||||
if code != 400 || !strings.Contains(string(body), tc.want) {
|
||||
t.Errorf("%s: %d %s, want 400 containing %q", name, code, body, tc.want)
|
||||
}
|
||||
}
|
||||
if got := regions(); len(got) != 2 {
|
||||
t.Fatalf("a refused change must change nothing, regions = %v", got)
|
||||
}
|
||||
|
||||
k.fail = errors.New("disk full")
|
||||
if code, body := f.do(t, "PUT", "/api/v1/regions", `{"regions":["u1j"]}`, true); code != 500 || !strings.Contains(string(body), "disk full") {
|
||||
t.Fatalf("a failing keeper: %d %s", code, body)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRegionsCannotBeChangedWithoutAKeeper(t *testing.T) {
|
||||
f := newFixture(t, token)
|
||||
if code, _ := f.do(t, "PUT", "/api/v1/regions", `{"regions":["u1j"]}`, true); code == 200 {
|
||||
t.Fatal("regions changed on a node that has no keeper")
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user