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

View File

@ -2,7 +2,6 @@ package web
import ( import (
"context" "context"
"encoding/json"
"grain/relay/db" "grain/relay/db"
relay "grain/relay/types" relay "grain/relay/types"
"html/template" "html/template"
@ -36,20 +35,6 @@ func RootHandler(w http.ResponseWriter, r *http.Request) {
RenderTemplate(w, data, "index.html") 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 // Define the base directories for views and templates
const ( const (
viewsDir = "web/views/" viewsDir = "web/views/"

View File

@ -2,6 +2,7 @@ package web
import ( import (
"encoding/json" "encoding/json"
"net/http"
"os" "os"
) )
@ -30,3 +31,16 @@ func LoadRelayMetadata(filename string) error {
return nil 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)
}