Skip to content

Commit

Permalink
Merge e84ce9c into fadc2a7
Browse files Browse the repository at this point in the history
  • Loading branch information
ninoseki committed May 14, 2022
2 parents fadc2a7 + e84ce9c commit 291a40f
Show file tree
Hide file tree
Showing 6 changed files with 234 additions and 119 deletions.
40 changes: 40 additions & 0 deletions .dockerignore
@@ -0,0 +1,40 @@
# git
.git

# vscode
.vscode

# node
**/node_modules
node_modules

# SQLite
*.db

# Environments
.env
.venv

# python
# Byte-compiled / optimized / DLL files
__pycache__/
**/*.py[cod]
**/*$py.class

# Unit test / coverage reports
htmlcov/
.tox/
.nox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
.hypothesis/
.pytest_cache/

# mypy
.mypy_cache/
.dmypy.json
dmypy.json
22 changes: 22 additions & 0 deletions Dockerfile
@@ -0,0 +1,22 @@
FROM python:3.9-slim-buster

WORKDIR /backend

RUN apt-get update \
&& apt-get install -y whois \
&& apt-get clean \
&& rm -rf /var/lib/apt/lists/*

COPY pyproject.toml poetry.lock README.md /backend/
COPY abuse_whois /backend/abuse_whois
COPY gunicorn.conf.py /backend/

RUN pip install -U pip && \
pip install poetry \
&& poetry install --extras api --no-dev

ENV PORT 8000

EXPOSE $PORT

CMD poetry run gunicorn -k uvicorn.workers.UvicornWorker abuse_whois.api.app:app
10 changes: 9 additions & 1 deletion README.md
Expand Up @@ -86,6 +86,15 @@ $ http localhost:8000/api/whois/ address=https://github.com
}
```

### With Docker

```bash
git clone https://github.com/ninoseki/abuse_whois
cd abuse_whois
docker build . -t abuse-whois
docker run -i -d -p 8000:8000 abuse-whois
```

## Settings

All settings can be done via environment variables or `.env` file.
Expand All @@ -99,7 +108,6 @@ All settings can be done via environment variables or `.env` file.
| IP_ADDRESS_LOOKUP_CACHE_SIZE | int | 1024 | Cache size for IP address lookup |
| IP_ADDRESS_LOOKUP_CACHE_TTL | int | 3600 | Cache TTL value for IP address lookup (seconds) |


## Contributions

`abuse_whois` works based on a combination of static rules and a parsing result of whois response.
Expand Down
43 changes: 43 additions & 0 deletions gunicorn.conf.py
@@ -0,0 +1,43 @@
import multiprocessing
import os

workers_per_core_str = os.getenv("WORKERS_PER_CORE", "1")
max_workers_str = os.getenv("MAX_WORKERS")
use_max_workers = None
if max_workers_str:
use_max_workers = int(max_workers_str)
web_concurrency_str = os.getenv("WEB_CONCURRENCY", None)

host = os.getenv("HOST", "0.0.0.0")
port = os.getenv("PORT", "8000")
use_loglevel = os.getenv("LOG_LEVEL", "info")
use_bind = f"{host}:{port}"

accesslog_var = os.getenv("ACCESS_LOG", "-")
use_accesslog = accesslog_var or None
errorlog_var = os.getenv("ERROR_LOG", "-")
use_errorlog = errorlog_var or None
graceful_timeout_str = os.getenv("GRACEFUL_TIMEOUT", "120")
timeout_str = os.getenv("TIMEOUT", "120")
keepalive_str = os.getenv("KEEP_ALIVE", "5")

cores = multiprocessing.cpu_count()
workers_per_core = float(workers_per_core_str)
default_web_concurrency = workers_per_core * cores
if web_concurrency_str:
web_concurrency = int(web_concurrency_str)
assert web_concurrency > 0
else:
web_concurrency = max(int(default_web_concurrency), 2)
if use_max_workers:
web_concurrency = min(web_concurrency, use_max_workers)

# Gunicorn config variables
loglevel = use_loglevel
workers = web_concurrency
bind = use_bind
errorlog = use_errorlog
accesslog = use_accesslog
graceful_timeout = int(graceful_timeout_str)
timeout = int(timeout_str)
keepalive = int(keepalive_str)

0 comments on commit 291a40f

Please sign in to comment.