Add http serving

This commit is contained in:
Jon Staab
2025-10-01 07:52:19 -07:00
parent 9bda1643a4
commit fcf873690f
2 changed files with 54 additions and 43 deletions
+1
View File
@@ -0,0 +1 @@
hi
+53 -43
View File
@@ -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
}
}
}
}
}