netflux-homepage/handler/handler.go

39 lines
1009 B
Go

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) {
if r.Method != http.MethodGet && r.Method != http.MethodHead {
w.WriteHeader(http.StatusMethodNotAllowed)
w.Write([]byte("405 method not allowed\n"))
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 + `"}}`))
case "/":
w.Header().Add("content-type", "text/html")
w.WriteHeader(http.StatusOK)
if r.Method == http.MethodGet {
w.Write([]byte(indexPage))
}
default:
http.NotFound(w, r)
}
}
}