Skip to content

Commit

Permalink
fix(api) Remove slowapi dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
VVoruganti committed Aug 15, 2024
1 parent 1e40778 commit 6b21e33
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 105 deletions.
4 changes: 2 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# https://pythonspeed.com/articles/base-image-python-docker-images/
# https://testdriven.io/blog/docker-best-practices/
FROM python:3.10-slim-bullseye
FROM python:3.11-slim-bullseye

RUN apt-get update && apt-get install -y build-essential

Expand Down Expand Up @@ -36,5 +36,5 @@ COPY --chown=app:app src/ /app/src/
EXPOSE 8000

# https://stackoverflow.com/questions/29663459/python-app-does-not-print-anything-when-running-detached-in-docker
CMD ["python", "-m", "uvicorn", "src.main:app", "--host", "0.0.0.0", "--port", "8000"]
CMD ["python", "-m", "fastapi", "run", "src/main.py"]

29 changes: 15 additions & 14 deletions fly.toml
Original file line number Diff line number Diff line change
@@ -1,29 +1,30 @@
# fly.toml app configuration file generated for honcho-restless-dew-484 on 2024-01-18T08:20:57-08:00
#
# See https://fly.io/docs/reference/configuration/ for information about how to use this file.
#

app = "honcho"
kill_signal = "SIGINT"
kill_timeout = "5s"
app = 'honcho'
primary_region = 'ewr'
kill_signal = 'SIGINT'
kill_timeout = '5s'

[build]

[processes]
api = "python -m uvicorn src.main:app --host 0.0.0.0 --port 8000"
deriver = "python -m src.deriver"
api = 'python -m fastapi run src/main.py'
deriver = 'python -m src.deriver'

[http_service]
internal_port = 8000
auto_stop_machines = false
auto_stop_machines = 'off'
auto_start_machines = true
min_machines_running = 1
processes = ["api"]
processes = ['api']

[http_service.concurrency]
type = "requests"
type = 'requests'
hard_limit = 250
soft_limit = 200

[[vm]]
cpu_kind = "shared"
memory = '512mb'
cpu_kind = 'shared'
cpus = 1
memory_mb = 512
processes = ["api", "deriver"]
processes = ['api', 'deriver']
66 changes: 1 addition & 65 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ python = "^3.9"
fastapi = "^0.111.0"
python-dotenv = "^1.0.0"
sqlalchemy = "^2.0.30"
slowapi = "^0.1.9"
fastapi-pagination = "^0.12.24"
pgvector = "^0.2.5"
openai = "^1.12.0"
Expand Down
28 changes: 5 additions & 23 deletions src/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,6 @@
ConsoleSpanExporter,
SimpleSpanProcessor,
)
from slowapi import Limiter, _rate_limit_exceeded_handler
from slowapi.errors import RateLimitExceeded
from slowapi.middleware import SlowAPIMiddleware
from slowapi.util import get_remote_address
from starlette.exceptions import HTTPException as StarletteHTTPException

from src.routers import (
Expand Down Expand Up @@ -230,32 +226,18 @@ async def lifespan(app: FastAPI):

router = APIRouter(prefix="/apps/{app_id}/users/{user_id}")

# Create a Limiter instance
limiter = Limiter(key_func=get_remote_address, default_limits=["100/minute"])

# Add SlowAPI middleware to the application
app.state.limiter = limiter
app.add_exception_handler(
exc_class_or_status_code=RateLimitExceeded,
handler=_rate_limit_exceeded_handler, # type: ignore
)
app.add_middleware(SlowAPIMiddleware)


add_pagination(app)


@app.exception_handler(StarletteHTTPException)
async def http_exception_handler(request, exc):
current_span = trace.get_current_span()
if (current_span is not None) and (current_span.is_recording()):
current_span.set_attributes(
{
"http.status_text": str(exc.detail),
"otel.status_description": f"{exc.status_code} / {str(exc.detail)}",
"otel.status_code": "ERROR",
}
)
current_span.set_attributes({
"http.status_text": str(exc.detail),
"otel.status_description": f"{exc.status_code} / {str(exc.detail)}",
"otel.status_code": "ERROR",
})
return PlainTextResponse(
json.dumps({"detail": str(exc.detail)}), status_code=exc.status_code
)
Expand Down

0 comments on commit 6b21e33

Please sign in to comment.