Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update cookiecutter generated files #604

Merged
merged 1 commit into from
Nov 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
upstream nats_fastapi_backend {
# Enable sticky sessions with IP hash
ip_hash;


}

upstream fastapi_backend {
# Enable sticky sessions with IP hash
ip_hash;


}

upstream mesop_backend {
# Enable sticky sessions with IP hash
ip_hash;
Expand All @@ -18,9 +32,47 @@ map $fly_machine_id $sticky_action {
default "replay"; # Cookie exists but doesn't match - need to replay
}

# Main server block
# Fastapi server block
server {
listen $FASTAPI_PORT;
server_name localhost;

# Security headers
add_header X-Frame-Options "SAMEORIGIN";
add_header X-Content-Type-Options "nosniff";
add_header X-XSS-Protection "1; mode=block";

location / {
# Handle cookie setting
if ($sticky_action = "set_cookie") {
add_header Set-Cookie "fly-machine-id=$FLY_MACHINE_ID; Max-Age=518400; Path=/";
}

# Handle replay
if ($sticky_action = "replay") {
add_header Fly-Replay "instance=$fly_machine_id";
return 307;
}

proxy_pass http://fastapi_backend;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_redirect off;
proxy_buffering off;

# WSGI support
proxy_set_header X-Forwarded-Host $server_name;

# WebSocket support
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}
}
# Mesop server block
server {
listen $SERVICE_PORT;
listen $MESOP_PORT;
server_name localhost;

# Security headers
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
#!/bin/bash

# Accept env variable for PORT


FASTAPI_PORT=${FASTAPI_PORT:-8008}


export NATS_FASTAPI_PORT=${NATS_FASTAPI_PORT:-8000}
export FASTAPI_PORT=${FASTAPI_PORT:-8008}
export MESOP_PORT=${MESOP_PORT:-8888}
export SERVICE_PORT=$MESOP_PORT

# Default number of workers if not set
WORKERS=${WORKERS:-1}
Expand All @@ -20,10 +16,23 @@ echo "Fly machine ID: $FLY_MACHINE_ID"
# Generate nginx config
for ((i=1; i<$WORKERS+1; i++))
do
PORT=$((SERVICE_PORT + i))
PORT=$((MESOP_PORT + i))
sed -i "19i\ server 127.0.0.1:$PORT;" nginx.conf.template
done

for ((i=1; i<$WORKERS+1; i++))
do
PORT=$((FASTAPI_PORT + i))
sed -i "12i\ server 127.0.0.1:$PORT;" nginx.conf.template
done

for ((i=1; i<$WORKERS+1; i++))
do
PORT=$((NATS_FASTAPI_PORT + i))
sed -i "5i\ server 127.0.0.1:$PORT;" nginx.conf.template
done
envsubst '${SERVICE_PORT},${FLY_MACHINE_ID}' < nginx.conf.template >/etc/nginx/conf.d/default.conf

envsubst '${NATS_FASTAPI_PORT},${FASTAPI_PORT},${MESOP_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 @@ -32,14 +41,20 @@ nginx -g "daemon off;" &


# Run uvicorn server
uvicorn my_fastagency_app.deployment.main_1_fastapi:app --host 0.0.0.0 --port $FASTAPI_PORT > /dev/stdout 2>&1 &
# Start multiple single-worker uvicorn instances on consecutive ports
for ((i=1; i<$WORKERS+1; i++))
do
PORT=$((FASTAPI_PORT + i))
echo "Starting fastapi uvicorn on port $PORT"
uvicorn my_fastagency_app.deployment.main_1_fastapi:app --workers=1 --host 0.0.0.0 --port $PORT > /dev/stdout 2>&1 &
done


# Run gunicorn server
# Start multiple single-worker gunicorn instances on consecutive ports
for ((i=1; i<$WORKERS+1; i++))
do
PORT=$((SERVICE_PORT + i))
PORT=$((MESOP_PORT + i))
echo "Starting gunicorn on port $PORT"
gunicorn --workers=1 my_fastagency_app.deployment.main_2_mesop:app --bind 0.0.0.0:$PORT > /dev/stdout 2>&1 &
done
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,30 @@ primary_region = 'ams'
memory = '1gb'
cpu_kind = 'shared'
cpus = 1

[[services]]
http_checks = []
internal_port = 8008
processes = ["app"]
protocol = "tcp"
script_checks = []

[services.concurrency]
type = "connections"

[[services.ports]]
handlers = ["tls", "http"]
port = 8008
[[services]]
http_checks = []
internal_port = 8888
processes = ["app"]
protocol = "tcp"
script_checks = []

[services.concurrency]
type = "connections"

[[services.ports]]
handlers = ["tls", "http"]
port = 8888
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ else
echo -e "\033[0;32mAlready logged into fly.io\033[0m"
fi

export FLY_APP_NAME=my-fastagency-app
export FLY_APP_NAME=$(grep "^app = " fly.toml | awk -F"'" '{print $2}')

echo -e "\033[0;32mRegistering app name in fly.io\033[0m"
if flyctl apps create $FLY_APP_NAME; then
Expand All @@ -27,6 +27,6 @@ if flyctl apps create $FLY_APP_NAME; then
cat registered_app_domain.txt
else
echo -e "\033[1;31mError: App name is not available.\033[0m"
echo -e "\033[1;31mPlease change the app name in fly.toml and scripts/register_to_fly_io.sh and run this script again.\033[0m"
echo -e "\033[1;31mPlease change the app name in fly.toml and run this script again.\033[0m"
exit 1
fi
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
upstream nats_fastapi_backend {
# Enable sticky sessions with IP hash
ip_hash;


}

upstream fastapi_backend {
# Enable sticky sessions with IP hash
ip_hash;


}

upstream mesop_backend {
# Enable sticky sessions with IP hash
ip_hash;
Expand All @@ -18,9 +32,9 @@ map $fly_machine_id $sticky_action {
default "replay"; # Cookie exists but doesn't match - need to replay
}

# Main server block
# Mesop server block
server {
listen $SERVICE_PORT;
listen $MESOP_PORT;
server_name localhost;

# Security headers
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
#!/bin/bash

# Accept env variable for PORT



export NATS_FASTAPI_PORT=${NATS_FASTAPI_PORT:-8000}
export FASTAPI_PORT=${FASTAPI_PORT:-8008}
export MESOP_PORT=${MESOP_PORT:-8888}
export SERVICE_PORT=$MESOP_PORT

# Default number of workers if not set
WORKERS=${WORKERS:-1}
Expand All @@ -18,26 +16,36 @@ echo "Fly machine ID: $FLY_MACHINE_ID"
# Generate nginx config
for ((i=1; i<$WORKERS+1; i++))
do
PORT=$((SERVICE_PORT + i))
PORT=$((MESOP_PORT + i))
sed -i "19i\ server 127.0.0.1:$PORT;" nginx.conf.template
done

for ((i=1; i<$WORKERS+1; i++))
do
PORT=$((FASTAPI_PORT + i))
sed -i "12i\ server 127.0.0.1:$PORT;" nginx.conf.template
done

for ((i=1; i<$WORKERS+1; i++))
do
PORT=$((NATS_FASTAPI_PORT + i))
sed -i "5i\ server 127.0.0.1:$PORT;" nginx.conf.template
done
envsubst '${SERVICE_PORT},${FLY_MACHINE_ID}' < nginx.conf.template >/etc/nginx/conf.d/default.conf

envsubst '${NATS_FASTAPI_PORT},${FASTAPI_PORT},${MESOP_PORT},${FLY_MACHINE_ID}' < nginx.conf.template >/etc/nginx/conf.d/default.conf
echo "Nginx config:"
cat /etc/nginx/conf.d/default.conf

# Start nginx
nginx -g "daemon off;" &


# Run uvicorn server
uvicorn my_fastagency_app.deployment.main_:app --host 0.0.0.0 --port $FASTAPI_PORT > /dev/stdout 2>&1 &


# Run gunicorn server
# Start multiple single-worker gunicorn instances on consecutive ports
for ((i=1; i<$WORKERS+1; i++))
do
PORT=$((SERVICE_PORT + i))
PORT=$((MESOP_PORT + i))
echo "Starting gunicorn on port $PORT"
gunicorn --workers=1 my_fastagency_app.deployment.main:app --bind 0.0.0.0:$PORT > /dev/stdout 2>&1 &
done
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,17 @@ primary_region = 'ams'
memory = '1gb'
cpu_kind = 'shared'
cpus = 1

[[services]]
http_checks = []
internal_port = 8888
processes = ["app"]
protocol = "tcp"
script_checks = []

[services.concurrency]
type = "connections"

[[services.ports]]
handlers = ["tls", "http"]
port = 8888
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ else
echo -e "\033[0;32mAlready logged into fly.io\033[0m"
fi

export FLY_APP_NAME=my-fastagency-app
export FLY_APP_NAME=$(grep "^app = " fly.toml | awk -F"'" '{print $2}')

echo -e "\033[0;32mRegistering app name in fly.io\033[0m"
if flyctl apps create $FLY_APP_NAME; then
Expand All @@ -27,6 +27,6 @@ if flyctl apps create $FLY_APP_NAME; then
cat registered_app_domain.txt
else
echo -e "\033[1;31mError: App name is not available.\033[0m"
echo -e "\033[1;31mPlease change the app name in fly.toml and scripts/register_to_fly_io.sh and run this script again.\033[0m"
echo -e "\033[1;31mPlease change the app name in fly.toml and run this script again.\033[0m"
exit 1
fi
Loading