Fix up matrix client responses

This commit is contained in:
Amarpreet Minhas 2023-02-03 20:15:41 -05:00
parent 2b7a06a0f4
commit c7257e6885
2 changed files with 53 additions and 4 deletions

View file

@ -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)

View file

@ -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)
}
}