package matrix import ( "encoding/json" "fmt" "net/http" "git.minhas.io/asara/well-goknown/config" "git.minhas.io/asara/well-goknown/logger" ) type matrixServerWellKnown struct { WellKnownAddress string `json:"m.server"` } 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 MatrixServer(w http.ResponseWriter, req *http.Request) { l := logger.Get() // uses an environment variable for now wellKnownAddr := config.GetConfig().MatrixWellKnownAddress if wellKnownAddr == "" { w.WriteHeader(http.StatusNotFound) l.Debug().Str("path", "matrix/server").Msg("matrix well known address unset") return } w.Header().Set("Content-Type", "application/json") w.WriteHeader(http.StatusOK) wellKnown := &matrixServerWellKnown{ WellKnownAddress: wellKnownAddr, } json.NewEncoder(w).Encode(wellKnown) } func MatrixClient(w http.ResponseWriter, req *http.Request) { l := logger.Get() // uses an environment variable for now wellKnownAddr := config.GetConfig().MatrixWellKnownAddress if wellKnownAddr == "" { w.WriteHeader(http.StatusNotFound) l.Debug().Str("path", "matrix/client").Msg("matrix well known address unset") return } identityServer := config.GetConfig().MatrixIdentityServer if identityServer == "" { 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) } }