Skip to content

Commit 75208ad

Browse files
authored
Merge pull request #14 from slashformotion/configuration
Config
2 parents 8680a28 + 66ebfd3 commit 75208ad

File tree

4 files changed

+24
-8
lines changed

4 files changed

+24
-8
lines changed

Dockerfile

+5-1
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,14 @@ FROM debian:bookworm-slim
2929
COPY --from=builder --chown=python:python /python /python
3030

3131
# Copy the application from the builder
32-
COPY --from=builder --chown=app:app /app /app
32+
COPY --from=builder --chown=app:app /app/src /app/src
33+
COPY --from=builder --chown=app:app /app/.venv /app/.venv
3334

3435
# Place executables in the environment at the front of the path
3536
ENV PATH="/app/.venv/bin:$PATH"
3637
WORKDIR /app
3738
# Run the FastAPI application by default
3839
CMD ["uvicorn", "--host", "0.0.0.0", "src:app"]
40+
41+
LABEL ogr.opencontainer.image.url="https://github.com/slashformotion/typst-http-api/pkgs/container/typst-http-api"
42+
LABEL org.opencontainers.image.licenses="MIT"

README.md

+8
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,14 @@ services:
5151
- "8000:8000"
5252
```
5353
54+
## Configuration
55+
56+
### Rate limiting
57+
58+
You can enable IP based rate limiting with the following environement variable : `TYPST_HTTP_API_REQUESTS_PER_MINUTES`. Must be an uint32 > 0, it will define the number of requests per minute.
59+
60+
If not defined, rate limiting is disabled.
61+
5462
## Build the docker image locally
5563

5664
Build the docker image

result.pdf

-11.8 KB
Binary file not shown.

src/__init__.py

+11-7
Original file line numberDiff line numberDiff line change
@@ -9,27 +9,33 @@
99
from slowapi import Limiter, _rate_limit_exceeded_handler
1010
from slowapi.errors import RateLimitExceeded
1111
from slowapi.util import get_remote_address
12+
import os
1213

1314
DEFAULT_CHUNK_SIZE = 1024
15+
REQUEST_PER_MINUTES = os.getenv("TYPST_HTTP_API_REQUESTS_PER_MINUTES")
16+
17+
18+
logger = logging.getLogger(__name__)
1419

15-
limiter = Limiter(key_func=get_remote_address)
1620
app = FastAPI(docs_url=None, redoc_url=None)
17-
app.state.limiter = limiter
21+
22+
limiter = Limiter(key_func=get_remote_address)
23+
if REQUEST_PER_MINUTES is not None:
24+
app.state.limiter = limiter
25+
1826
app.add_exception_handler(RateLimitExceeded, _rate_limit_exceeded_handler)
1927

2028

2129
Instrumentator().instrument(app).expose(app)
2230

23-
logger = logging.getLogger(__name__)
24-
2531

2632
class CompilationError(BaseModel):
2733
reason: str
2834
content: str
2935

3036

3137
@app.post("/")
32-
@limiter.limit("6/minute")
38+
@limiter.limit(f"{REQUEST_PER_MINUTES}/minute")
3339
async def build(request: Request, response: Response):
3440
typst_bytes = await request.body()
3541

@@ -49,5 +55,3 @@ def iterfile(
4955
yield input_bytes[i : i + chunk_size]
5056

5157
return StreamingResponse(iterfile(res), media_type="application/pdf")
52-
53-

0 commit comments

Comments
 (0)