diff --git a/docs/docs/en/user-guide/getting-started/index.md b/docs/docs/en/user-guide/getting-started/index.md index 750dff2f..21aa9ffa 100644 --- a/docs/docs/en/user-guide/getting-started/index.md +++ b/docs/docs/en/user-guide/getting-started/index.md @@ -392,15 +392,16 @@ Running the above command will start the Docker container in the foreground with ### Deploying to Fly.io -If you created the project using Cookiecutter, there is a built-in script to deploy your workflow to [**Fly.io**](https://fly.io/). Run it as shown below: +If you created the project using Cookiecutter, there are built-in scripts to deploy your workflow to [**Fly.io**](https://fly.io/). In Fly.io, the application namespace is global, so the application name you chose might already be taken. To check your application's name availability and to reserve it, you can run the following script: ```console -./scripts/deploy_to_fly_io.sh +./scripts/register_to_fly_io.sh ``` Running the above command will prompt you to log in to your Fly.io account (if not already logged in) by opening a fly.io URL in your browser. The login prompt will look like this: ```console +Checking if already logged into fly.io Logging into fly.io failed opening browser. Copy the url (https://fly.io/app/auth/cli/78366a6d347a377a6e346465776167726f6b693537666a333674346978626d37) into a browser and continue Opening https://fly.io/app/auth/cli/78366a6d347a377a6e346465776167726f6b693537666a333674346978626d37 ... @@ -408,32 +409,38 @@ Opening https://fly.io/app/auth/cli/78366a6d347a377a6e346465776167726f6b69353766 Waiting for session... ``` -After logging in, the script will deploy your application to Fly.io without any further input. The output will look like this: +After logging in, the script will check if the application name is available and reserve it if it is. The reserved app domain for Fly.io is saved in a file called "registered_app_domain.txt". The output of running the script will look like this:
Output ```console successfully logged in + Registering app name in fly.io + New app created: test-registration + App name registered successfully + Registered app name is: + test-registration.fly.dev + ``` +
+ +Once you have reserved your application name, you can deploy your application to Fly.io using the following script: + +```console +./scripts/deploy_to_fly_io.sh +``` + +This script will deploy your application to Fly.io without any further input. The output will look like this: + +
+ Output + ```console + Checking if already logged into fly.io + Already logged into fly.io Deploying to fly.io - An existing fly.toml file was found for app write-fastagency-docs-cool-dew-7193 - Using build strategies '[the "docker/Dockerfile" dockerfile]'. Remove [build] from fly.toml to force a rescan - Creating app in /workspaces/write_fastagency_docs - We're about to launch your app on Fly.io. Here's what you're getting: - - Organization: Organization Name (fly launch defaults to the personal org) - Name: write-fastagency-docs-delicate-waterfall-7272 (generated) - Region: Amsterdam, Netherlands (from your fly.toml) - App Machines: shared-cpu-1x, 1GB RAM (from your fly.toml) - Postgres: (not requested) - Redis: (not requested) - Tigris: (not requested) - - Created app 'write-fastagency-docs-delicate-waterfall-7272' in organization 'personal' - Admin URL: https://fly.io/apps/write-fastagency-docs-delicate-waterfall-7272 - Hostname: write-fastagency-docs-delicate-waterfall-7272.fly.dev - Wrote config file fly.toml - Validating /workspaces/write_fastagency_docs/fly.toml + ==> Verifying app config + Validating fly.toml ✓ Configuration is valid + --> Verified app config ==> Building image ==> Building image with Depot --> build: (​) diff --git a/docs/docs_src/getting_started/basic_auth/fastapi/folder_structure.txt b/docs/docs_src/getting_started/basic_auth/fastapi/folder_structure.txt index ed71ba04..c0c1fc5b 100644 --- a/docs/docs_src/getting_started/basic_auth/fastapi/folder_structure.txt +++ b/docs/docs_src/getting_started/basic_auth/fastapi/folder_structure.txt @@ -21,9 +21,12 @@ my_fastagency_app │   └── workflow.py ├── scripts │   ├── build_docker.sh +│   ├── check-registered-app-pre-commit.sh +│   ├── check-registered-app.sh │   ├── deploy_to_fly_io.sh │   ├── lint-pre-commit.sh │   ├── lint.sh +│   ├── register_to_fly_io.sh │   ├── run_docker.sh │   ├── run_mesop_locally.sh │   ├── static-analysis.sh diff --git a/docs/docs_src/getting_started/basic_auth/fastapi/my_fastagency_app/.devcontainer/setup.sh b/docs/docs_src/getting_started/basic_auth/fastapi/my_fastagency_app/.devcontainer/setup.sh index 550f8258..872323dd 100644 --- a/docs/docs_src/getting_started/basic_auth/fastapi/my_fastagency_app/.devcontainer/setup.sh +++ b/docs/docs_src/getting_started/basic_auth/fastapi/my_fastagency_app/.devcontainer/setup.sh @@ -4,6 +4,9 @@ pip install --upgrade pip # install dev packages pip install -e ".[dev]" +# install pre-commit hooks +pre-commit install + # install fly.io CLI and set fly.io CLI PATH in bashrc and zshrc curl -L https://fly.io/install.sh | sh echo 'export FLYCTL_INSTALL="/home/vscode/.fly"' | tee -a ~/.bashrc ~/.zshrc diff --git a/docs/docs_src/getting_started/basic_auth/fastapi/my_fastagency_app/.github/workflows/deploy_to_fly_io.yml b/docs/docs_src/getting_started/basic_auth/fastapi/my_fastagency_app/.github/workflows/deploy_to_fly_io.yml new file mode 100644 index 00000000..8c69f77c --- /dev/null +++ b/docs/docs_src/getting_started/basic_auth/fastapi/my_fastagency_app/.github/workflows/deploy_to_fly_io.yml @@ -0,0 +1,22 @@ + +name: Fly Deploy + +on: + push: + branches: + - main + workflow_dispatch: + +env: + FLY_API_TOKEN: ${{ secrets.FLY_API_TOKEN }} + OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }} + +jobs: + deploy: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - uses: superfly/flyctl-actions/setup-flyctl@master + + - name: Check if the app name is registered in fly.io and deploy + run: ./scripts/deploy_to_fly_io.sh diff --git a/docs/docs_src/getting_started/basic_auth/fastapi/my_fastagency_app/.pre-commit-config.yaml b/docs/docs_src/getting_started/basic_auth/fastapi/my_fastagency_app/.pre-commit-config.yaml index 19b86314..68af06e1 100644 --- a/docs/docs_src/getting_started/basic_auth/fastapi/my_fastagency_app/.pre-commit-config.yaml +++ b/docs/docs_src/getting_started/basic_auth/fastapi/my_fastagency_app/.pre-commit-config.yaml @@ -36,3 +36,12 @@ repos: hooks: - id: detect-secrets args: ["--baseline", ".secrets.baseline"] + + - repo: local + hooks: + - id: check-registered-app + name: Check if the app name is registered in fly.io + entry: "scripts/check-registered-app-pre-commit.sh" + language: python + require_serial: true + verbose: true diff --git a/docs/docs_src/getting_started/basic_auth/fastapi/my_fastagency_app/README.md b/docs/docs_src/getting_started/basic_auth/fastapi/my_fastagency_app/README.md index 7ff5cac0..1960269e 100644 --- a/docs/docs_src/getting_started/basic_auth/fastapi/my_fastagency_app/README.md +++ b/docs/docs_src/getting_started/basic_auth/fastapi/my_fastagency_app/README.md @@ -77,19 +77,19 @@ This `FastAgency` project includes a `fly.toml` file for deployment to [fly.io]( 1. Login into fly.io: ```bash - fly auth login + flyctl auth login ``` 2. Launch the fly.io app: ```bash - fly launch --config fly.toml --copy-config --yes + flyctl launch --config fly.toml --copy-config --yes ``` 3. Set necessary LLM API key(for example, OPENAI_API_KEY) as a secret: ```bash - fly secrets set OPENAI_API_KEY=paste_openai_api_key_here + flyctl secrets set OPENAI_API_KEY=paste_openai_api_key_here ``` ## What's Next? diff --git a/docs/docs_src/getting_started/basic_auth/fastapi/my_fastagency_app/docker/content/nginx.conf.template b/docs/docs_src/getting_started/basic_auth/fastapi/my_fastagency_app/docker/content/nginx.conf.template index c55a5bc4..0ecb7679 100644 --- a/docs/docs_src/getting_started/basic_auth/fastapi/my_fastagency_app/docker/content/nginx.conf.template +++ b/docs/docs_src/getting_started/basic_auth/fastapi/my_fastagency_app/docker/content/nginx.conf.template @@ -5,6 +5,20 @@ upstream mesop_backend { } +# Extract fly-machine-id cookie value +map $http_cookie $fly_machine_id { + "~*fly-machine-id=([^;]+)" $1; + default ""; +} + +# Determine action based on cookie value +map $fly_machine_id $sticky_action { + "" "set_cookie"; # Empty cookie - need to set it + $FLY_MACHINE_ID "proceed"; # Cookie matches current instance + default "replay"; # Cookie exists but doesn't match - need to replay +} + +# Main server block server { listen $MESOP_PORT; server_name localhost; @@ -15,6 +29,17 @@ server { 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://mesop_backend; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; diff --git a/docs/docs_src/getting_started/basic_auth/fastapi/my_fastagency_app/docker/content/run_fastagency.sh b/docs/docs_src/getting_started/basic_auth/fastapi/my_fastagency_app/docker/content/run_fastagency.sh index bbce57e3..77781096 100755 --- a/docs/docs_src/getting_started/basic_auth/fastapi/my_fastagency_app/docker/content/run_fastagency.sh +++ b/docs/docs_src/getting_started/basic_auth/fastapi/my_fastagency_app/docker/content/run_fastagency.sh @@ -11,13 +11,17 @@ export MESOP_PORT=${MESOP_PORT:-8888} WORKERS=${WORKERS:-1} echo "Number of workers: $WORKERS" +# Check FLY_MACHINE_ID is set, if not set, set it to dummy value +export FLY_MACHINE_ID=${FLY_MACHINE_ID:-dummy_fly_machine_id_value} +echo "Fly machine ID: $FLY_MACHINE_ID" + # Generate nginx config for ((i=1; i<$WORKERS+1; i++)) do PORT=$((MESOP_PORT + i)) sed -i "5i\ server 127.0.0.1:$PORT;" nginx.conf.template done -envsubst '${MESOP_PORT}' < nginx.conf.template >/etc/nginx/conf.d/default.conf +envsubst '${MESOP_PORT},${FLY_MACHINE_ID}' < nginx.conf.template >/etc/nginx/conf.d/default.conf echo "Nginx config:" cat /etc/nginx/conf.d/default.conf diff --git a/docs/docs_src/getting_started/basic_auth/fastapi/my_fastagency_app/scripts/check-registered-app-pre-commit.sh b/docs/docs_src/getting_started/basic_auth/fastapi/my_fastagency_app/scripts/check-registered-app-pre-commit.sh new file mode 100755 index 00000000..5f2aef85 --- /dev/null +++ b/docs/docs_src/getting_started/basic_auth/fastapi/my_fastagency_app/scripts/check-registered-app-pre-commit.sh @@ -0,0 +1,13 @@ +#!/usr/bin/env bash + +# taken from: https://jaredkhan.com/blog/mypy-pre-commit + +# A script for running mypy, +# with all its dependencies installed. + +set -o errexit + +# Change directory to the project root directory. +cd "$(dirname "$0")"/.. + +./scripts/check-registered-app.sh diff --git a/docs/docs_src/getting_started/basic_auth/fastapi/my_fastagency_app/scripts/check-registered-app.sh b/docs/docs_src/getting_started/basic_auth/fastapi/my_fastagency_app/scripts/check-registered-app.sh new file mode 100755 index 00000000..62a816ee --- /dev/null +++ b/docs/docs_src/getting_started/basic_auth/fastapi/my_fastagency_app/scripts/check-registered-app.sh @@ -0,0 +1,8 @@ +#!/bin/bash + +# Check file registered_app_domain.txt exists. If it does not exists, echo and exit. +if [ ! -f registered_app_domain.txt ]; then + echo -e "\033[0;33mWarning: App name is not registered.\033[0m" + echo -e "\033[0;33mGithub Actions may fail if you push without registering.\033[0m" + echo -e "\033[0;33mRegister your app name by running the script 'scripts/register_to_fly_io.sh'.\033[0m" +fi diff --git a/docs/docs_src/getting_started/basic_auth/fastapi/my_fastagency_app/scripts/deploy_to_fly_io.sh b/docs/docs_src/getting_started/basic_auth/fastapi/my_fastagency_app/scripts/deploy_to_fly_io.sh index 46e58ff5..42e47b16 100755 --- a/docs/docs_src/getting_started/basic_auth/fastapi/my_fastagency_app/scripts/deploy_to_fly_io.sh +++ b/docs/docs_src/getting_started/basic_auth/fastapi/my_fastagency_app/scripts/deploy_to_fly_io.sh @@ -1,10 +1,23 @@ #!/bin/bash -echo -e "\033[0;32mLogging into fly.io\033[0m" -fly auth login +# Check file registered_app_domain.txt exists. If it does not exists, echo and exit. +if [ ! -f registered_app_domain.txt ]; then + echo -e "\033[0;31mError: App name is not registered.\033[0m" + echo -e "\033[0;31mRegister your app name by running the script 'scripts/register_to_fly_io.sh'.\033[0m" + echo -e "\033[0;31mExiting.\033[0m" + exit 1 +fi + +echo -e "\033[0;32mChecking if already logged into fly.io\033[0m" +if ! flyctl auth whoami > /dev/null 2>&1; then + echo -e "\033[0;32mLogging into fly.io\033[0m" + flyctl auth login +else + echo -e "\033[0;32mAlready logged into fly.io\033[0m" +fi echo -e "\033[0;32mDeploying to fly.io\033[0m" -fly launch --config fly.toml --copy-config --yes +flyctl deploy --config fly.toml --yes echo -e "\033[0;32mSetting secrets\033[0m" -fly secrets set OPENAI_API_KEY=$OPENAI_API_KEY +flyctl secrets set OPENAI_API_KEY=$OPENAI_API_KEY diff --git a/docs/docs_src/getting_started/basic_auth/fastapi/my_fastagency_app/scripts/register_to_fly_io.sh b/docs/docs_src/getting_started/basic_auth/fastapi/my_fastagency_app/scripts/register_to_fly_io.sh new file mode 100755 index 00000000..a49aac06 --- /dev/null +++ b/docs/docs_src/getting_started/basic_auth/fastapi/my_fastagency_app/scripts/register_to_fly_io.sh @@ -0,0 +1,32 @@ +#!/bin/bash + + +# Check file registered_app_domain.txt exists. If it does, echo and exit. +if [ -f registered_app_domain.txt ]; then + echo -e "\033[1;33mWarning: App name is already registered.\033[0m" + echo -e "\033[0;32mRegistered app name is:\033[0m" + cat registered_app_domain.txt + exit 1 +fi + +echo -e "\033[0;32mChecking if already logged into fly.io\033[0m" +if ! flyctl auth whoami > /dev/null 2>&1; then + echo -e "\033[0;32mLogging into fly.io\033[0m" + flyctl auth login +else + echo -e "\033[0;32mAlready logged into fly.io\033[0m" +fi + +export FLY_APP_NAME=my-fastagency-app + +echo -e "\033[0;32mRegistering app name in fly.io\033[0m" +if flyctl apps create $FLY_APP_NAME; then + echo "$FLY_APP_NAME.fly.dev" > registered_app_domain.txt + echo -e "\033[0;32mApp name registered successfully\033[0m" + echo -e "\033[0;32mRegistered app name is:\033[0m" + 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" + exit 1 +fi diff --git a/docs/docs_src/getting_started/basic_auth/mesop/folder_structure.txt b/docs/docs_src/getting_started/basic_auth/mesop/folder_structure.txt index 465bc6b7..46225106 100644 --- a/docs/docs_src/getting_started/basic_auth/mesop/folder_structure.txt +++ b/docs/docs_src/getting_started/basic_auth/mesop/folder_structure.txt @@ -20,9 +20,12 @@ my_fastagency_app │   └── workflow.py ├── scripts │   ├── build_docker.sh +│   ├── check-registered-app-pre-commit.sh +│   ├── check-registered-app.sh │   ├── deploy_to_fly_io.sh │   ├── lint-pre-commit.sh │   ├── lint.sh +│   ├── register_to_fly_io.sh │   ├── run_docker.sh │   ├── run_mesop_locally.sh │   ├── static-analysis.sh diff --git a/docs/docs_src/getting_started/basic_auth/mesop/my_fastagency_app/.devcontainer/setup.sh b/docs/docs_src/getting_started/basic_auth/mesop/my_fastagency_app/.devcontainer/setup.sh index 550f8258..872323dd 100644 --- a/docs/docs_src/getting_started/basic_auth/mesop/my_fastagency_app/.devcontainer/setup.sh +++ b/docs/docs_src/getting_started/basic_auth/mesop/my_fastagency_app/.devcontainer/setup.sh @@ -4,6 +4,9 @@ pip install --upgrade pip # install dev packages pip install -e ".[dev]" +# install pre-commit hooks +pre-commit install + # install fly.io CLI and set fly.io CLI PATH in bashrc and zshrc curl -L https://fly.io/install.sh | sh echo 'export FLYCTL_INSTALL="/home/vscode/.fly"' | tee -a ~/.bashrc ~/.zshrc diff --git a/docs/docs_src/getting_started/basic_auth/mesop/my_fastagency_app/.github/workflows/deploy_to_fly_io.yml b/docs/docs_src/getting_started/basic_auth/mesop/my_fastagency_app/.github/workflows/deploy_to_fly_io.yml new file mode 100644 index 00000000..8c69f77c --- /dev/null +++ b/docs/docs_src/getting_started/basic_auth/mesop/my_fastagency_app/.github/workflows/deploy_to_fly_io.yml @@ -0,0 +1,22 @@ + +name: Fly Deploy + +on: + push: + branches: + - main + workflow_dispatch: + +env: + FLY_API_TOKEN: ${{ secrets.FLY_API_TOKEN }} + OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }} + +jobs: + deploy: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - uses: superfly/flyctl-actions/setup-flyctl@master + + - name: Check if the app name is registered in fly.io and deploy + run: ./scripts/deploy_to_fly_io.sh diff --git a/docs/docs_src/getting_started/basic_auth/mesop/my_fastagency_app/.pre-commit-config.yaml b/docs/docs_src/getting_started/basic_auth/mesop/my_fastagency_app/.pre-commit-config.yaml index 19b86314..68af06e1 100644 --- a/docs/docs_src/getting_started/basic_auth/mesop/my_fastagency_app/.pre-commit-config.yaml +++ b/docs/docs_src/getting_started/basic_auth/mesop/my_fastagency_app/.pre-commit-config.yaml @@ -36,3 +36,12 @@ repos: hooks: - id: detect-secrets args: ["--baseline", ".secrets.baseline"] + + - repo: local + hooks: + - id: check-registered-app + name: Check if the app name is registered in fly.io + entry: "scripts/check-registered-app-pre-commit.sh" + language: python + require_serial: true + verbose: true diff --git a/docs/docs_src/getting_started/basic_auth/mesop/my_fastagency_app/README.md b/docs/docs_src/getting_started/basic_auth/mesop/my_fastagency_app/README.md index 28c1100b..9d45fb79 100644 --- a/docs/docs_src/getting_started/basic_auth/mesop/my_fastagency_app/README.md +++ b/docs/docs_src/getting_started/basic_auth/mesop/my_fastagency_app/README.md @@ -71,19 +71,19 @@ This `FastAgency` project includes a `fly.toml` file for deployment to [fly.io]( 1. Login into fly.io: ```bash - fly auth login + flyctl auth login ``` 2. Launch the fly.io app: ```bash - fly launch --config fly.toml --copy-config --yes + flyctl launch --config fly.toml --copy-config --yes ``` 3. Set necessary LLM API key(for example, OPENAI_API_KEY) as a secret: ```bash - fly secrets set OPENAI_API_KEY=paste_openai_api_key_here + flyctl secrets set OPENAI_API_KEY=paste_openai_api_key_here ``` ## What's Next? diff --git a/docs/docs_src/getting_started/basic_auth/mesop/my_fastagency_app/docker/content/nginx.conf.template b/docs/docs_src/getting_started/basic_auth/mesop/my_fastagency_app/docker/content/nginx.conf.template index c55a5bc4..0ecb7679 100644 --- a/docs/docs_src/getting_started/basic_auth/mesop/my_fastagency_app/docker/content/nginx.conf.template +++ b/docs/docs_src/getting_started/basic_auth/mesop/my_fastagency_app/docker/content/nginx.conf.template @@ -5,6 +5,20 @@ upstream mesop_backend { } +# Extract fly-machine-id cookie value +map $http_cookie $fly_machine_id { + "~*fly-machine-id=([^;]+)" $1; + default ""; +} + +# Determine action based on cookie value +map $fly_machine_id $sticky_action { + "" "set_cookie"; # Empty cookie - need to set it + $FLY_MACHINE_ID "proceed"; # Cookie matches current instance + default "replay"; # Cookie exists but doesn't match - need to replay +} + +# Main server block server { listen $MESOP_PORT; server_name localhost; @@ -15,6 +29,17 @@ server { 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://mesop_backend; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; diff --git a/docs/docs_src/getting_started/basic_auth/mesop/my_fastagency_app/docker/content/run_fastagency.sh b/docs/docs_src/getting_started/basic_auth/mesop/my_fastagency_app/docker/content/run_fastagency.sh index 01761049..5f546fc6 100755 --- a/docs/docs_src/getting_started/basic_auth/mesop/my_fastagency_app/docker/content/run_fastagency.sh +++ b/docs/docs_src/getting_started/basic_auth/mesop/my_fastagency_app/docker/content/run_fastagency.sh @@ -9,13 +9,17 @@ export MESOP_PORT=${MESOP_PORT:-8888} WORKERS=${WORKERS:-1} echo "Number of workers: $WORKERS" +# Check FLY_MACHINE_ID is set, if not set, set it to dummy value +export FLY_MACHINE_ID=${FLY_MACHINE_ID:-dummy_fly_machine_id_value} +echo "Fly machine ID: $FLY_MACHINE_ID" + # Generate nginx config for ((i=1; i<$WORKERS+1; i++)) do PORT=$((MESOP_PORT + i)) sed -i "5i\ server 127.0.0.1:$PORT;" nginx.conf.template done -envsubst '${MESOP_PORT}' < nginx.conf.template >/etc/nginx/conf.d/default.conf +envsubst '${MESOP_PORT},${FLY_MACHINE_ID}' < nginx.conf.template >/etc/nginx/conf.d/default.conf echo "Nginx config:" cat /etc/nginx/conf.d/default.conf diff --git a/docs/docs_src/getting_started/basic_auth/mesop/my_fastagency_app/scripts/check-registered-app-pre-commit.sh b/docs/docs_src/getting_started/basic_auth/mesop/my_fastagency_app/scripts/check-registered-app-pre-commit.sh new file mode 100755 index 00000000..5f2aef85 --- /dev/null +++ b/docs/docs_src/getting_started/basic_auth/mesop/my_fastagency_app/scripts/check-registered-app-pre-commit.sh @@ -0,0 +1,13 @@ +#!/usr/bin/env bash + +# taken from: https://jaredkhan.com/blog/mypy-pre-commit + +# A script for running mypy, +# with all its dependencies installed. + +set -o errexit + +# Change directory to the project root directory. +cd "$(dirname "$0")"/.. + +./scripts/check-registered-app.sh diff --git a/docs/docs_src/getting_started/basic_auth/mesop/my_fastagency_app/scripts/check-registered-app.sh b/docs/docs_src/getting_started/basic_auth/mesop/my_fastagency_app/scripts/check-registered-app.sh new file mode 100755 index 00000000..62a816ee --- /dev/null +++ b/docs/docs_src/getting_started/basic_auth/mesop/my_fastagency_app/scripts/check-registered-app.sh @@ -0,0 +1,8 @@ +#!/bin/bash + +# Check file registered_app_domain.txt exists. If it does not exists, echo and exit. +if [ ! -f registered_app_domain.txt ]; then + echo -e "\033[0;33mWarning: App name is not registered.\033[0m" + echo -e "\033[0;33mGithub Actions may fail if you push without registering.\033[0m" + echo -e "\033[0;33mRegister your app name by running the script 'scripts/register_to_fly_io.sh'.\033[0m" +fi diff --git a/docs/docs_src/getting_started/basic_auth/mesop/my_fastagency_app/scripts/deploy_to_fly_io.sh b/docs/docs_src/getting_started/basic_auth/mesop/my_fastagency_app/scripts/deploy_to_fly_io.sh index 46e58ff5..42e47b16 100755 --- a/docs/docs_src/getting_started/basic_auth/mesop/my_fastagency_app/scripts/deploy_to_fly_io.sh +++ b/docs/docs_src/getting_started/basic_auth/mesop/my_fastagency_app/scripts/deploy_to_fly_io.sh @@ -1,10 +1,23 @@ #!/bin/bash -echo -e "\033[0;32mLogging into fly.io\033[0m" -fly auth login +# Check file registered_app_domain.txt exists. If it does not exists, echo and exit. +if [ ! -f registered_app_domain.txt ]; then + echo -e "\033[0;31mError: App name is not registered.\033[0m" + echo -e "\033[0;31mRegister your app name by running the script 'scripts/register_to_fly_io.sh'.\033[0m" + echo -e "\033[0;31mExiting.\033[0m" + exit 1 +fi + +echo -e "\033[0;32mChecking if already logged into fly.io\033[0m" +if ! flyctl auth whoami > /dev/null 2>&1; then + echo -e "\033[0;32mLogging into fly.io\033[0m" + flyctl auth login +else + echo -e "\033[0;32mAlready logged into fly.io\033[0m" +fi echo -e "\033[0;32mDeploying to fly.io\033[0m" -fly launch --config fly.toml --copy-config --yes +flyctl deploy --config fly.toml --yes echo -e "\033[0;32mSetting secrets\033[0m" -fly secrets set OPENAI_API_KEY=$OPENAI_API_KEY +flyctl secrets set OPENAI_API_KEY=$OPENAI_API_KEY diff --git a/docs/docs_src/getting_started/basic_auth/mesop/my_fastagency_app/scripts/register_to_fly_io.sh b/docs/docs_src/getting_started/basic_auth/mesop/my_fastagency_app/scripts/register_to_fly_io.sh new file mode 100755 index 00000000..a49aac06 --- /dev/null +++ b/docs/docs_src/getting_started/basic_auth/mesop/my_fastagency_app/scripts/register_to_fly_io.sh @@ -0,0 +1,32 @@ +#!/bin/bash + + +# Check file registered_app_domain.txt exists. If it does, echo and exit. +if [ -f registered_app_domain.txt ]; then + echo -e "\033[1;33mWarning: App name is already registered.\033[0m" + echo -e "\033[0;32mRegistered app name is:\033[0m" + cat registered_app_domain.txt + exit 1 +fi + +echo -e "\033[0;32mChecking if already logged into fly.io\033[0m" +if ! flyctl auth whoami > /dev/null 2>&1; then + echo -e "\033[0;32mLogging into fly.io\033[0m" + flyctl auth login +else + echo -e "\033[0;32mAlready logged into fly.io\033[0m" +fi + +export FLY_APP_NAME=my-fastagency-app + +echo -e "\033[0;32mRegistering app name in fly.io\033[0m" +if flyctl apps create $FLY_APP_NAME; then + echo "$FLY_APP_NAME.fly.dev" > registered_app_domain.txt + echo -e "\033[0;32mApp name registered successfully\033[0m" + echo -e "\033[0;32mRegistered app name is:\033[0m" + 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" + exit 1 +fi diff --git a/docs/docs_src/getting_started/basic_auth/nats_n_fastapi/folder_structure.txt b/docs/docs_src/getting_started/basic_auth/nats_n_fastapi/folder_structure.txt index 1ffee012..26395887 100644 --- a/docs/docs_src/getting_started/basic_auth/nats_n_fastapi/folder_structure.txt +++ b/docs/docs_src/getting_started/basic_auth/nats_n_fastapi/folder_structure.txt @@ -22,9 +22,12 @@ my_fastagency_app │   └── workflow.py ├── scripts │   ├── build_docker.sh +│   ├── check-registered-app-pre-commit.sh +│   ├── check-registered-app.sh │   ├── deploy_to_fly_io.sh │   ├── lint-pre-commit.sh │   ├── lint.sh +│   ├── register_to_fly_io.sh │   ├── run_docker.sh │   ├── run_mesop_locally.sh │   ├── static-analysis.sh diff --git a/docs/docs_src/getting_started/basic_auth/nats_n_fastapi/my_fastagency_app/.devcontainer/setup.sh b/docs/docs_src/getting_started/basic_auth/nats_n_fastapi/my_fastagency_app/.devcontainer/setup.sh index 550f8258..872323dd 100644 --- a/docs/docs_src/getting_started/basic_auth/nats_n_fastapi/my_fastagency_app/.devcontainer/setup.sh +++ b/docs/docs_src/getting_started/basic_auth/nats_n_fastapi/my_fastagency_app/.devcontainer/setup.sh @@ -4,6 +4,9 @@ pip install --upgrade pip # install dev packages pip install -e ".[dev]" +# install pre-commit hooks +pre-commit install + # install fly.io CLI and set fly.io CLI PATH in bashrc and zshrc curl -L https://fly.io/install.sh | sh echo 'export FLYCTL_INSTALL="/home/vscode/.fly"' | tee -a ~/.bashrc ~/.zshrc diff --git a/docs/docs_src/getting_started/basic_auth/nats_n_fastapi/my_fastagency_app/.github/workflows/deploy_to_fly_io.yml b/docs/docs_src/getting_started/basic_auth/nats_n_fastapi/my_fastagency_app/.github/workflows/deploy_to_fly_io.yml new file mode 100644 index 00000000..8c69f77c --- /dev/null +++ b/docs/docs_src/getting_started/basic_auth/nats_n_fastapi/my_fastagency_app/.github/workflows/deploy_to_fly_io.yml @@ -0,0 +1,22 @@ + +name: Fly Deploy + +on: + push: + branches: + - main + workflow_dispatch: + +env: + FLY_API_TOKEN: ${{ secrets.FLY_API_TOKEN }} + OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }} + +jobs: + deploy: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - uses: superfly/flyctl-actions/setup-flyctl@master + + - name: Check if the app name is registered in fly.io and deploy + run: ./scripts/deploy_to_fly_io.sh diff --git a/docs/docs_src/getting_started/basic_auth/nats_n_fastapi/my_fastagency_app/.pre-commit-config.yaml b/docs/docs_src/getting_started/basic_auth/nats_n_fastapi/my_fastagency_app/.pre-commit-config.yaml index 19b86314..68af06e1 100644 --- a/docs/docs_src/getting_started/basic_auth/nats_n_fastapi/my_fastagency_app/.pre-commit-config.yaml +++ b/docs/docs_src/getting_started/basic_auth/nats_n_fastapi/my_fastagency_app/.pre-commit-config.yaml @@ -36,3 +36,12 @@ repos: hooks: - id: detect-secrets args: ["--baseline", ".secrets.baseline"] + + - repo: local + hooks: + - id: check-registered-app + name: Check if the app name is registered in fly.io + entry: "scripts/check-registered-app-pre-commit.sh" + language: python + require_serial: true + verbose: true diff --git a/docs/docs_src/getting_started/basic_auth/nats_n_fastapi/my_fastagency_app/README.md b/docs/docs_src/getting_started/basic_auth/nats_n_fastapi/my_fastagency_app/README.md index 174c41a6..d17f7981 100644 --- a/docs/docs_src/getting_started/basic_auth/nats_n_fastapi/my_fastagency_app/README.md +++ b/docs/docs_src/getting_started/basic_auth/nats_n_fastapi/my_fastagency_app/README.md @@ -83,19 +83,19 @@ This `FastAgency` project includes a `fly.toml` file for deployment to [fly.io]( 1. Login into fly.io: ```bash - fly auth login + flyctl auth login ``` 2. Launch the fly.io app: ```bash - fly launch --config fly.toml --copy-config --yes + flyctl launch --config fly.toml --copy-config --yes ``` 3. Set necessary LLM API key(for example, OPENAI_API_KEY) as a secret: ```bash - fly secrets set OPENAI_API_KEY=paste_openai_api_key_here + flyctl secrets set OPENAI_API_KEY=paste_openai_api_key_here ``` ## What's Next? diff --git a/docs/docs_src/getting_started/basic_auth/nats_n_fastapi/my_fastagency_app/docker/content/nginx.conf.template b/docs/docs_src/getting_started/basic_auth/nats_n_fastapi/my_fastagency_app/docker/content/nginx.conf.template index c55a5bc4..0ecb7679 100644 --- a/docs/docs_src/getting_started/basic_auth/nats_n_fastapi/my_fastagency_app/docker/content/nginx.conf.template +++ b/docs/docs_src/getting_started/basic_auth/nats_n_fastapi/my_fastagency_app/docker/content/nginx.conf.template @@ -5,6 +5,20 @@ upstream mesop_backend { } +# Extract fly-machine-id cookie value +map $http_cookie $fly_machine_id { + "~*fly-machine-id=([^;]+)" $1; + default ""; +} + +# Determine action based on cookie value +map $fly_machine_id $sticky_action { + "" "set_cookie"; # Empty cookie - need to set it + $FLY_MACHINE_ID "proceed"; # Cookie matches current instance + default "replay"; # Cookie exists but doesn't match - need to replay +} + +# Main server block server { listen $MESOP_PORT; server_name localhost; @@ -15,6 +29,17 @@ server { 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://mesop_backend; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; diff --git a/docs/docs_src/getting_started/basic_auth/nats_n_fastapi/my_fastagency_app/docker/content/run_fastagency.sh b/docs/docs_src/getting_started/basic_auth/nats_n_fastapi/my_fastagency_app/docker/content/run_fastagency.sh index f1ec4c0e..d1a56eba 100755 --- a/docs/docs_src/getting_started/basic_auth/nats_n_fastapi/my_fastagency_app/docker/content/run_fastagency.sh +++ b/docs/docs_src/getting_started/basic_auth/nats_n_fastapi/my_fastagency_app/docker/content/run_fastagency.sh @@ -13,13 +13,17 @@ export MESOP_PORT=${MESOP_PORT:-8888} WORKERS=${WORKERS:-1} echo "Number of workers: $WORKERS" +# Check FLY_MACHINE_ID is set, if not set, set it to dummy value +export FLY_MACHINE_ID=${FLY_MACHINE_ID:-dummy_fly_machine_id_value} +echo "Fly machine ID: $FLY_MACHINE_ID" + # Generate nginx config for ((i=1; i<$WORKERS+1; i++)) do PORT=$((MESOP_PORT + i)) sed -i "5i\ server 127.0.0.1:$PORT;" nginx.conf.template done -envsubst '${MESOP_PORT}' < nginx.conf.template >/etc/nginx/conf.d/default.conf +envsubst '${MESOP_PORT},${FLY_MACHINE_ID}' < nginx.conf.template >/etc/nginx/conf.d/default.conf echo "Nginx config:" cat /etc/nginx/conf.d/default.conf diff --git a/docs/docs_src/getting_started/basic_auth/nats_n_fastapi/my_fastagency_app/scripts/check-registered-app-pre-commit.sh b/docs/docs_src/getting_started/basic_auth/nats_n_fastapi/my_fastagency_app/scripts/check-registered-app-pre-commit.sh new file mode 100755 index 00000000..5f2aef85 --- /dev/null +++ b/docs/docs_src/getting_started/basic_auth/nats_n_fastapi/my_fastagency_app/scripts/check-registered-app-pre-commit.sh @@ -0,0 +1,13 @@ +#!/usr/bin/env bash + +# taken from: https://jaredkhan.com/blog/mypy-pre-commit + +# A script for running mypy, +# with all its dependencies installed. + +set -o errexit + +# Change directory to the project root directory. +cd "$(dirname "$0")"/.. + +./scripts/check-registered-app.sh diff --git a/docs/docs_src/getting_started/basic_auth/nats_n_fastapi/my_fastagency_app/scripts/check-registered-app.sh b/docs/docs_src/getting_started/basic_auth/nats_n_fastapi/my_fastagency_app/scripts/check-registered-app.sh new file mode 100755 index 00000000..62a816ee --- /dev/null +++ b/docs/docs_src/getting_started/basic_auth/nats_n_fastapi/my_fastagency_app/scripts/check-registered-app.sh @@ -0,0 +1,8 @@ +#!/bin/bash + +# Check file registered_app_domain.txt exists. If it does not exists, echo and exit. +if [ ! -f registered_app_domain.txt ]; then + echo -e "\033[0;33mWarning: App name is not registered.\033[0m" + echo -e "\033[0;33mGithub Actions may fail if you push without registering.\033[0m" + echo -e "\033[0;33mRegister your app name by running the script 'scripts/register_to_fly_io.sh'.\033[0m" +fi diff --git a/docs/docs_src/getting_started/basic_auth/nats_n_fastapi/my_fastagency_app/scripts/deploy_to_fly_io.sh b/docs/docs_src/getting_started/basic_auth/nats_n_fastapi/my_fastagency_app/scripts/deploy_to_fly_io.sh index 46e58ff5..42e47b16 100755 --- a/docs/docs_src/getting_started/basic_auth/nats_n_fastapi/my_fastagency_app/scripts/deploy_to_fly_io.sh +++ b/docs/docs_src/getting_started/basic_auth/nats_n_fastapi/my_fastagency_app/scripts/deploy_to_fly_io.sh @@ -1,10 +1,23 @@ #!/bin/bash -echo -e "\033[0;32mLogging into fly.io\033[0m" -fly auth login +# Check file registered_app_domain.txt exists. If it does not exists, echo and exit. +if [ ! -f registered_app_domain.txt ]; then + echo -e "\033[0;31mError: App name is not registered.\033[0m" + echo -e "\033[0;31mRegister your app name by running the script 'scripts/register_to_fly_io.sh'.\033[0m" + echo -e "\033[0;31mExiting.\033[0m" + exit 1 +fi + +echo -e "\033[0;32mChecking if already logged into fly.io\033[0m" +if ! flyctl auth whoami > /dev/null 2>&1; then + echo -e "\033[0;32mLogging into fly.io\033[0m" + flyctl auth login +else + echo -e "\033[0;32mAlready logged into fly.io\033[0m" +fi echo -e "\033[0;32mDeploying to fly.io\033[0m" -fly launch --config fly.toml --copy-config --yes +flyctl deploy --config fly.toml --yes echo -e "\033[0;32mSetting secrets\033[0m" -fly secrets set OPENAI_API_KEY=$OPENAI_API_KEY +flyctl secrets set OPENAI_API_KEY=$OPENAI_API_KEY diff --git a/docs/docs_src/getting_started/basic_auth/nats_n_fastapi/my_fastagency_app/scripts/register_to_fly_io.sh b/docs/docs_src/getting_started/basic_auth/nats_n_fastapi/my_fastagency_app/scripts/register_to_fly_io.sh new file mode 100755 index 00000000..a49aac06 --- /dev/null +++ b/docs/docs_src/getting_started/basic_auth/nats_n_fastapi/my_fastagency_app/scripts/register_to_fly_io.sh @@ -0,0 +1,32 @@ +#!/bin/bash + + +# Check file registered_app_domain.txt exists. If it does, echo and exit. +if [ -f registered_app_domain.txt ]; then + echo -e "\033[1;33mWarning: App name is already registered.\033[0m" + echo -e "\033[0;32mRegistered app name is:\033[0m" + cat registered_app_domain.txt + exit 1 +fi + +echo -e "\033[0;32mChecking if already logged into fly.io\033[0m" +if ! flyctl auth whoami > /dev/null 2>&1; then + echo -e "\033[0;32mLogging into fly.io\033[0m" + flyctl auth login +else + echo -e "\033[0;32mAlready logged into fly.io\033[0m" +fi + +export FLY_APP_NAME=my-fastagency-app + +echo -e "\033[0;32mRegistering app name in fly.io\033[0m" +if flyctl apps create $FLY_APP_NAME; then + echo "$FLY_APP_NAME.fly.dev" > registered_app_domain.txt + echo -e "\033[0;32mApp name registered successfully\033[0m" + echo -e "\033[0;32mRegistered app name is:\033[0m" + 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" + exit 1 +fi diff --git a/docs/docs_src/getting_started/no_auth/fastapi/folder_structure.txt b/docs/docs_src/getting_started/no_auth/fastapi/folder_structure.txt index ed71ba04..c0c1fc5b 100644 --- a/docs/docs_src/getting_started/no_auth/fastapi/folder_structure.txt +++ b/docs/docs_src/getting_started/no_auth/fastapi/folder_structure.txt @@ -21,9 +21,12 @@ my_fastagency_app │   └── workflow.py ├── scripts │   ├── build_docker.sh +│   ├── check-registered-app-pre-commit.sh +│   ├── check-registered-app.sh │   ├── deploy_to_fly_io.sh │   ├── lint-pre-commit.sh │   ├── lint.sh +│   ├── register_to_fly_io.sh │   ├── run_docker.sh │   ├── run_mesop_locally.sh │   ├── static-analysis.sh diff --git a/docs/docs_src/getting_started/no_auth/fastapi/my_fastagency_app/.devcontainer/setup.sh b/docs/docs_src/getting_started/no_auth/fastapi/my_fastagency_app/.devcontainer/setup.sh index 550f8258..872323dd 100644 --- a/docs/docs_src/getting_started/no_auth/fastapi/my_fastagency_app/.devcontainer/setup.sh +++ b/docs/docs_src/getting_started/no_auth/fastapi/my_fastagency_app/.devcontainer/setup.sh @@ -4,6 +4,9 @@ pip install --upgrade pip # install dev packages pip install -e ".[dev]" +# install pre-commit hooks +pre-commit install + # install fly.io CLI and set fly.io CLI PATH in bashrc and zshrc curl -L https://fly.io/install.sh | sh echo 'export FLYCTL_INSTALL="/home/vscode/.fly"' | tee -a ~/.bashrc ~/.zshrc diff --git a/docs/docs_src/getting_started/no_auth/fastapi/my_fastagency_app/.github/workflows/deploy_to_fly_io.yml b/docs/docs_src/getting_started/no_auth/fastapi/my_fastagency_app/.github/workflows/deploy_to_fly_io.yml new file mode 100644 index 00000000..8c69f77c --- /dev/null +++ b/docs/docs_src/getting_started/no_auth/fastapi/my_fastagency_app/.github/workflows/deploy_to_fly_io.yml @@ -0,0 +1,22 @@ + +name: Fly Deploy + +on: + push: + branches: + - main + workflow_dispatch: + +env: + FLY_API_TOKEN: ${{ secrets.FLY_API_TOKEN }} + OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }} + +jobs: + deploy: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - uses: superfly/flyctl-actions/setup-flyctl@master + + - name: Check if the app name is registered in fly.io and deploy + run: ./scripts/deploy_to_fly_io.sh diff --git a/docs/docs_src/getting_started/no_auth/fastapi/my_fastagency_app/.pre-commit-config.yaml b/docs/docs_src/getting_started/no_auth/fastapi/my_fastagency_app/.pre-commit-config.yaml index 19b86314..68af06e1 100644 --- a/docs/docs_src/getting_started/no_auth/fastapi/my_fastagency_app/.pre-commit-config.yaml +++ b/docs/docs_src/getting_started/no_auth/fastapi/my_fastagency_app/.pre-commit-config.yaml @@ -36,3 +36,12 @@ repos: hooks: - id: detect-secrets args: ["--baseline", ".secrets.baseline"] + + - repo: local + hooks: + - id: check-registered-app + name: Check if the app name is registered in fly.io + entry: "scripts/check-registered-app-pre-commit.sh" + language: python + require_serial: true + verbose: true diff --git a/docs/docs_src/getting_started/no_auth/fastapi/my_fastagency_app/README.md b/docs/docs_src/getting_started/no_auth/fastapi/my_fastagency_app/README.md index 7ff5cac0..1960269e 100644 --- a/docs/docs_src/getting_started/no_auth/fastapi/my_fastagency_app/README.md +++ b/docs/docs_src/getting_started/no_auth/fastapi/my_fastagency_app/README.md @@ -77,19 +77,19 @@ This `FastAgency` project includes a `fly.toml` file for deployment to [fly.io]( 1. Login into fly.io: ```bash - fly auth login + flyctl auth login ``` 2. Launch the fly.io app: ```bash - fly launch --config fly.toml --copy-config --yes + flyctl launch --config fly.toml --copy-config --yes ``` 3. Set necessary LLM API key(for example, OPENAI_API_KEY) as a secret: ```bash - fly secrets set OPENAI_API_KEY=paste_openai_api_key_here + flyctl secrets set OPENAI_API_KEY=paste_openai_api_key_here ``` ## What's Next? diff --git a/docs/docs_src/getting_started/no_auth/fastapi/my_fastagency_app/docker/content/nginx.conf.template b/docs/docs_src/getting_started/no_auth/fastapi/my_fastagency_app/docker/content/nginx.conf.template index c55a5bc4..0ecb7679 100644 --- a/docs/docs_src/getting_started/no_auth/fastapi/my_fastagency_app/docker/content/nginx.conf.template +++ b/docs/docs_src/getting_started/no_auth/fastapi/my_fastagency_app/docker/content/nginx.conf.template @@ -5,6 +5,20 @@ upstream mesop_backend { } +# Extract fly-machine-id cookie value +map $http_cookie $fly_machine_id { + "~*fly-machine-id=([^;]+)" $1; + default ""; +} + +# Determine action based on cookie value +map $fly_machine_id $sticky_action { + "" "set_cookie"; # Empty cookie - need to set it + $FLY_MACHINE_ID "proceed"; # Cookie matches current instance + default "replay"; # Cookie exists but doesn't match - need to replay +} + +# Main server block server { listen $MESOP_PORT; server_name localhost; @@ -15,6 +29,17 @@ server { 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://mesop_backend; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; diff --git a/docs/docs_src/getting_started/no_auth/fastapi/my_fastagency_app/docker/content/run_fastagency.sh b/docs/docs_src/getting_started/no_auth/fastapi/my_fastagency_app/docker/content/run_fastagency.sh index bbce57e3..77781096 100755 --- a/docs/docs_src/getting_started/no_auth/fastapi/my_fastagency_app/docker/content/run_fastagency.sh +++ b/docs/docs_src/getting_started/no_auth/fastapi/my_fastagency_app/docker/content/run_fastagency.sh @@ -11,13 +11,17 @@ export MESOP_PORT=${MESOP_PORT:-8888} WORKERS=${WORKERS:-1} echo "Number of workers: $WORKERS" +# Check FLY_MACHINE_ID is set, if not set, set it to dummy value +export FLY_MACHINE_ID=${FLY_MACHINE_ID:-dummy_fly_machine_id_value} +echo "Fly machine ID: $FLY_MACHINE_ID" + # Generate nginx config for ((i=1; i<$WORKERS+1; i++)) do PORT=$((MESOP_PORT + i)) sed -i "5i\ server 127.0.0.1:$PORT;" nginx.conf.template done -envsubst '${MESOP_PORT}' < nginx.conf.template >/etc/nginx/conf.d/default.conf +envsubst '${MESOP_PORT},${FLY_MACHINE_ID}' < nginx.conf.template >/etc/nginx/conf.d/default.conf echo "Nginx config:" cat /etc/nginx/conf.d/default.conf diff --git a/docs/docs_src/getting_started/no_auth/fastapi/my_fastagency_app/scripts/check-registered-app-pre-commit.sh b/docs/docs_src/getting_started/no_auth/fastapi/my_fastagency_app/scripts/check-registered-app-pre-commit.sh new file mode 100755 index 00000000..5f2aef85 --- /dev/null +++ b/docs/docs_src/getting_started/no_auth/fastapi/my_fastagency_app/scripts/check-registered-app-pre-commit.sh @@ -0,0 +1,13 @@ +#!/usr/bin/env bash + +# taken from: https://jaredkhan.com/blog/mypy-pre-commit + +# A script for running mypy, +# with all its dependencies installed. + +set -o errexit + +# Change directory to the project root directory. +cd "$(dirname "$0")"/.. + +./scripts/check-registered-app.sh diff --git a/docs/docs_src/getting_started/no_auth/fastapi/my_fastagency_app/scripts/check-registered-app.sh b/docs/docs_src/getting_started/no_auth/fastapi/my_fastagency_app/scripts/check-registered-app.sh new file mode 100755 index 00000000..62a816ee --- /dev/null +++ b/docs/docs_src/getting_started/no_auth/fastapi/my_fastagency_app/scripts/check-registered-app.sh @@ -0,0 +1,8 @@ +#!/bin/bash + +# Check file registered_app_domain.txt exists. If it does not exists, echo and exit. +if [ ! -f registered_app_domain.txt ]; then + echo -e "\033[0;33mWarning: App name is not registered.\033[0m" + echo -e "\033[0;33mGithub Actions may fail if you push without registering.\033[0m" + echo -e "\033[0;33mRegister your app name by running the script 'scripts/register_to_fly_io.sh'.\033[0m" +fi diff --git a/docs/docs_src/getting_started/no_auth/fastapi/my_fastagency_app/scripts/deploy_to_fly_io.sh b/docs/docs_src/getting_started/no_auth/fastapi/my_fastagency_app/scripts/deploy_to_fly_io.sh index 46e58ff5..42e47b16 100755 --- a/docs/docs_src/getting_started/no_auth/fastapi/my_fastagency_app/scripts/deploy_to_fly_io.sh +++ b/docs/docs_src/getting_started/no_auth/fastapi/my_fastagency_app/scripts/deploy_to_fly_io.sh @@ -1,10 +1,23 @@ #!/bin/bash -echo -e "\033[0;32mLogging into fly.io\033[0m" -fly auth login +# Check file registered_app_domain.txt exists. If it does not exists, echo and exit. +if [ ! -f registered_app_domain.txt ]; then + echo -e "\033[0;31mError: App name is not registered.\033[0m" + echo -e "\033[0;31mRegister your app name by running the script 'scripts/register_to_fly_io.sh'.\033[0m" + echo -e "\033[0;31mExiting.\033[0m" + exit 1 +fi + +echo -e "\033[0;32mChecking if already logged into fly.io\033[0m" +if ! flyctl auth whoami > /dev/null 2>&1; then + echo -e "\033[0;32mLogging into fly.io\033[0m" + flyctl auth login +else + echo -e "\033[0;32mAlready logged into fly.io\033[0m" +fi echo -e "\033[0;32mDeploying to fly.io\033[0m" -fly launch --config fly.toml --copy-config --yes +flyctl deploy --config fly.toml --yes echo -e "\033[0;32mSetting secrets\033[0m" -fly secrets set OPENAI_API_KEY=$OPENAI_API_KEY +flyctl secrets set OPENAI_API_KEY=$OPENAI_API_KEY diff --git a/docs/docs_src/getting_started/no_auth/fastapi/my_fastagency_app/scripts/register_to_fly_io.sh b/docs/docs_src/getting_started/no_auth/fastapi/my_fastagency_app/scripts/register_to_fly_io.sh new file mode 100755 index 00000000..a49aac06 --- /dev/null +++ b/docs/docs_src/getting_started/no_auth/fastapi/my_fastagency_app/scripts/register_to_fly_io.sh @@ -0,0 +1,32 @@ +#!/bin/bash + + +# Check file registered_app_domain.txt exists. If it does, echo and exit. +if [ -f registered_app_domain.txt ]; then + echo -e "\033[1;33mWarning: App name is already registered.\033[0m" + echo -e "\033[0;32mRegistered app name is:\033[0m" + cat registered_app_domain.txt + exit 1 +fi + +echo -e "\033[0;32mChecking if already logged into fly.io\033[0m" +if ! flyctl auth whoami > /dev/null 2>&1; then + echo -e "\033[0;32mLogging into fly.io\033[0m" + flyctl auth login +else + echo -e "\033[0;32mAlready logged into fly.io\033[0m" +fi + +export FLY_APP_NAME=my-fastagency-app + +echo -e "\033[0;32mRegistering app name in fly.io\033[0m" +if flyctl apps create $FLY_APP_NAME; then + echo "$FLY_APP_NAME.fly.dev" > registered_app_domain.txt + echo -e "\033[0;32mApp name registered successfully\033[0m" + echo -e "\033[0;32mRegistered app name is:\033[0m" + 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" + exit 1 +fi diff --git a/docs/docs_src/getting_started/no_auth/mesop/folder_structure.txt b/docs/docs_src/getting_started/no_auth/mesop/folder_structure.txt index 465bc6b7..46225106 100644 --- a/docs/docs_src/getting_started/no_auth/mesop/folder_structure.txt +++ b/docs/docs_src/getting_started/no_auth/mesop/folder_structure.txt @@ -20,9 +20,12 @@ my_fastagency_app │   └── workflow.py ├── scripts │   ├── build_docker.sh +│   ├── check-registered-app-pre-commit.sh +│   ├── check-registered-app.sh │   ├── deploy_to_fly_io.sh │   ├── lint-pre-commit.sh │   ├── lint.sh +│   ├── register_to_fly_io.sh │   ├── run_docker.sh │   ├── run_mesop_locally.sh │   ├── static-analysis.sh diff --git a/docs/docs_src/getting_started/no_auth/mesop/my_fastagency_app/.devcontainer/setup.sh b/docs/docs_src/getting_started/no_auth/mesop/my_fastagency_app/.devcontainer/setup.sh index 550f8258..872323dd 100644 --- a/docs/docs_src/getting_started/no_auth/mesop/my_fastagency_app/.devcontainer/setup.sh +++ b/docs/docs_src/getting_started/no_auth/mesop/my_fastagency_app/.devcontainer/setup.sh @@ -4,6 +4,9 @@ pip install --upgrade pip # install dev packages pip install -e ".[dev]" +# install pre-commit hooks +pre-commit install + # install fly.io CLI and set fly.io CLI PATH in bashrc and zshrc curl -L https://fly.io/install.sh | sh echo 'export FLYCTL_INSTALL="/home/vscode/.fly"' | tee -a ~/.bashrc ~/.zshrc diff --git a/docs/docs_src/getting_started/no_auth/mesop/my_fastagency_app/.github/workflows/deploy_to_fly_io.yml b/docs/docs_src/getting_started/no_auth/mesop/my_fastagency_app/.github/workflows/deploy_to_fly_io.yml new file mode 100644 index 00000000..8c69f77c --- /dev/null +++ b/docs/docs_src/getting_started/no_auth/mesop/my_fastagency_app/.github/workflows/deploy_to_fly_io.yml @@ -0,0 +1,22 @@ + +name: Fly Deploy + +on: + push: + branches: + - main + workflow_dispatch: + +env: + FLY_API_TOKEN: ${{ secrets.FLY_API_TOKEN }} + OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }} + +jobs: + deploy: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - uses: superfly/flyctl-actions/setup-flyctl@master + + - name: Check if the app name is registered in fly.io and deploy + run: ./scripts/deploy_to_fly_io.sh diff --git a/docs/docs_src/getting_started/no_auth/mesop/my_fastagency_app/.pre-commit-config.yaml b/docs/docs_src/getting_started/no_auth/mesop/my_fastagency_app/.pre-commit-config.yaml index 19b86314..68af06e1 100644 --- a/docs/docs_src/getting_started/no_auth/mesop/my_fastagency_app/.pre-commit-config.yaml +++ b/docs/docs_src/getting_started/no_auth/mesop/my_fastagency_app/.pre-commit-config.yaml @@ -36,3 +36,12 @@ repos: hooks: - id: detect-secrets args: ["--baseline", ".secrets.baseline"] + + - repo: local + hooks: + - id: check-registered-app + name: Check if the app name is registered in fly.io + entry: "scripts/check-registered-app-pre-commit.sh" + language: python + require_serial: true + verbose: true diff --git a/docs/docs_src/getting_started/no_auth/mesop/my_fastagency_app/README.md b/docs/docs_src/getting_started/no_auth/mesop/my_fastagency_app/README.md index 28c1100b..9d45fb79 100644 --- a/docs/docs_src/getting_started/no_auth/mesop/my_fastagency_app/README.md +++ b/docs/docs_src/getting_started/no_auth/mesop/my_fastagency_app/README.md @@ -71,19 +71,19 @@ This `FastAgency` project includes a `fly.toml` file for deployment to [fly.io]( 1. Login into fly.io: ```bash - fly auth login + flyctl auth login ``` 2. Launch the fly.io app: ```bash - fly launch --config fly.toml --copy-config --yes + flyctl launch --config fly.toml --copy-config --yes ``` 3. Set necessary LLM API key(for example, OPENAI_API_KEY) as a secret: ```bash - fly secrets set OPENAI_API_KEY=paste_openai_api_key_here + flyctl secrets set OPENAI_API_KEY=paste_openai_api_key_here ``` ## What's Next? diff --git a/docs/docs_src/getting_started/no_auth/mesop/my_fastagency_app/docker/content/nginx.conf.template b/docs/docs_src/getting_started/no_auth/mesop/my_fastagency_app/docker/content/nginx.conf.template index c55a5bc4..0ecb7679 100644 --- a/docs/docs_src/getting_started/no_auth/mesop/my_fastagency_app/docker/content/nginx.conf.template +++ b/docs/docs_src/getting_started/no_auth/mesop/my_fastagency_app/docker/content/nginx.conf.template @@ -5,6 +5,20 @@ upstream mesop_backend { } +# Extract fly-machine-id cookie value +map $http_cookie $fly_machine_id { + "~*fly-machine-id=([^;]+)" $1; + default ""; +} + +# Determine action based on cookie value +map $fly_machine_id $sticky_action { + "" "set_cookie"; # Empty cookie - need to set it + $FLY_MACHINE_ID "proceed"; # Cookie matches current instance + default "replay"; # Cookie exists but doesn't match - need to replay +} + +# Main server block server { listen $MESOP_PORT; server_name localhost; @@ -15,6 +29,17 @@ server { 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://mesop_backend; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; diff --git a/docs/docs_src/getting_started/no_auth/mesop/my_fastagency_app/docker/content/run_fastagency.sh b/docs/docs_src/getting_started/no_auth/mesop/my_fastagency_app/docker/content/run_fastagency.sh index 01761049..5f546fc6 100755 --- a/docs/docs_src/getting_started/no_auth/mesop/my_fastagency_app/docker/content/run_fastagency.sh +++ b/docs/docs_src/getting_started/no_auth/mesop/my_fastagency_app/docker/content/run_fastagency.sh @@ -9,13 +9,17 @@ export MESOP_PORT=${MESOP_PORT:-8888} WORKERS=${WORKERS:-1} echo "Number of workers: $WORKERS" +# Check FLY_MACHINE_ID is set, if not set, set it to dummy value +export FLY_MACHINE_ID=${FLY_MACHINE_ID:-dummy_fly_machine_id_value} +echo "Fly machine ID: $FLY_MACHINE_ID" + # Generate nginx config for ((i=1; i<$WORKERS+1; i++)) do PORT=$((MESOP_PORT + i)) sed -i "5i\ server 127.0.0.1:$PORT;" nginx.conf.template done -envsubst '${MESOP_PORT}' < nginx.conf.template >/etc/nginx/conf.d/default.conf +envsubst '${MESOP_PORT},${FLY_MACHINE_ID}' < nginx.conf.template >/etc/nginx/conf.d/default.conf echo "Nginx config:" cat /etc/nginx/conf.d/default.conf diff --git a/docs/docs_src/getting_started/no_auth/mesop/my_fastagency_app/scripts/check-registered-app-pre-commit.sh b/docs/docs_src/getting_started/no_auth/mesop/my_fastagency_app/scripts/check-registered-app-pre-commit.sh new file mode 100755 index 00000000..5f2aef85 --- /dev/null +++ b/docs/docs_src/getting_started/no_auth/mesop/my_fastagency_app/scripts/check-registered-app-pre-commit.sh @@ -0,0 +1,13 @@ +#!/usr/bin/env bash + +# taken from: https://jaredkhan.com/blog/mypy-pre-commit + +# A script for running mypy, +# with all its dependencies installed. + +set -o errexit + +# Change directory to the project root directory. +cd "$(dirname "$0")"/.. + +./scripts/check-registered-app.sh diff --git a/docs/docs_src/getting_started/no_auth/mesop/my_fastagency_app/scripts/check-registered-app.sh b/docs/docs_src/getting_started/no_auth/mesop/my_fastagency_app/scripts/check-registered-app.sh new file mode 100755 index 00000000..62a816ee --- /dev/null +++ b/docs/docs_src/getting_started/no_auth/mesop/my_fastagency_app/scripts/check-registered-app.sh @@ -0,0 +1,8 @@ +#!/bin/bash + +# Check file registered_app_domain.txt exists. If it does not exists, echo and exit. +if [ ! -f registered_app_domain.txt ]; then + echo -e "\033[0;33mWarning: App name is not registered.\033[0m" + echo -e "\033[0;33mGithub Actions may fail if you push without registering.\033[0m" + echo -e "\033[0;33mRegister your app name by running the script 'scripts/register_to_fly_io.sh'.\033[0m" +fi diff --git a/docs/docs_src/getting_started/no_auth/mesop/my_fastagency_app/scripts/deploy_to_fly_io.sh b/docs/docs_src/getting_started/no_auth/mesop/my_fastagency_app/scripts/deploy_to_fly_io.sh index 46e58ff5..42e47b16 100755 --- a/docs/docs_src/getting_started/no_auth/mesop/my_fastagency_app/scripts/deploy_to_fly_io.sh +++ b/docs/docs_src/getting_started/no_auth/mesop/my_fastagency_app/scripts/deploy_to_fly_io.sh @@ -1,10 +1,23 @@ #!/bin/bash -echo -e "\033[0;32mLogging into fly.io\033[0m" -fly auth login +# Check file registered_app_domain.txt exists. If it does not exists, echo and exit. +if [ ! -f registered_app_domain.txt ]; then + echo -e "\033[0;31mError: App name is not registered.\033[0m" + echo -e "\033[0;31mRegister your app name by running the script 'scripts/register_to_fly_io.sh'.\033[0m" + echo -e "\033[0;31mExiting.\033[0m" + exit 1 +fi + +echo -e "\033[0;32mChecking if already logged into fly.io\033[0m" +if ! flyctl auth whoami > /dev/null 2>&1; then + echo -e "\033[0;32mLogging into fly.io\033[0m" + flyctl auth login +else + echo -e "\033[0;32mAlready logged into fly.io\033[0m" +fi echo -e "\033[0;32mDeploying to fly.io\033[0m" -fly launch --config fly.toml --copy-config --yes +flyctl deploy --config fly.toml --yes echo -e "\033[0;32mSetting secrets\033[0m" -fly secrets set OPENAI_API_KEY=$OPENAI_API_KEY +flyctl secrets set OPENAI_API_KEY=$OPENAI_API_KEY diff --git a/docs/docs_src/getting_started/no_auth/mesop/my_fastagency_app/scripts/register_to_fly_io.sh b/docs/docs_src/getting_started/no_auth/mesop/my_fastagency_app/scripts/register_to_fly_io.sh new file mode 100755 index 00000000..a49aac06 --- /dev/null +++ b/docs/docs_src/getting_started/no_auth/mesop/my_fastagency_app/scripts/register_to_fly_io.sh @@ -0,0 +1,32 @@ +#!/bin/bash + + +# Check file registered_app_domain.txt exists. If it does, echo and exit. +if [ -f registered_app_domain.txt ]; then + echo -e "\033[1;33mWarning: App name is already registered.\033[0m" + echo -e "\033[0;32mRegistered app name is:\033[0m" + cat registered_app_domain.txt + exit 1 +fi + +echo -e "\033[0;32mChecking if already logged into fly.io\033[0m" +if ! flyctl auth whoami > /dev/null 2>&1; then + echo -e "\033[0;32mLogging into fly.io\033[0m" + flyctl auth login +else + echo -e "\033[0;32mAlready logged into fly.io\033[0m" +fi + +export FLY_APP_NAME=my-fastagency-app + +echo -e "\033[0;32mRegistering app name in fly.io\033[0m" +if flyctl apps create $FLY_APP_NAME; then + echo "$FLY_APP_NAME.fly.dev" > registered_app_domain.txt + echo -e "\033[0;32mApp name registered successfully\033[0m" + echo -e "\033[0;32mRegistered app name is:\033[0m" + 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" + exit 1 +fi diff --git a/docs/docs_src/getting_started/no_auth/nats_n_fastapi/folder_structure.txt b/docs/docs_src/getting_started/no_auth/nats_n_fastapi/folder_structure.txt index 1ffee012..26395887 100644 --- a/docs/docs_src/getting_started/no_auth/nats_n_fastapi/folder_structure.txt +++ b/docs/docs_src/getting_started/no_auth/nats_n_fastapi/folder_structure.txt @@ -22,9 +22,12 @@ my_fastagency_app │   └── workflow.py ├── scripts │   ├── build_docker.sh +│   ├── check-registered-app-pre-commit.sh +│   ├── check-registered-app.sh │   ├── deploy_to_fly_io.sh │   ├── lint-pre-commit.sh │   ├── lint.sh +│   ├── register_to_fly_io.sh │   ├── run_docker.sh │   ├── run_mesop_locally.sh │   ├── static-analysis.sh diff --git a/docs/docs_src/getting_started/no_auth/nats_n_fastapi/my_fastagency_app/.devcontainer/setup.sh b/docs/docs_src/getting_started/no_auth/nats_n_fastapi/my_fastagency_app/.devcontainer/setup.sh index 550f8258..872323dd 100644 --- a/docs/docs_src/getting_started/no_auth/nats_n_fastapi/my_fastagency_app/.devcontainer/setup.sh +++ b/docs/docs_src/getting_started/no_auth/nats_n_fastapi/my_fastagency_app/.devcontainer/setup.sh @@ -4,6 +4,9 @@ pip install --upgrade pip # install dev packages pip install -e ".[dev]" +# install pre-commit hooks +pre-commit install + # install fly.io CLI and set fly.io CLI PATH in bashrc and zshrc curl -L https://fly.io/install.sh | sh echo 'export FLYCTL_INSTALL="/home/vscode/.fly"' | tee -a ~/.bashrc ~/.zshrc diff --git a/docs/docs_src/getting_started/no_auth/nats_n_fastapi/my_fastagency_app/.github/workflows/deploy_to_fly_io.yml b/docs/docs_src/getting_started/no_auth/nats_n_fastapi/my_fastagency_app/.github/workflows/deploy_to_fly_io.yml new file mode 100644 index 00000000..8c69f77c --- /dev/null +++ b/docs/docs_src/getting_started/no_auth/nats_n_fastapi/my_fastagency_app/.github/workflows/deploy_to_fly_io.yml @@ -0,0 +1,22 @@ + +name: Fly Deploy + +on: + push: + branches: + - main + workflow_dispatch: + +env: + FLY_API_TOKEN: ${{ secrets.FLY_API_TOKEN }} + OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }} + +jobs: + deploy: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - uses: superfly/flyctl-actions/setup-flyctl@master + + - name: Check if the app name is registered in fly.io and deploy + run: ./scripts/deploy_to_fly_io.sh diff --git a/docs/docs_src/getting_started/no_auth/nats_n_fastapi/my_fastagency_app/.pre-commit-config.yaml b/docs/docs_src/getting_started/no_auth/nats_n_fastapi/my_fastagency_app/.pre-commit-config.yaml index 19b86314..68af06e1 100644 --- a/docs/docs_src/getting_started/no_auth/nats_n_fastapi/my_fastagency_app/.pre-commit-config.yaml +++ b/docs/docs_src/getting_started/no_auth/nats_n_fastapi/my_fastagency_app/.pre-commit-config.yaml @@ -36,3 +36,12 @@ repos: hooks: - id: detect-secrets args: ["--baseline", ".secrets.baseline"] + + - repo: local + hooks: + - id: check-registered-app + name: Check if the app name is registered in fly.io + entry: "scripts/check-registered-app-pre-commit.sh" + language: python + require_serial: true + verbose: true diff --git a/docs/docs_src/getting_started/no_auth/nats_n_fastapi/my_fastagency_app/README.md b/docs/docs_src/getting_started/no_auth/nats_n_fastapi/my_fastagency_app/README.md index 174c41a6..d17f7981 100644 --- a/docs/docs_src/getting_started/no_auth/nats_n_fastapi/my_fastagency_app/README.md +++ b/docs/docs_src/getting_started/no_auth/nats_n_fastapi/my_fastagency_app/README.md @@ -83,19 +83,19 @@ This `FastAgency` project includes a `fly.toml` file for deployment to [fly.io]( 1. Login into fly.io: ```bash - fly auth login + flyctl auth login ``` 2. Launch the fly.io app: ```bash - fly launch --config fly.toml --copy-config --yes + flyctl launch --config fly.toml --copy-config --yes ``` 3. Set necessary LLM API key(for example, OPENAI_API_KEY) as a secret: ```bash - fly secrets set OPENAI_API_KEY=paste_openai_api_key_here + flyctl secrets set OPENAI_API_KEY=paste_openai_api_key_here ``` ## What's Next? diff --git a/docs/docs_src/getting_started/no_auth/nats_n_fastapi/my_fastagency_app/docker/content/nginx.conf.template b/docs/docs_src/getting_started/no_auth/nats_n_fastapi/my_fastagency_app/docker/content/nginx.conf.template index c55a5bc4..0ecb7679 100644 --- a/docs/docs_src/getting_started/no_auth/nats_n_fastapi/my_fastagency_app/docker/content/nginx.conf.template +++ b/docs/docs_src/getting_started/no_auth/nats_n_fastapi/my_fastagency_app/docker/content/nginx.conf.template @@ -5,6 +5,20 @@ upstream mesop_backend { } +# Extract fly-machine-id cookie value +map $http_cookie $fly_machine_id { + "~*fly-machine-id=([^;]+)" $1; + default ""; +} + +# Determine action based on cookie value +map $fly_machine_id $sticky_action { + "" "set_cookie"; # Empty cookie - need to set it + $FLY_MACHINE_ID "proceed"; # Cookie matches current instance + default "replay"; # Cookie exists but doesn't match - need to replay +} + +# Main server block server { listen $MESOP_PORT; server_name localhost; @@ -15,6 +29,17 @@ server { 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://mesop_backend; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; diff --git a/docs/docs_src/getting_started/no_auth/nats_n_fastapi/my_fastagency_app/docker/content/run_fastagency.sh b/docs/docs_src/getting_started/no_auth/nats_n_fastapi/my_fastagency_app/docker/content/run_fastagency.sh index f1ec4c0e..d1a56eba 100755 --- a/docs/docs_src/getting_started/no_auth/nats_n_fastapi/my_fastagency_app/docker/content/run_fastagency.sh +++ b/docs/docs_src/getting_started/no_auth/nats_n_fastapi/my_fastagency_app/docker/content/run_fastagency.sh @@ -13,13 +13,17 @@ export MESOP_PORT=${MESOP_PORT:-8888} WORKERS=${WORKERS:-1} echo "Number of workers: $WORKERS" +# Check FLY_MACHINE_ID is set, if not set, set it to dummy value +export FLY_MACHINE_ID=${FLY_MACHINE_ID:-dummy_fly_machine_id_value} +echo "Fly machine ID: $FLY_MACHINE_ID" + # Generate nginx config for ((i=1; i<$WORKERS+1; i++)) do PORT=$((MESOP_PORT + i)) sed -i "5i\ server 127.0.0.1:$PORT;" nginx.conf.template done -envsubst '${MESOP_PORT}' < nginx.conf.template >/etc/nginx/conf.d/default.conf +envsubst '${MESOP_PORT},${FLY_MACHINE_ID}' < nginx.conf.template >/etc/nginx/conf.d/default.conf echo "Nginx config:" cat /etc/nginx/conf.d/default.conf diff --git a/docs/docs_src/getting_started/no_auth/nats_n_fastapi/my_fastagency_app/scripts/check-registered-app-pre-commit.sh b/docs/docs_src/getting_started/no_auth/nats_n_fastapi/my_fastagency_app/scripts/check-registered-app-pre-commit.sh new file mode 100755 index 00000000..5f2aef85 --- /dev/null +++ b/docs/docs_src/getting_started/no_auth/nats_n_fastapi/my_fastagency_app/scripts/check-registered-app-pre-commit.sh @@ -0,0 +1,13 @@ +#!/usr/bin/env bash + +# taken from: https://jaredkhan.com/blog/mypy-pre-commit + +# A script for running mypy, +# with all its dependencies installed. + +set -o errexit + +# Change directory to the project root directory. +cd "$(dirname "$0")"/.. + +./scripts/check-registered-app.sh diff --git a/docs/docs_src/getting_started/no_auth/nats_n_fastapi/my_fastagency_app/scripts/check-registered-app.sh b/docs/docs_src/getting_started/no_auth/nats_n_fastapi/my_fastagency_app/scripts/check-registered-app.sh new file mode 100755 index 00000000..62a816ee --- /dev/null +++ b/docs/docs_src/getting_started/no_auth/nats_n_fastapi/my_fastagency_app/scripts/check-registered-app.sh @@ -0,0 +1,8 @@ +#!/bin/bash + +# Check file registered_app_domain.txt exists. If it does not exists, echo and exit. +if [ ! -f registered_app_domain.txt ]; then + echo -e "\033[0;33mWarning: App name is not registered.\033[0m" + echo -e "\033[0;33mGithub Actions may fail if you push without registering.\033[0m" + echo -e "\033[0;33mRegister your app name by running the script 'scripts/register_to_fly_io.sh'.\033[0m" +fi diff --git a/docs/docs_src/getting_started/no_auth/nats_n_fastapi/my_fastagency_app/scripts/deploy_to_fly_io.sh b/docs/docs_src/getting_started/no_auth/nats_n_fastapi/my_fastagency_app/scripts/deploy_to_fly_io.sh index 46e58ff5..42e47b16 100755 --- a/docs/docs_src/getting_started/no_auth/nats_n_fastapi/my_fastagency_app/scripts/deploy_to_fly_io.sh +++ b/docs/docs_src/getting_started/no_auth/nats_n_fastapi/my_fastagency_app/scripts/deploy_to_fly_io.sh @@ -1,10 +1,23 @@ #!/bin/bash -echo -e "\033[0;32mLogging into fly.io\033[0m" -fly auth login +# Check file registered_app_domain.txt exists. If it does not exists, echo and exit. +if [ ! -f registered_app_domain.txt ]; then + echo -e "\033[0;31mError: App name is not registered.\033[0m" + echo -e "\033[0;31mRegister your app name by running the script 'scripts/register_to_fly_io.sh'.\033[0m" + echo -e "\033[0;31mExiting.\033[0m" + exit 1 +fi + +echo -e "\033[0;32mChecking if already logged into fly.io\033[0m" +if ! flyctl auth whoami > /dev/null 2>&1; then + echo -e "\033[0;32mLogging into fly.io\033[0m" + flyctl auth login +else + echo -e "\033[0;32mAlready logged into fly.io\033[0m" +fi echo -e "\033[0;32mDeploying to fly.io\033[0m" -fly launch --config fly.toml --copy-config --yes +flyctl deploy --config fly.toml --yes echo -e "\033[0;32mSetting secrets\033[0m" -fly secrets set OPENAI_API_KEY=$OPENAI_API_KEY +flyctl secrets set OPENAI_API_KEY=$OPENAI_API_KEY diff --git a/docs/docs_src/getting_started/no_auth/nats_n_fastapi/my_fastagency_app/scripts/register_to_fly_io.sh b/docs/docs_src/getting_started/no_auth/nats_n_fastapi/my_fastagency_app/scripts/register_to_fly_io.sh new file mode 100755 index 00000000..a49aac06 --- /dev/null +++ b/docs/docs_src/getting_started/no_auth/nats_n_fastapi/my_fastagency_app/scripts/register_to_fly_io.sh @@ -0,0 +1,32 @@ +#!/bin/bash + + +# Check file registered_app_domain.txt exists. If it does, echo and exit. +if [ -f registered_app_domain.txt ]; then + echo -e "\033[1;33mWarning: App name is already registered.\033[0m" + echo -e "\033[0;32mRegistered app name is:\033[0m" + cat registered_app_domain.txt + exit 1 +fi + +echo -e "\033[0;32mChecking if already logged into fly.io\033[0m" +if ! flyctl auth whoami > /dev/null 2>&1; then + echo -e "\033[0;32mLogging into fly.io\033[0m" + flyctl auth login +else + echo -e "\033[0;32mAlready logged into fly.io\033[0m" +fi + +export FLY_APP_NAME=my-fastagency-app + +echo -e "\033[0;32mRegistering app name in fly.io\033[0m" +if flyctl apps create $FLY_APP_NAME; then + echo "$FLY_APP_NAME.fly.dev" > registered_app_domain.txt + echo -e "\033[0;32mApp name registered successfully\033[0m" + echo -e "\033[0;32mRegistered app name is:\033[0m" + 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" + exit 1 +fi