Skip to content

Commit

Permalink
Add robots.txt file
Browse files Browse the repository at this point in the history
fix #172
  • Loading branch information
augusto-herrmann committed Feb 13, 2025
1 parent bd15651 commit cd9ad37
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 3 deletions.
23 changes: 20 additions & 3 deletions src/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from datetime import timedelta
import json
import os
from textwrap import dedent
from typing import Annotated, Awaitable, Callable, Union

from fastapi import Depends, FastAPI, HTTPException, status, Header, Request, Response
Expand Down Expand Up @@ -118,9 +119,7 @@ async def check_user_agent(
async def docs_redirect(
accept: Union[str, None] = Header(default="text/html")
) -> RedirectResponse:
"""
Redireciona para a documentação da API.
"""
"""Redireciona para a documentação da API."""

if accept == "application/json":
location = "/openapi.json"
Expand All @@ -132,6 +131,24 @@ async def docs_redirect(
)


@app.get("/robots.txt", include_in_schema=False)
async def robots_txt() -> Response:
"""Retorna um arquivo robots.txt para orientar crawlers e permitir
indexação somente dos caminhos de documentação.
"""
return Response(
dedent(
"""
User-agent: *
Allow: /docs$
Allow: /redoc$
Disallow: /
"""
).lstrip(),
media_type="text/plain",
)


# ## AUTH --------------------------------------------------


Expand Down
15 changes: 15 additions & 0 deletions tests/endpoints_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,3 +85,18 @@ def test_docs_csp_header(client: Client, path: str):

assert response.status_code == status.HTTP_200_OK
assert response.headers.get("Content-Security-Policy", None) is not None


def test_robots_txt(client: Client):
"""Testa a presença do arquivo robots.txt
Args:
client (Client): fixture do cliente http.
"""
response = client.get("/robots.txt", headers={"Accept": "text/plain"})
content = response.text

assert response.status_code == status.HTTP_200_OK
assert "Allow: /docs$" in content
assert "Allow: /redoc$" in content
assert "Disallow: /\n" in content

0 comments on commit cd9ad37

Please sign in to comment.