forked from coracle/zooid
Add http serving
This commit is contained in:
@@ -0,0 +1 @@
|
|||||||
|
hi
|
||||||
+53
-43
@@ -54,6 +54,8 @@ func MakeInstance(filename string) (*Instance, error) {
|
|||||||
Relay: khatru.NewRelay(),
|
Relay: khatru.NewRelay(),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// NIP 11 info
|
||||||
|
|
||||||
instance.Relay.Negentropy = true
|
instance.Relay.Negentropy = true
|
||||||
instance.Relay.Info.Name = config.Info.Name
|
instance.Relay.Info.Name = config.Info.Name
|
||||||
instance.Relay.Info.Icon = config.Info.Icon
|
instance.Relay.Info.Icon = config.Info.Icon
|
||||||
@@ -71,7 +73,7 @@ func MakeInstance(filename string) (*Instance, error) {
|
|||||||
instance.Relay.Info.PubKey = &pubkey
|
instance.Relay.Info.PubKey = &pubkey
|
||||||
}
|
}
|
||||||
|
|
||||||
instance.Relay.UseEventstore(instance.Events, 400)
|
// Handlers
|
||||||
|
|
||||||
instance.Relay.OnConnect = instance.OnConnect
|
instance.Relay.OnConnect = instance.OnConnect
|
||||||
instance.Relay.OnEvent = instance.OnEvent
|
instance.Relay.OnEvent = instance.OnEvent
|
||||||
@@ -85,6 +87,14 @@ func MakeInstance(filename string) (*Instance, error) {
|
|||||||
instance.Relay.RejectConnection = instance.RejectConnection
|
instance.Relay.RejectConnection = instance.RejectConnection
|
||||||
instance.Relay.PreventBroadcast = instance.PreventBroadcast
|
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
|
// Initialize stuff
|
||||||
|
|
||||||
if err := instance.Events.Init(); err != nil {
|
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] {
|
func (instance *Instance) QueryStored(ctx context.Context, filter nostr.Filter) iter.Seq[nostr.Event] {
|
||||||
return func(yield func(nostr.Event) bool) {
|
return func(yield func(nostr.Event) bool) {
|
||||||
if khatru.IsInternalCall(ctx) {
|
if khatru.IsInternalCall(ctx) {
|
||||||
for event := range instance.Events.QueryEvents(filter, 0) {
|
for event := range instance.Events.QueryEvents(filter, 0) {
|
||||||
if !yield(event) {
|
if !yield(event) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
pubkey, isAuthed := khatru.GetAuthed(ctx)
|
pubkey, isAuthed := khatru.GetAuthed(ctx)
|
||||||
|
|
||||||
if !isAuthed {
|
if !isAuthed {
|
||||||
log.Panic("Unauthorized user was allowed to query events")
|
log.Panic("Unauthorized user was allowed to query events")
|
||||||
}
|
}
|
||||||
|
|
||||||
stripSignature := func(event nostr.Event) nostr.Event {
|
stripSignature := func(event nostr.Event) nostr.Event {
|
||||||
if instance.Config.Policy.StripSignatures && !instance.Config.IsAdmin(pubkey) {
|
if instance.Config.Policy.StripSignatures && !instance.Config.IsAdmin(pubkey) {
|
||||||
var zeroSig [64]byte
|
var zeroSig [64]byte
|
||||||
event.Sig = zeroSig
|
event.Sig = zeroSig
|
||||||
}
|
}
|
||||||
|
|
||||||
return event
|
return event
|
||||||
}
|
}
|
||||||
|
|
||||||
if slices.Contains(filter.Kinds, AUTH_INVITE) && instance.Config.CanInvite(pubkey) {
|
if slices.Contains(filter.Kinds, AUTH_INVITE) && instance.Config.CanInvite(pubkey) {
|
||||||
if !yield(stripSignature(instance.GenerateInviteEvent(pubkey))) {
|
if !yield(stripSignature(instance.GenerateInviteEvent(pubkey))) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for event := range instance.Events.QueryEvents(filter, 1000) {
|
for event := range instance.Events.QueryEvents(filter, 1000) {
|
||||||
// We save some ephemeral events for bookkeeping, don't return them
|
// We save some ephemeral events for bookkeeping, don't return them
|
||||||
if event.Kind.IsEphemeral() {
|
if event.Kind.IsEphemeral() {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
h := GetGroupIDFromEvent(event)
|
h := GetGroupIDFromEvent(event)
|
||||||
|
|
||||||
if h != "" {
|
if h != "" {
|
||||||
if !instance.Config.Groups.Enabled {
|
if !instance.Config.Groups.Enabled {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
if !instance.HasGroupAccess(h, pubkey) {
|
if !instance.HasGroupAccess(h, pubkey) {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if !instance.Config.Groups.Enabled && slices.Contains(nip29.MetadataEventKinds, event.Kind) {
|
if !instance.Config.Groups.Enabled && slices.Contains(nip29.MetadataEventKinds, event.Kind) {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
if !yield(event) {
|
if !yield(event) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user