Skip to content

Commit

Permalink
WIP: Update nginx and docker files
Browse files Browse the repository at this point in the history
  • Loading branch information
kumaranvpl committed Nov 21, 2024
1 parent d370bae commit 6ac0887
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ map $fly_machine_id $sticky_action {

# Main server block
server {
listen $MESOP_PORT;
listen $TO_EXPOSE_SERVICE_PORT;
server_name localhost;

# Security headers
Expand Down
23 changes: 19 additions & 4 deletions {{cookiecutter.project_slug}}/docker/content/run_fastagency.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,12 @@ NATS_FASTAPI_PORT=${NATS_FASTAPI_PORT:-8000}
{% if "fastapi" in cookiecutter.app_type %}
FASTAPI_PORT=${FASTAPI_PORT:-8008}
{% endif %}
{% if "mesop" in cookiecutter.app_type %}
export MESOP_PORT=${MESOP_PORT:-8888}

export TO_EXPOSE_SERVICE_PORT=$MESOP_PORT
{% else %}
export TO_EXPOSE_SERVICE_PORT=$FASTAPI_PORT
{% endif %}
# Default number of workers if not set
WORKERS=${WORKERS:-1}
echo "Number of workers: $WORKERS"
Expand All @@ -20,10 +24,10 @@ echo "Fly machine ID: $FLY_MACHINE_ID"
# Generate nginx config
for ((i=1; i<$WORKERS+1; i++))
do
PORT=$((MESOP_PORT + i))
PORT=$((TO_EXPOSE_SERVICE_PORT + i))
sed -i "5i\ server 127.0.0.1:$PORT;" nginx.conf.template
done
envsubst '${MESOP_PORT},${FLY_MACHINE_ID}' < nginx.conf.template >/etc/nginx/conf.d/default.conf
envsubst '${TO_EXPOSE_SERVICE_PORT},${FLY_MACHINE_ID}' < nginx.conf.template >/etc/nginx/conf.d/default.conf
echo "Nginx config:"
cat /etc/nginx/conf.d/default.conf

Expand All @@ -33,14 +37,25 @@ nginx -g "daemon off;" &
# Run nats uvicorn server
uvicorn {{cookiecutter.project_slug}}.deployment.main_1_nats:app --host 0.0.0.0 --port $NATS_FASTAPI_PORT > /dev/stdout 2>&1 &
{% endif %}
{% if cookiecutter.app_type == "fastapi" %}
# Run uvicorn server
# Start multiple single-worker uvicorn instances on consecutive ports
for ((i=1; i<$WORKERS+1; i++))
do
PORT=$((TO_EXPOSE_SERVICE_PORT + i))
echo "Starting gunicorn on port $PORT"
uvicorn {{cookiecutter.project_slug}}.deployment.main_1_fastapi:app --workers=1 --host 0.0.0.0 --port $PORT > /dev/stdout 2>&1 &
done
{% else %}
# Run uvicorn server
uvicorn {{cookiecutter.project_slug}}.deployment.main_{% if "nats" in cookiecutter.app_type %}2_fastapi{% elif "fastapi" in cookiecutter.app_type %}1_fastapi{% endif %}:app --host 0.0.0.0 --port $FASTAPI_PORT > /dev/stdout 2>&1 &
{% endif %}
{% if "mesop" in cookiecutter.app_type %}
# Run gunicorn server
# Start multiple single-worker gunicorn instances on consecutive ports
for ((i=1; i<$WORKERS+1; i++))
do
PORT=$((MESOP_PORT + i))
PORT=$((TO_EXPOSE_SERVICE_PORT + i))
echo "Starting gunicorn on port $PORT"
gunicorn --workers=1 {{cookiecutter.project_slug}}.deployment.main{% if "nats" in cookiecutter.app_type %}_3_mesop{% elif "fastapi" in cookiecutter.app_type %}_2_mesop{% endif %}:app --bind 0.0.0.0:$PORT > /dev/stdout 2>&1 &
done
Expand Down
2 changes: 1 addition & 1 deletion {{cookiecutter.project_slug}}/scripts/run_docker.sh
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
#!/bin/bash

docker run -it -e OPENAI_API_KEY=$OPENAI_API_KEY {% if cookiecutter.authentication == "google"%}-e GOOGLE_APPLICATION_CREDENTIALS="serviceAccountKey.json"{% endif %} {% if "nats" in cookiecutter.app_type %}-e NATS_URL=$NATS_URL -e FASTAGENCY_NATS_PASSWORD=$FASTAGENCY_NATS_PASSWORD -p 8000:8000{% endif %}{% if "fastapi" in cookiecutter.app_type %} -p 8008:8008{% endif %} -p 8888:8888 {% if "nats" in cookiecutter.app_type %}--network=host{% endif %} deploy_fastagency
docker run -it -e OPENAI_API_KEY=$OPENAI_API_KEY {% if cookiecutter.authentication == "google"%}-e GOOGLE_APPLICATION_CREDENTIALS="serviceAccountKey.json"{% endif %} {% if "nats" in cookiecutter.app_type %}-e NATS_URL=$NATS_URL -e FASTAGENCY_NATS_PASSWORD=$FASTAGENCY_NATS_PASSWORD -p 8000:8000{% endif %}{% if "fastapi" in cookiecutter.app_type %} -p 8008:8008{% endif %}{% if "mesop" in cookiecutter.app_type %} -p 8888:8888{% endif %}{% if "nats" in cookiecutter.app_type %} --network=host{% endif %} deploy_fastagency

0 comments on commit 6ac0887

Please sign in to comment.