diff --git a/README.md b/README.md index e65e554..3132796 100644 --- a/README.md +++ b/README.md @@ -24,6 +24,7 @@ Configuration files are written using [toml](https://toml.io). Top level configu - `host` - a hostname to serve this relay on. - `schema` - a string that identifies this relay. This cannot be changed, and must be usable as a sqlite identifier. - `secret` - the nostr secret key of the relay. Will be used to populate the relay's NIP 11 `self` field and sign generated events. +- `inactive` - a boolean indicating whether the relay is currently inactive. The relay will not be available if set. ### `[info]` diff --git a/zooid/config.go b/zooid/config.go index 7ffb580..389ea44 100644 --- a/zooid/config.go +++ b/zooid/config.go @@ -16,10 +16,11 @@ type Role struct { } type Config struct { - Host string `toml:"host" json:"host"` - Schema string `toml:"schema" json:"schema"` - Secret string `toml:"secret" json:"secret"` - Info struct { + Host string `toml:"host" json:"host"` + Schema string `toml:"schema" json:"schema"` + Secret string `toml:"secret" json:"secret"` + Inactive bool `toml:"inactive" json:"inactive"` + Info struct { Name string `toml:"name" json:"name"` Icon string `toml:"icon" json:"icon"` Pubkey string `toml:"pubkey" json:"pubkey"` diff --git a/zooid/lib.go b/zooid/lib.go index 8493e35..8427b0d 100644 --- a/zooid/lib.go +++ b/zooid/lib.go @@ -21,6 +21,9 @@ func Dispatch(hostname string) (*Instance, bool) { defer instancesMux.RUnlock() instance, exists := instancesByHost[hostname] + if !exists || instance.Config.Inactive { + return nil, false + } return instance, exists } @@ -60,6 +63,8 @@ func Start() { if err != nil { log.Printf("Failed to make instance for %s: %v", entry.Name(), err) + } else if instance.Config.Inactive { + log.Printf("Skipped inactive %s", entry.Name()) } else { instancesByHost[instance.Config.Host] = instance instancesByName[entry.Name()] = instance @@ -105,6 +110,8 @@ func Start() { instance, err := MakeInstance(filename) if err != nil { log.Printf("Failed to reload %s: %v", filename, err) + } else if instance.Config.Inactive { + log.Printf("Skipped inactive %s", filename) } else { instancesByHost[instance.Config.Host] = instance instancesByName[filename] = instance diff --git a/zooid/lib_test.go b/zooid/lib_test.go new file mode 100644 index 0000000..eb671dd --- /dev/null +++ b/zooid/lib_test.go @@ -0,0 +1,26 @@ +package zooid + +import "testing" + +func TestDispatch_IgnoresInactiveInstances(t *testing.T) { + instancesOnce.Do(func() {}) + + instancesMux.Lock() + instancesByHost = map[string]*Instance{ + "active.example.com": { + Config: &Config{Host: "active.example.com"}, + }, + "inactive.example.com": { + Config: &Config{Host: "inactive.example.com", Inactive: true}, + }, + } + instancesMux.Unlock() + + if _, exists := Dispatch("active.example.com"); !exists { + t.Fatal("expected active instance to be dispatched") + } + + if _, exists := Dispatch("inactive.example.com"); exists { + t.Fatal("expected inactive instance to not be dispatched") + } +}