relay metadata fix

This commit is contained in:
Chris kerr 2024-07-28 16:25:20 -04:00
parent 81b2eef486
commit b0c2f98dcb
3 changed files with 16 additions and 16 deletions

View File

@ -59,7 +59,6 @@ func main() {
mux := http.NewServeMux()
mux.HandleFunc("/", ListenAndServe)
mux.HandleFunc("/relay-info", web.RelayInfoHandler)
mux.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("web/static"))))
mux.HandleFunc("/favicon.ico", func(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, "web/static/img/favicon.ico")
@ -77,6 +76,8 @@ func ListenAndServe(w http.ResponseWriter, r *http.Request) {
websocket.Handler(func(ws *websocket.Conn) {
relay.WebSocketHandler(ws)
}).ServeHTTP(w, r)
} else if r.Header.Get("Accept") == "application/nostr+json" {
web.RelayInfoHandler(w, r)
} else {
web.RootHandler(w, r)
}

View File

@ -2,7 +2,6 @@ package web
import (
"context"
"encoding/json"
"grain/relay/db"
relay "grain/relay/types"
"html/template"
@ -36,20 +35,6 @@ func RootHandler(w http.ResponseWriter, r *http.Request) {
RenderTemplate(w, data, "index.html")
}
func RelayInfoHandler(w http.ResponseWriter, r *http.Request) {
if r.Header.Get("Accept") != "application/nostr+json" {
http.Error(w, "Unsupported Media Type", http.StatusUnsupportedMediaType)
return
}
w.Header().Set("Content-Type", "application/nostr+json")
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Set("Access-Control-Allow-Headers", "Content-Type")
w.Header().Set("Access-Control-Allow-Methods", "GET")
json.NewEncoder(w).Encode(relayMetadata)
}
// Define the base directories for views and templates
const (
viewsDir = "web/views/"

View File

@ -2,6 +2,7 @@ package web
import (
"encoding/json"
"net/http"
"os"
)
@ -30,3 +31,16 @@ func LoadRelayMetadata(filename string) error {
return nil
}
func RelayInfoHandler(w http.ResponseWriter, r *http.Request) {
if r.Header.Get("Accept") != "application/nostr+json" {
http.Error(w, "Unsupported Media Type", http.StatusUnsupportedMediaType)
return
}
w.Header().Set("Content-Type", "application/nostr+json")
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Set("Access-Control-Allow-Headers", "Content-Type")
w.Header().Set("Access-Control-Allow-Methods", "GET")
json.NewEncoder(w).Encode(relayMetadata)
}