well-goknown/matrix/matrix.go

76 lines
1.8 KiB
Go

package matrix
import (
"encoding/json"
"fmt"
"net/http"
"os"
)
type matrixServerWellKnown struct {
WellKnownAddress string `json:"m.server"`
}
func MatrixServer(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
}
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, 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)
}
}