first commit
This commit is contained in:
41
internal/http/handlers/nostrjson.go
Normal file
41
internal/http/handlers/nostrjson.go
Normal file
@@ -0,0 +1,41 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/noderunners/nip05api/internal/user"
|
||||
)
|
||||
|
||||
type NostrJSON struct {
|
||||
Users *user.Service
|
||||
Relays []string
|
||||
}
|
||||
|
||||
func (h *NostrJSON) Handle(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Cache-Control", "public, max-age=60")
|
||||
|
||||
names, err := h.Users.Repo().ActiveByName(r.Context())
|
||||
if err != nil {
|
||||
WriteError(w, http.StatusInternalServerError, "InternalError", err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
if q := r.URL.Query().Get("name"); q != "" {
|
||||
filtered := map[string]string{}
|
||||
if pk, ok := names[q]; ok {
|
||||
filtered[q] = pk
|
||||
}
|
||||
names = filtered
|
||||
}
|
||||
|
||||
relays := map[string][]string{}
|
||||
if len(h.Relays) > 0 {
|
||||
for _, pk := range names {
|
||||
relays[pk] = h.Relays
|
||||
}
|
||||
}
|
||||
WriteJSON(w, http.StatusOK, map[string]any{
|
||||
"names": names,
|
||||
"relays": relays,
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user