well-goknown/matrix/matrix.go
2024-02-11 22:02:24 -05:00

84 lines
2 KiB
Go

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 msc3575address struct {
Url string `json:"url,omitempty"`
}
type matrixClientWellKnown struct {
Homeserver *matrixBaseUrl `json:"m.homeserver"`
IdentityServer *matrixIdentityServer `json:"m.identity_server,omitempty"`
Msc3575 *msc3575address `json:"org.matrix.msc3575.proxy,omitempty"`
}
func MatrixServer(w http.ResponseWriter, req *http.Request) {
l := logger.Get()
// uses an environment variable for now
wellKnownAddr := fmt.Sprintf("%s:8448", 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()
m := &matrixClientWellKnown{}
wellKnownAddr := config.GetConfig().MatrixWellKnownAddress
// no matrix config set
if wellKnownAddr == "" {
w.WriteHeader(http.StatusNotFound)
l.Debug().Str("path", "matrix/client").Msg("matrix well known address unset")
return
}
m.Homeserver = &matrixBaseUrl{
BaseUrl: fmt.Sprintf("https://%s", wellKnownAddr),
}
identityServer := config.GetConfig().MatrixIdentityServer
if identityServer != "" {
m.IdentityServer = &matrixIdentityServer{
BaseUrl: identityServer,
}
}
msc3575 := config.GetConfig().MatrixMsc3575Address
if msc3575 != "" {
m.Msc3575 = &msc3575address{
Url: msc3575,
}
}
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(m)
}