grain/server/nip/11RelayMetadata.go

52 lines
1.2 KiB
Go
Raw Normal View History

2024-07-31 18:41:48 +00:00
package nip
2024-07-27 20:46:25 +00:00
import (
"encoding/json"
2024-07-28 20:25:20 +00:00
"net/http"
2024-07-27 20:46:25 +00:00
"os"
)
type RelayMetadata struct {
Name string `json:"name"`
Description string `json:"description"`
Pubkey string `json:"pubkey"`
Contact string `json:"contact"`
SupportedNIPs []int `json:"supported_nips"`
Software string `json:"software"`
Version string `json:"version"`
}
var relayMetadata RelayMetadata
2024-07-30 15:51:05 +00:00
func LoadRelayMetadataJSON() error {
2024-07-31 18:41:48 +00:00
return LoadRelayMetadata("relay_metadata.json")
2024-07-30 15:51:05 +00:00
}
2024-07-27 20:46:25 +00:00
func LoadRelayMetadata(filename string) error {
data, err := os.ReadFile(filename)
if err != nil {
return err
}
err = json.Unmarshal(data, &relayMetadata)
if err != nil {
return err
}
return nil
}
2024-07-30 15:51:05 +00:00
2024-07-28 20:25:20 +00:00
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)
}