From fcf873690fc35636c6efc4387268c5ba8628d343 Mon Sep 17 00:00:00 2001 From: Jon Staab Date: Wed, 1 Oct 2025 07:52:19 -0700 Subject: [PATCH] Add http serving --- templates/index.html | 1 + zooid/instance.go | 96 ++++++++++++++++++++++++-------------------- 2 files changed, 54 insertions(+), 43 deletions(-) create mode 100644 templates/index.html diff --git a/templates/index.html b/templates/index.html new file mode 100644 index 0000000..45b983b --- /dev/null +++ b/templates/index.html @@ -0,0 +1 @@ +hi diff --git a/zooid/instance.go b/zooid/instance.go index 3dbf2a8..b24fa61 100644 --- a/zooid/instance.go +++ b/zooid/instance.go @@ -54,6 +54,8 @@ func MakeInstance(filename string) (*Instance, error) { Relay: khatru.NewRelay(), } + // NIP 11 info + instance.Relay.Negentropy = true instance.Relay.Info.Name = config.Info.Name instance.Relay.Info.Icon = config.Info.Icon @@ -71,7 +73,7 @@ func MakeInstance(filename string) (*Instance, error) { instance.Relay.Info.PubKey = &pubkey } - instance.Relay.UseEventstore(instance.Events, 400) + // Handlers instance.Relay.OnConnect = instance.OnConnect instance.Relay.OnEvent = instance.OnEvent @@ -85,6 +87,14 @@ func MakeInstance(filename string) (*Instance, error) { instance.Relay.RejectConnection = instance.RejectConnection instance.Relay.PreventBroadcast = instance.PreventBroadcast + // HTTP request handling + + router := instance.Relay.Router() + + router.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { + http.ServeFile(w, r, "templates/index.html") + }) + // Initialize stuff if err := instance.Events.Init(); err != nil { @@ -406,59 +416,59 @@ func (instance *Instance) OnRequest(ctx context.Context, filter nostr.Filter) (r func (instance *Instance) QueryStored(ctx context.Context, filter nostr.Filter) iter.Seq[nostr.Event] { return func(yield func(nostr.Event) bool) { if khatru.IsInternalCall(ctx) { - for event := range instance.Events.QueryEvents(filter, 0) { - if !yield(event) { - return - } - } + for event := range instance.Events.QueryEvents(filter, 0) { + if !yield(event) { + return + } + } } else { - pubkey, isAuthed := khatru.GetAuthed(ctx) + pubkey, isAuthed := khatru.GetAuthed(ctx) - if !isAuthed { - log.Panic("Unauthorized user was allowed to query events") - } + if !isAuthed { + log.Panic("Unauthorized user was allowed to query events") + } - stripSignature := func(event nostr.Event) nostr.Event { - if instance.Config.Policy.StripSignatures && !instance.Config.IsAdmin(pubkey) { - var zeroSig [64]byte - event.Sig = zeroSig - } + stripSignature := func(event nostr.Event) nostr.Event { + if instance.Config.Policy.StripSignatures && !instance.Config.IsAdmin(pubkey) { + var zeroSig [64]byte + event.Sig = zeroSig + } - return event - } + return event + } - if slices.Contains(filter.Kinds, AUTH_INVITE) && instance.Config.CanInvite(pubkey) { - if !yield(stripSignature(instance.GenerateInviteEvent(pubkey))) { - return - } - } + if slices.Contains(filter.Kinds, AUTH_INVITE) && instance.Config.CanInvite(pubkey) { + if !yield(stripSignature(instance.GenerateInviteEvent(pubkey))) { + return + } + } - for event := range instance.Events.QueryEvents(filter, 1000) { - // We save some ephemeral events for bookkeeping, don't return them - if event.Kind.IsEphemeral() { - continue - } + for event := range instance.Events.QueryEvents(filter, 1000) { + // We save some ephemeral events for bookkeeping, don't return them + if event.Kind.IsEphemeral() { + continue + } - h := GetGroupIDFromEvent(event) + h := GetGroupIDFromEvent(event) - if h != "" { - if !instance.Config.Groups.Enabled { - continue - } + if h != "" { + if !instance.Config.Groups.Enabled { + continue + } - if !instance.HasGroupAccess(h, pubkey) { - continue - } - } + if !instance.HasGroupAccess(h, pubkey) { + continue + } + } - if !instance.Config.Groups.Enabled && slices.Contains(nip29.MetadataEventKinds, event.Kind) { - continue - } + if !instance.Config.Groups.Enabled && slices.Contains(nip29.MetadataEventKinds, event.Kind) { + continue + } - if !yield(event) { - return - } - } + if !yield(event) { + return + } + } } } }