Compare commits
1 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 100c771d55 |
+32
-3
@@ -13,6 +13,7 @@ import (
|
||||
|
||||
"fiatjaf.com/nostr"
|
||||
"github.com/BurntSushi/toml"
|
||||
"github.com/gosimple/slug"
|
||||
)
|
||||
|
||||
// APIHandler handles REST API requests for managing virtual relays
|
||||
@@ -126,14 +127,42 @@ func (api *APIHandler) resolveRelayMembers(id string) ([]string, error) {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
instance, err := MakeInstanceFromPath(configPath)
|
||||
config, err := api.loadConfigFromPath(configPath)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer instance.Cleanup()
|
||||
|
||||
memberSet := make(map[string]struct{})
|
||||
for _, pubkey := range instance.Management.GetMembers() {
|
||||
|
||||
if owner, err := nostr.PubKeyFromHex(config.Info.Pubkey); err == nil {
|
||||
memberSet[owner.Hex()] = struct{}{}
|
||||
}
|
||||
|
||||
if config.Secret != "" {
|
||||
if secret, err := nostr.SecretKeyFromHex(config.Secret); err == nil {
|
||||
memberSet[secret.Public().Hex()] = struct{}{}
|
||||
}
|
||||
}
|
||||
|
||||
for _, role := range config.Roles {
|
||||
for _, pubkey := range role.Pubkeys {
|
||||
if parsed, err := nostr.PubKeyFromHex(pubkey); err == nil {
|
||||
memberSet[parsed.Hex()] = struct{}{}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
events := &EventStore{
|
||||
Config: config,
|
||||
Schema: &Schema{Name: slug.Make(config.Schema)},
|
||||
}
|
||||
|
||||
management := &ManagementStore{
|
||||
Config: config,
|
||||
Events: events,
|
||||
}
|
||||
|
||||
for _, pubkey := range management.GetMembers() {
|
||||
memberSet[pubkey.Hex()] = struct{}{}
|
||||
}
|
||||
|
||||
|
||||
+1
-1
@@ -681,7 +681,7 @@ func TestAPIHandler_ListRelayMembers(t *testing.T) {
|
||||
|
||||
config := &Config{
|
||||
Host: "members.example.com",
|
||||
Schema: "members_" + RandomString(8),
|
||||
Schema: "members_relay",
|
||||
Secret: relaySecret.Hex(),
|
||||
Roles: map[string]Role{
|
||||
"admin": {
|
||||
|
||||
@@ -64,11 +64,6 @@ type Config struct {
|
||||
func LoadConfig(filename string) (*Config, error) {
|
||||
path := filepath.Join(Env("CONFIG"), filename)
|
||||
|
||||
return LoadConfigFromPath(path)
|
||||
}
|
||||
|
||||
func LoadConfigFromPath(path string) (*Config, error) {
|
||||
|
||||
var config Config
|
||||
if _, err := toml.DecodeFile(path, &config); err != nil {
|
||||
return nil, fmt.Errorf("Failed to parse config file %s: %w", path, err)
|
||||
|
||||
+1
-14
@@ -28,19 +28,6 @@ func MakeInstance(filename string) (*Instance, error) {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return makeInstance(config, filename)
|
||||
}
|
||||
|
||||
func MakeInstanceFromPath(path string) (*Instance, error) {
|
||||
config, err := LoadConfigFromPath(path)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return makeInstance(config, path)
|
||||
}
|
||||
|
||||
func makeInstance(config *Config, source string) (*Instance, error) {
|
||||
relay := khatru.NewRelay()
|
||||
|
||||
events := &EventStore{
|
||||
@@ -135,7 +122,7 @@ func makeInstance(config *Config, source string) (*Instance, error) {
|
||||
// Initialize the database
|
||||
|
||||
if err := instance.Events.Init(); err != nil {
|
||||
log.Fatal("Failed to initialize event store for ", source, ": ", err)
|
||||
log.Fatal("Failed to initialize event store for ", filename, ": ", err)
|
||||
}
|
||||
|
||||
// Enable extra functionality
|
||||
|
||||
+2
-11
@@ -28,15 +28,6 @@ func Dispatch(hostname string) (*Instance, bool) {
|
||||
return instance, exists
|
||||
}
|
||||
|
||||
func cleanupIfInactive(instance *Instance) bool {
|
||||
if instance != nil && instance.Config != nil && instance.Config.Inactive {
|
||||
instance.Cleanup()
|
||||
return true
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
func Start() {
|
||||
dataDir := Env("DATA")
|
||||
if err := os.MkdirAll(dataDir, 0755); err != nil {
|
||||
@@ -72,7 +63,7 @@ func Start() {
|
||||
|
||||
if err != nil {
|
||||
log.Printf("Failed to make instance for %s: %v", entry.Name(), err)
|
||||
} else if cleanupIfInactive(instance) {
|
||||
} else if instance.Config.Inactive {
|
||||
log.Printf("Skipped inactive %s", entry.Name())
|
||||
} else {
|
||||
instancesByHost[instance.Config.Host] = instance
|
||||
@@ -119,7 +110,7 @@ func Start() {
|
||||
instance, err := MakeInstance(filename)
|
||||
if err != nil {
|
||||
log.Printf("Failed to reload %s: %v", filename, err)
|
||||
} else if cleanupIfInactive(instance) {
|
||||
} else if instance.Config.Inactive {
|
||||
log.Printf("Skipped inactive %s", filename)
|
||||
} else {
|
||||
instancesByHost[instance.Config.Host] = instance
|
||||
|
||||
Reference in New Issue
Block a user