well-goknown/matrix/matrix.go

84 lines
2 KiB
Go
Raw Normal View History

package matrix
import (
"encoding/json"
2023-02-04 01:15:41 +00:00
"fmt"
"net/http"
2023-02-05 01:21:10 +00:00
"git.minhas.io/asara/well-goknown/config"
"git.minhas.io/asara/well-goknown/logger"
)
2023-02-04 01:15:41 +00:00
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"`
}
2024-02-12 02:27:05 +00:00
type msc3575address struct {
Url string `json:"url,omitempty"`
}
type matrixClientWellKnown struct {
2024-02-12 02:27:05 +00:00
Homeserver *matrixBaseUrl `json:"m.homeserver"`
IdentityServer *matrixIdentityServer `json:"m.identity_server,omitempty"`
2024-02-12 02:27:05 +00:00
Msc3575 *msc3575address `json:"org.matrix.msc3575.proxy,omitempty"`
}
2023-02-04 01:15:41 +00:00
func MatrixServer(w http.ResponseWriter, req *http.Request) {
l := logger.Get()
// uses an environment variable for now
2023-12-31 00:51:27 +00:00
wellKnownAddr := fmt.Sprintf("%s:8448", config.GetConfig().MatrixWellKnownAddress)
2023-02-05 01:21:10 +00:00
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)
2023-02-04 01:15:41 +00:00
wellKnown := &matrixServerWellKnown{
WellKnownAddress: wellKnownAddr,
}
json.NewEncoder(w).Encode(wellKnown)
}
2023-02-04 01:15:41 +00:00
func MatrixClient(w http.ResponseWriter, req *http.Request) {
l := logger.Get()
2024-02-12 02:27:05 +00:00
m := &matrixClientWellKnown{}
2023-02-05 01:21:10 +00:00
wellKnownAddr := config.GetConfig().MatrixWellKnownAddress
2024-02-12 02:27:05 +00:00
// no matrix config set
2023-02-05 01:21:10 +00:00
if wellKnownAddr == "" {
2023-02-04 01:15:41 +00:00
w.WriteHeader(http.StatusNotFound)
l.Debug().Str("path", "matrix/client").Msg("matrix well known address unset")
2023-02-04 01:15:41 +00:00
return
}
2024-02-12 02:27:05 +00:00
m.Homeserver = &matrixBaseUrl{
BaseUrl: fmt.Sprintf("https://%s", wellKnownAddr),
}
2023-02-05 01:21:10 +00:00
identityServer := config.GetConfig().MatrixIdentityServer
2024-02-12 02:27:05 +00:00
if identityServer != "" {
m.IdentityServer = &matrixIdentityServer{
BaseUrl: identityServer,
2023-02-04 01:15:41 +00:00
}
2024-02-12 02:27:05 +00:00
}
msc3575 := config.GetConfig().MatrixMsc3575Address
if msc3575 != "" {
m.Msc3575 = &msc3575address{
Url: msc3575,
2023-02-04 01:15:41 +00:00
}
}
2024-02-12 02:27:05 +00:00
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(m)
2023-02-04 01:15:41 +00:00
}