Files
allmende/cmd/allmende/regions.go
T
nilsandClaude Sonnet 5.5 fd439710df 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
2026-11-18 10:28:16 +01:00

88 lines
2.3 KiB
Go

package main
import (
"encoding/json"
"errors"
"fmt"
"os"
"path/filepath"
"sync"
"git.stinnesbeck.com/nils/allmende/internal/listing"
"git.stinnesbeck.com/nils/allmende/internal/p2p"
)
// regionsFile remembers the regions an owner chose in the browser. Once it
// exists it decides which regions the node follows; --region only seeds it.
const regionsFile = "regions.json"
// loadRegions returns the regions saved in dir; ok is false if none were saved.
func loadRegions(dir string) (regions []string, ok bool, err error) {
data, err := os.ReadFile(filepath.Join(dir, regionsFile))
if errors.Is(err, os.ErrNotExist) {
return nil, false, nil
}
if err != nil {
return nil, false, err
}
var f struct {
Regions []string `json:"regions"`
}
if err := json.Unmarshal(data, &f); err != nil {
return nil, false, fmt.Errorf("%s: %w", regionsFile, err)
}
for _, r := range f.Regions {
if !listing.ValidRegion(r) {
return nil, false, fmt.Errorf("%s: %q is not a region", regionsFile, r)
}
}
return f.Regions, len(f.Regions) > 0, nil
}
// saveRegions writes the regions so a restart keeps them. The file is replaced
// whole, so a crash never leaves half of one.
func saveRegions(dir string, regions []string) error {
data, err := json.MarshalIndent(map[string][]string{"regions": regions}, "", " ")
if err != nil {
return err
}
tmp, err := os.CreateTemp(dir, regionsFile+".*")
if err != nil {
return err
}
defer os.Remove(tmp.Name())
if _, err := tmp.Write(data); err != nil {
tmp.Close()
return err
}
if err := tmp.Close(); err != nil {
return err
}
return os.Rename(tmp.Name(), filepath.Join(dir, regionsFile))
}
// regionKeeper applies a change of regions to the running node and saves it.
type regionKeeper struct {
node *p2p.Node
dir string
mu sync.Mutex // one change at a time
}
func (k *regionKeeper) Regions() []string { return k.node.Regions() }
func (k *regionKeeper) SetRegions(regions []string) error {
k.mu.Lock()
defer k.mu.Unlock()
before := k.node.Regions()
if err := k.node.SetRegions(regions); err != nil {
return err
}
if err := saveRegions(k.dir, regions); err != nil {
// Not remembered means the change would silently vanish at the next
// start: undo it instead.
k.node.SetRegions(before)
return err
}
return nil
}