well-goknown/matrix/matrix.go

82 lines
2.1 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"`
}
type matrixClientWellKnown struct {
HomeServer *matrixBaseUrl `json:"m.homeserver"`
IdentityServer *matrixIdentityServer `json:"m.identity_server,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()
2023-02-04 01:15:41 +00:00
// uses an environment variable for now
2023-02-05 01:21:10 +00:00
wellKnownAddr := config.GetConfig().MatrixWellKnownAddress
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
}
2023-02-05 01:21:10 +00:00
identityServer := config.GetConfig().MatrixIdentityServer
if identityServer == "" {
2023-02-04 01:15:41 +00:00
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)
}
}