2022-06-06 16:48:53 +00:00
|
|
|
package handler
|
|
|
|
|
|
|
|
import (
|
|
|
|
_ "embed"
|
|
|
|
"net/http"
|
|
|
|
)
|
|
|
|
|
|
|
|
//go:embed static/index.html
|
|
|
|
var indexPage string
|
|
|
|
|
|
|
|
func New(matrixHostname, matrixBaseURL string) http.HandlerFunc {
|
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
2022-06-07 03:04:33 +00:00
|
|
|
if r.Method != http.MethodGet && r.Method != http.MethodHead {
|
2022-06-06 18:23:49 +00:00
|
|
|
w.WriteHeader(http.StatusMethodNotAllowed)
|
|
|
|
w.Write([]byte("405 method not allowed\n"))
|
2022-06-06 16:48:53 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
switch r.URL.Path {
|
|
|
|
case "/.well-known/matrix/server":
|
|
|
|
w.Header().Add("content-type", "application/json")
|
|
|
|
w.WriteHeader(http.StatusOK)
|
|
|
|
w.Write([]byte(`{"m.server": "` + matrixHostname + `"}`))
|
|
|
|
case "/.well-known/matrix/client":
|
|
|
|
w.Header().Add("content-type", "application/json")
|
|
|
|
w.WriteHeader(http.StatusOK)
|
|
|
|
w.Write([]byte(`{"m.homeserver": {"base_url": "` + matrixBaseURL + `"}}`))
|
2022-06-06 18:23:49 +00:00
|
|
|
case "/":
|
2022-06-06 16:48:53 +00:00
|
|
|
w.Header().Add("content-type", "text/html")
|
|
|
|
w.WriteHeader(http.StatusOK)
|
2022-06-07 03:04:33 +00:00
|
|
|
if r.Method == http.MethodGet {
|
|
|
|
w.Write([]byte(indexPage))
|
|
|
|
}
|
2022-06-06 18:23:49 +00:00
|
|
|
default:
|
|
|
|
http.NotFound(w, r)
|
2022-06-06 16:48:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|