2023-01-25 05:47:06 +00:00
|
|
|
package matrix
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2023-02-04 01:15:41 +00:00
|
|
|
"fmt"
|
2023-01-25 05:47:06 +00:00
|
|
|
"net/http"
|
2023-02-05 01:21:10 +00:00
|
|
|
|
|
|
|
"git.minhas.io/asara/well-goknown/config"
|
2023-04-29 18:01:52 +00:00
|
|
|
"git.minhas.io/asara/well-goknown/logger"
|
2023-01-25 05:47:06 +00:00
|
|
|
)
|
|
|
|
|
2023-02-04 01:15:41 +00:00
|
|
|
type matrixServerWellKnown struct {
|
2023-01-25 05:47:06 +00:00
|
|
|
WellKnownAddress string `json:"m.server"`
|
|
|
|
}
|
|
|
|
|
2023-04-29 18:01:52 +00:00
|
|
|
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) {
|
2023-04-29 18:01:52 +00:00
|
|
|
l := logger.Get()
|
2023-01-25 05:47:06 +00:00
|
|
|
// uses an environment variable for now
|
2023-02-05 01:21:10 +00:00
|
|
|
wellKnownAddr := config.GetConfig().MatrixWellKnownAddress
|
|
|
|
if wellKnownAddr == "" {
|
2023-01-25 05:47:06 +00:00
|
|
|
w.WriteHeader(http.StatusNotFound)
|
2023-04-29 18:01:52 +00:00
|
|
|
l.Debug().Str("path", "matrix/server").Msg("matrix well known address unset")
|
2023-01-25 05:47:06 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
w.WriteHeader(http.StatusOK)
|
2023-02-04 01:15:41 +00:00
|
|
|
wellKnown := &matrixServerWellKnown{
|
2023-01-25 05:47:06 +00:00
|
|
|
WellKnownAddress: wellKnownAddr,
|
|
|
|
}
|
|
|
|
json.NewEncoder(w).Encode(wellKnown)
|
|
|
|
}
|
2023-02-04 01:15:41 +00:00
|
|
|
|
|
|
|
func MatrixClient(w http.ResponseWriter, req *http.Request) {
|
2023-04-29 18:01:52 +00:00
|
|
|
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)
|
2023-04-29 18:01:52 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|