diff --git a/handler/handler.go b/handler/handler.go index e682f12..938327d 100644 --- a/handler/handler.go +++ b/handler/handler.go @@ -5,8 +5,8 @@ import ( "net/http" ) -//go:embed static/index.html -var indexPage string +//go:embed static/robots.txt +var robotsTxt []byte type Params struct { MatrixHostname string @@ -27,7 +27,7 @@ func New(params Params) http.Handler { 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 /robots.txt", http.HandlerFunc(h.getRobotsTxt)) h.Handle("GET /", http.FileServer(http.Dir(params.RootPath))) return h @@ -45,10 +45,8 @@ func (h *handler) getMatrixClient(w http.ResponseWriter, r *http.Request) { 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") +func (h *handler) getRobotsTxt(w http.ResponseWriter, r *http.Request) { + w.Header().Add("content-type", "text/plain") w.WriteHeader(http.StatusOK) - if r.Method == http.MethodGet { - w.Write([]byte(indexPage)) - } + w.Write(robotsTxt) } diff --git a/handler/handler_test.go b/handler/handler_test.go index 28aa533..797832c 100644 --- a/handler/handler_test.go +++ b/handler/handler_test.go @@ -41,6 +41,14 @@ func TestHandler(t *testing.T) { wantStatusCode: http.StatusOK, wantBody: `{"m.homeserver": {"base_url": "https://foo.example.com"}}`, }, + { + name: "GET /robots.txt", + method: http.MethodGet, + path: "/robots.txt", + wantContentType: "text/plain", + wantStatusCode: http.StatusOK, + wantBody: "Allow: /", + }, { name: "GET /test.html", method: http.MethodGet, @@ -53,15 +61,15 @@ func TestHandler(t *testing.T) { name: "GET /", method: http.MethodGet, path: "/", - wantContentType: "text/html", + wantContentType: "text/html; charset=utf-8", wantStatusCode: http.StatusOK, - wantBody: "Welcome to netflux.io", + wantBody: "\n
\n