55 lines
1.3 KiB
Go
55 lines
1.3 KiB
Go
package handler
|
|
|
|
import (
|
|
_ "embed"
|
|
"net/http"
|
|
)
|
|
|
|
//go:embed static/index.html
|
|
var indexPage string
|
|
|
|
type Params struct {
|
|
MatrixHostname string
|
|
MatrixBaseURL string
|
|
RootPath string
|
|
}
|
|
|
|
type handler struct {
|
|
*http.ServeMux
|
|
params Params
|
|
}
|
|
|
|
func New(params Params) http.Handler {
|
|
h := &handler{
|
|
ServeMux: http.NewServeMux(),
|
|
params: params,
|
|
}
|
|
|
|
h.Handle("GET /.well-known/matrix/server", http.HandlerFunc(h.getMatrixServer))
|
|
h.Handle("GET /.well-known/matrix/client", http.HandlerFunc(h.getMatrixClient))
|
|
h.Handle("GET /{$}", http.HandlerFunc(h.getHomepage))
|
|
h.Handle("GET /", http.FileServer(http.Dir(params.RootPath)))
|
|
|
|
return h
|
|
}
|
|
|
|
func (h *handler) getMatrixServer(w http.ResponseWriter, r *http.Request) {
|
|
w.Header().Add("content-type", "application/json")
|
|
w.WriteHeader(http.StatusOK)
|
|
w.Write([]byte(`{"m.server": "` + h.params.MatrixHostname + `"}`))
|
|
}
|
|
|
|
func (h *handler) getMatrixClient(w http.ResponseWriter, r *http.Request) {
|
|
w.Header().Add("content-type", "application/json")
|
|
w.WriteHeader(http.StatusOK)
|
|
w.Write([]byte(`{"m.homeserver": {"base_url": "` + h.params.MatrixBaseURL + `"}}`))
|
|
}
|
|
|
|
func (h *handler) getHomepage(w http.ResponseWriter, r *http.Request) {
|
|
w.Header().Add("content-type", "text/html")
|
|
w.WriteHeader(http.StatusOK)
|
|
if r.Method == http.MethodGet {
|
|
w.Write([]byte(indexPage))
|
|
}
|
|
}
|