package matrix import ( "encoding/json" "fmt" "net/http" "git.minhas.io/asara/well-goknown/config" ) type matrixServerWellKnown struct { WellKnownAddress string `json:"m.server"` } func MatrixServer(w http.ResponseWriter, req *http.Request) { // uses an environment variable for now wellKnownAddr := config.GetConfig().MatrixWellKnownAddress if wellKnownAddr == "" { w.WriteHeader(http.StatusNotFound) return } w.Header().Set("Content-Type", "application/json") w.WriteHeader(http.StatusOK) 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 := config.GetConfig().MatrixWellKnownAddress if wellKnownAddr == "" { w.WriteHeader(http.StatusNotFound) 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) } }