feat(backend): add /robots.txt endpoint

This commit is contained in:
Łukasz Mierzwa
2021-02-24 21:17:54 +00:00
committed by Łukasz Mierzwa
parent 927a996f54
commit a3b13d0fcc
4 changed files with 23 additions and 0 deletions
+6
View File
@@ -1,5 +1,11 @@
# Changelog
## [unreleased]
### Added
- Add `/robots.txt` to block search engine crawlers.
## v0.79
### Fixed
+1
View File
@@ -128,6 +128,7 @@ func setupRouter(router *chi.Mux) {
router.Get(getViewURL("/"), index)
router.Get(getViewURL("/health"), pong)
router.Get(getViewURL("/robots.txt"), robots)
router.Get(getViewURL("/metrics"), http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
h := promhttp.Handler()
h.ServeHTTP(w, r)
+4
View File
@@ -41,6 +41,10 @@ func pong(w http.ResponseWriter, r *http.Request) {
_, _ = w.Write([]byte("Pong\n"))
}
func robots(w http.ResponseWriter, r *http.Request) {
_, _ = w.Write([]byte("User-agent: *\nDisallow: /\n"))
}
func compressResponse(data []byte) ([]byte, error) {
var b bytes.Buffer
// this only fails if we pass unsupported level (3 is valid)
+12
View File
@@ -75,6 +75,18 @@ func TestHealth(t *testing.T) {
}
}
func TestRobots(t *testing.T) {
mockConfig()
r := testRouter()
setupRouter(r)
req := httptest.NewRequest("GET", "/robots.txt", nil)
resp := httptest.NewRecorder()
r.ServeHTTP(resp, req)
if resp.Code != http.StatusOK {
t.Errorf("GET /robots.txt returned status %d", resp.Code)
}
}
func TestHealthPrefix(t *testing.T) {
os.Setenv("LISTEN_PREFIX", "/prefix")
defer os.Unsetenv("LISTEN_PREFIX")