1

I have some app, writhed on Python 3.7, that runs on ASGI and Starlette. I need app to accept only HTTP requests.

I've opened ports 443 and 80 via docker-compose and add HTTPSRedirectMiddleware to ensure that all connections will be redirected to https

docker-compose

version: '3.4'
services:
  api:
    image: api
    stop_grace_period: 30s
    environment:
      API_VERSION: $API_VERSION
    deploy:
      mode: replicated
      replicas: 1
      restart_policy:
        condition: on-failure
      update_config:
        delay: 1s
        monitor: 10s
        order: stop-first
        parallelism: 1
        failure_action: rollback
    ports:
      - 80:80
443:443
networks:
    default:
        driver: overlay
Dockerfile
ARG PYTHON_VERSION=3.7.3
FROM python:${PYTHON_VERSION} as builder
ENV PYTHONBUFFERED 1
WORKDIR /wheels
COPY ["requirements.txt","/wheels/requirements/"]
RUN pip install -U pip \
    && pip wheel -r ./requirements/requirements.txt
FROM python:${PYTHON_VERSION} as release
COPY --from=builder /wheels /wheels
ARG API_PORT

ENV PYTHONPATH=/app \
GUWORKERS=0.5\
WORKERS_PER_CORE=1\
GUWORKERS_CONNECTIONS=50

COPY ["./", "/"]

RUN pip install -r /wheels/requirements/requirements.txt\
    -f /wheels\
    && rm -rf /wheels \
    && rm -rf /root/.cache/pip/* \
    && chmod +x /scripts/entrypoint.sh
WORKDIR /app/

ENTRYPOINT ["/scripts/entrypoint.sh"]

Python

def create_app():
    app = Starlette()
    app.add_exception_handler(HTTPException, error_handler)
    app.add_middleware(ServerErrorMiddleware, handler=server_error_handler)
    app.add_middleware(HTTPSRedirectMiddleware)
    app.add_route("/", calculate, methods=["POST"])
    return app

But when i try to send a POST request via postman on container running on localhost i get error that i can't connect and in logs of app i'll get

[WARNING] Invalid HTTP request received.
Stasello Boldirev
  • 123
  • 1
  • 2
  • 7

1 Answers1

0

You need to specify the SSL certificate and private key for HTTPS to work.

$ gunicorn --keyfile=./key.pem --certfile=./cert.pem -k uvicorn.workers.UvicornWorker example:app

You can find more here: https://www.uvicorn.org/deployment/#running-with-https

Kuba Beránek
  • 458
  • 9
  • 19