diff --git a/gunicorn/gunicorn_conf.py b/gunicorn/gunicorn_conf.py new file mode 100644 index 0000000..f0808bc --- /dev/null +++ b/gunicorn/gunicorn_conf.py @@ -0,0 +1,46 @@ +# based on https://github.com/tiangolo/uvicorn-gunicorn-fastapi-docker + +import multiprocessing +import os + +host = os.getenv("HOST", "0.0.0.0") +port = os.getenv("PORT", "8012") +bind_env = os.getenv("BIND", None) + +use_bind = bind_env if bind_env else f"{host}:{port}" + +workers_per_core_str = os.getenv("WORKERS_PER_CORE", "1") +max_workers_str = os.getenv("MAX_WORKERS") +web_concurrency_str = os.getenv("WEB_CONCURRENCY", None) + +cores = multiprocessing.cpu_count() +workers_per_core = int(workers_per_core_str) +default_web_concurrency = workers_per_core * cores + 1 + +if web_concurrency_str: + web_concurrency = int(web_concurrency_str) + assert web_concurrency > 0 +else: + web_concurrency = max(int(default_web_concurrency), 2) + if max_workers_str: + use_max_workers = int(max_workers_str) + web_concurrency = min(web_concurrency, use_max_workers) + +graceful_timeout_str = os.getenv("GRACEFUL_TIMEOUT", "120") +timeout_str = os.getenv("TIMEOUT", "120") +keepalive_str = os.getenv("KEEP_ALIVE", "5") +use_loglevel = os.getenv("LOG_LEVEL", "info") + +# Gunicorn config variables +# 错误日志级别 +errorlog = "-" +# 访问日志级别 +accesslog = "-" +loglevel = use_loglevel +workers = web_concurrency +bind = use_bind +worker_tmp_dir = "/dev/shm" +graceful_timeout = int(graceful_timeout_str) +timeout = int(timeout_str) +keepalive = int(keepalive_str) +logconfig = os.getenv("LOG_CONFIG", "logging_production.ini") diff --git a/scripts/format b/scripts/format new file mode 100644 index 0000000..1177d97 --- /dev/null +++ b/scripts/format @@ -0,0 +1,5 @@ +#!/bin/sh -e +set -x + +ruff --fix src +ruff format src diff --git a/scripts/start-dev.sh b/scripts/start-dev.sh new file mode 100644 index 0000000..23c564b --- /dev/null +++ b/scripts/start-dev.sh @@ -0,0 +1,18 @@ +#!/usr/bin/env bash + +set -e + +DEFAULT_MODULE_NAME=src.main + +MODULE_NAME=${MODULE_NAME:-$DEFAULT_MODULE_NAME} +VARIABLE_NAME=${VARIABLE_NAME:-app} +export APP_MODULE=${APP_MODULE:-"$MODULE_NAME:$VARIABLE_NAME"} + +HOST=${HOST:-0.0.0.0} +PORT=${PORT:-8000} +LOG_LEVEL=${LOG_LEVEL:-info} +#LOG_CONFIG=${LOG_CONFIG:-/src/logging.ini} +LOG_CONFIG=${LOG_CONFIG:-./logging.ini} + +# Start Uvicorn with live reload +exec uvicorn --reload --proxy-headers --host $HOST --port $PORT --log-config $LOG_CONFIG "$APP_MODULE" \ No newline at end of file diff --git a/scripts/start-prod.sh b/scripts/start-prod.sh new file mode 100644 index 0000000..f2dcce0 --- /dev/null +++ b/scripts/start-prod.sh @@ -0,0 +1,16 @@ +#!/usr/bin/env bash + +set -e + +DEFAULT_MODULE_NAME=src.main + +MODULE_NAME=${MODULE_NAME:-$DEFAULT_MODULE_NAME} +VARIABLE_NAME=${VARIABLE_NAME:-app} +export APP_MODULE=${APP_MODULE:-"$MODULE_NAME:$VARIABLE_NAME"} + +DEFAULT_GUNICORN_CONF=gunicorn/gunicorn_conf.py +export GUNICORN_CONF=${GUNICORN_CONF:-$DEFAULT_GUNICORN_CONF} +export WORKER_CLASS=${WORKER_CLASS:-"uvicorn.workers.UvicornWorker"} + +# Start Gunicorn +gunicorn --forwarded-allow-ips "*" -k "$WORKER_CLASS" -c "$GUNICORN_CONF" "$APP_MODULE"