Add handler

This commit is contained in:
Jon Staab
2025-09-23 16:44:26 -07:00
parent bb0269b728
commit 6642cba977
13 changed files with 314 additions and 3 deletions
+45
View File
@@ -0,0 +1,45 @@
package zooid
import (
"fmt"
"github.com/BurntSushi/toml"
"path/filepath"
)
type Config struct {
Self struct {
Name string `toml:"name"`
Icon string `toml:"icon"`
Secret string `toml:"secret"`
Pubkey string `toml:"pubkey"`
Description string `toml:"description"`
} `toml:"self"`
Groups struct {
Enabled bool `toml:"enabled"`
AutoJoin bool `toml:"auto_join"`
AutoLeave bool `toml:"auto_leave"`
} `toml:"groups"`
Roles map[string]struct {
Pubkeys []string `toml:"pubkeys"`
Nip86Methods []string `toml:"nip86_methods"`
CanInvite bool `toml:"can_invite"`
} `toml:"roles"`
Data struct {
Sqlite string `toml:"sqlite"`
Media string `toml:"media"`
} `toml:"data"`
}
func LoadConfig(hostname string) (*Config, error) {
path := filepath.Join("configs", hostname)
var config Config
if _, err := toml.DecodeFile(path, &config); err != nil {
return nil, fmt.Errorf("failed to parse config file %s: %w", path, err)
}
return &config, nil
}
+17
View File
@@ -0,0 +1,17 @@
package zooid
import (
"log"
"net/http"
)
func ServeHTTP(w http.ResponseWriter, r *http.Request) {
relay, err := GetRelay(r.Host)
if err != nil {
log.Printf("Failed to load relay config for hostname %s: %v", r.Host, err)
http.Error(w, "Not Found", http.StatusNotFound)
return
}
relay.ServeHTTP(w, r)
}
+38
View File
@@ -0,0 +1,38 @@
package zooid
import (
"fiatjaf.com/nostr/khatru"
"sync"
)
func MakeRelay(hostname string, config *Config) *khatru.Relay {
relay := khatru.NewRelay()
return relay
}
var (
relays map[string]*khatru.Relay
relayOnce sync.Once
)
func GetRelay(hostname string) (*khatru.Relay, error) {
relayOnce.Do(func() {
relays = make(map[string]*khatru.Relay)
})
relay, exists := relays[hostname]
if !exists {
config, err := LoadConfig(hostname)
if err != nil {
return nil, err
}
newRelay := MakeRelay(hostname, config)
relays[hostname] = newRelay
relay = newRelay
}
return relay, nil
}
+31
View File
@@ -0,0 +1,31 @@
package zooid
import (
"os"
"strings"
"sync"
)
var (
env map[string]string
envOnce sync.Once
)
func Env(k string, fallback ...string) (v string) {
envOnce.Do(func() {
env = make(map[string]string)
for _, item := range os.Environ() {
parts := strings.SplitN(item, "=", 2)
env[parts[0]] = parts[1]
}
})
v = env[k]
if v == "" && len(fallback) > 0 {
v = fallback[0]
}
return v
}