我有一些应用程序,在 Python 3.7 上运行,在 ASGI 和 Starlette 上运行。我需要应用程序只接受 HTTP 请求。
我已经通过 docker-compose 打开了端口 443 和 80 并添加了 HTTPSRedirectMiddleware 以确保所有连接都将被重定向到 https
码头工人撰写
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
但是当我尝试通过邮递员在本地主机上运行的容器上发送 POST 请求时,我收到无法连接的错误,并且在应用程序日志中我会得到
[WARNING] Invalid HTTP request received.