well-goknown/matrix/matrix.go

86 lines
2.2 KiB
Go
Raw Permalink 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
2024-08-15 00:38:38 +00:00
"git.devvul.com/asara/gologger"
"git.devvul.com/asara/well-goknown/config"
)
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 := gologger.Get(config.GetConfig().LogLevel).With().Caller().Logger()
// 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 == "" {
l.Debug().Str("path", "matrix/server").Msg("matrix well known address unset")
2024-08-11 04:03:19 +00:00
http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound)
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 := gologger.Get(config.GetConfig().LogLevel).With().Caller().Logger()
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 == "" {
l.Debug().Str("path", "matrix/client").Msg("matrix well known address unset")
2024-08-11 04:03:19 +00:00
http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound)
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
}
}
w.Header().Set("Content-Type", "application/json")
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
}