From c7257e68851aefc5e08e79557c9ab15aad060b94 Mon Sep 17 00:00:00 2001 From: Asara Date: Fri, 3 Feb 2023 20:15:41 -0500 Subject: [PATCH] Fix up matrix client responses --- main.go | 3 ++- matrix/matrix.go | 54 +++++++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 53 insertions(+), 4 deletions(-) diff --git a/main.go b/main.go index 17da593..2fb310f 100644 --- a/main.go +++ b/main.go @@ -17,7 +17,8 @@ func main() { nostr.AddNostrAddr(nostrTest) */ // matrix endpoints - http.HandleFunc("/.well-known/matrix/server", matrix.Matrix) + http.HandleFunc("/.well-known/matrix/server", matrix.MatrixServer) + http.HandleFunc("/.well-known/matrix/client", matrix.MatrixClient) // nostr endpoints http.HandleFunc("/.well-known/nostr.json", nostr.GetNostrAddr) diff --git a/matrix/matrix.go b/matrix/matrix.go index 9d842ee..ff028a3 100644 --- a/matrix/matrix.go +++ b/matrix/matrix.go @@ -2,15 +2,16 @@ package matrix import ( "encoding/json" + "fmt" "net/http" "os" ) -type matrixWellKnown struct { +type matrixServerWellKnown struct { WellKnownAddress string `json:"m.server"` } -func Matrix(w http.ResponseWriter, req *http.Request) { +func MatrixServer(w http.ResponseWriter, req *http.Request) { // uses an environment variable for now wellKnownAddr, ok := os.LookupEnv("MATRIX_WELL_KNOWN_ADDRESS") if !ok { @@ -20,8 +21,55 @@ func Matrix(w http.ResponseWriter, req *http.Request) { w.Header().Set("Content-Type", "application/json") w.WriteHeader(http.StatusOK) - wellKnown := &matrixWellKnown{ + wellKnown := &matrixServerWellKnown{ WellKnownAddress: wellKnownAddr, } json.NewEncoder(w).Encode(wellKnown) } + +type matrixBaseUrl struct { + BaseUrl string `json:"base_url"` +} + +type matrixIdentityServer struct { + BaseUrl string `json:"base_url,omitempty"` +} + +type matrixClientWellKnown struct { + HomeServer *matrixBaseUrl `json:"m.homeserver"` + IdentityServer *matrixIdentityServer `json:"m.identity_server,omitempty"` +} + +func MatrixClient(w http.ResponseWriter, req *http.Request) { + // uses an environment variable for now + wellKnownAddr, ok := os.LookupEnv("MATRIX_WELL_KNOWN_ADDRESS") + if !ok { + w.WriteHeader(http.StatusNotFound) + return + } + + identityServer, ok := os.LookupEnv("MATRIX_IDENTITY_SERVER") + if !ok { + w.Header().Set("Content-Type", "application/json") + w.WriteHeader(http.StatusOK) + baseUrl := &matrixClientWellKnown{ + HomeServer: &matrixBaseUrl{ + BaseUrl: fmt.Sprintf("https://%s", wellKnownAddr), + }, + } + json.NewEncoder(w).Encode(baseUrl) + } else { + identityServer = fmt.Sprintf("https://%s", identityServer) + w.Header().Set("Content-Type", "application/json") + w.WriteHeader(http.StatusOK) + baseUrl := &matrixClientWellKnown{ + HomeServer: &matrixBaseUrl{ + BaseUrl: fmt.Sprintf("https://%s", wellKnownAddr), + }, + IdentityServer: &matrixIdentityServer{ + BaseUrl: identityServer, + }, + } + json.NewEncoder(w).Encode(baseUrl) + } +}