diff --git a/samples/fastapi-postgres/README.md b/samples/fastapi-postgres/README.md index 7c8744e1..64233842 100644 --- a/samples/fastapi-postgres/README.md +++ b/samples/fastapi-postgres/README.md @@ -9,15 +9,52 @@ This sample project demonstrates how to deploy FastAPI with PostgreSQL with Defa 1. Download Defang CLI 2. (optional) If you are using Defang BYOC, make sure you have properly authenticated your AWS account. -## Deploying +## Development -1. Open the terminal and type `defang login` -2. Type `defang compose up` in the CLI. -3. Your app will be running within a few minutes. +To run the development container(s) locally, do: -## Local Development +```bash +docker compose -f compose.dev.yaml up --build +``` -1. Run `docker compose -f compose.dev.yaml up --build` +Or to run the production container(s) locally, do: + +```bash +POSTGRES_PASSWORD=postgres docker compose up --build +``` + +## Configuration + +For this sample, you will need to provide the following [configuration](https://docs.defang.io/docs/concepts/configuration): + +> Note that if you are using the 1-click deploy option, you can set these values as secrets in your GitHub repository and the action will automatically deploy them for you. + +### `POSTGRES_PASSWORD` +``` bash +defang config set POSTGRES_PASSWORD +``` + +## Deployment + +> [!NOTE] +> Download [Defang CLI](https://github.com/DefangLabs/defang) + +### Defang Playground + +Deploy your application to the Defang Playground by opening up your terminal and typing: +```bash +defang compose up +``` + +### BYOC (AWS) + +If you want to deploy to your own cloud account, you can use Defang BYOC: + +1. [Authenticate your AWS account](https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-configure.html), and check that you have properly set your environment variables like `AWS_PROFILE`, `AWS_REGION`, `AWS_ACCESS_KEY_ID`, and `AWS_SECRET_ACCESS_KEY`. +2. Run in a terminal that has access to your AWS environment variables: + ```bash + defang --provider=aws compose up + ``` --- diff --git a/samples/fastapi-postgres/compose.dev.yaml b/samples/fastapi-postgres/compose.dev.yaml index 5bc6688b..feca2f18 100644 --- a/samples/fastapi-postgres/compose.dev.yaml +++ b/samples/fastapi-postgres/compose.dev.yaml @@ -3,9 +3,6 @@ services: extends: file: compose.yaml service: fastapi - restart: unless-stopped - ports: - - "8000:8000" environment: - DATABASE_URL=postgres://postgres:postgres@db:5432/postgres volumes: @@ -15,8 +12,9 @@ services: command: fastapi dev main.py --host 0.0.0.0 db: - image: postgres:15 - restart: unless-stopped + extends: + file: compose.yaml + service: db environment: - POSTGRES_USER=postgres - POSTGRES_PASSWORD=postgres diff --git a/samples/fastapi-postgres/compose.yaml b/samples/fastapi-postgres/compose.yaml index bbcdc70a..06621caa 100644 --- a/samples/fastapi-postgres/compose.yaml +++ b/samples/fastapi-postgres/compose.yaml @@ -7,9 +7,23 @@ services: ports: - mode: ingress target: 8000 + published: 8000 environment: - - DATABASE_URL + - DB_URL=postgres://postgres:${POSTGRES_PASSWORD}@db:5432/postgres + depends_on: + - db #deploy: # resources: # reservations: # memory: 256M + + db: + image: postgres:15 + restart: unless-stopped + environment: + - POSTGRES_USER=postgres + - POSTGRES_PASSWORD + - POSTGRES_DB=postgres + ports: + - mode: host + target: 5432 diff --git a/samples/fastapi-postgres/fastapi/main.py b/samples/fastapi-postgres/fastapi/main.py index 5dc4da3b..0e0812d3 100644 --- a/samples/fastapi-postgres/fastapi/main.py +++ b/samples/fastapi-postgres/fastapi/main.py @@ -8,9 +8,9 @@ from databases import Database from dotenv import load_dotenv -load_dotenv() +load_dotenv() -database_url = os.getenv("DATABASE_URL") +database_url = os.getenv("DB_URL") print(f"Connecting to database at: {database_url}") database = Database(database_url) diff --git a/samples/fastapi-postgres/fastapi/requirements.txt b/samples/fastapi-postgres/fastapi/requirements.txt index 657f98bf..21f29bb6 100644 --- a/samples/fastapi-postgres/fastapi/requirements.txt +++ b/samples/fastapi-postgres/fastapi/requirements.txt @@ -1,6 +1,8 @@ uwsgi -fastapi +fastapi[standard] uvicorn asyncpg databases python-dotenv +jinja2 +python-multipart \ No newline at end of file diff --git a/samples/fastapi/README.md b/samples/fastapi/README.md index 8e7a19f3..355943b2 100644 --- a/samples/fastapi/README.md +++ b/samples/fastapi/README.md @@ -6,24 +6,49 @@ This sample project demonstrates how to deploy FastAPI with Defang. ## Prerequisites -1. Download Defang CLI -2. (optional) If you are using Defang BYOC, make sure you have properly authenticated your AWS account. - -## Deploying - -1. Open the terminal and type `defang login` -2. Type `defang compose up` in the CLI. -3. Your app will be running within a few minutes. +1. Download [Defang CLI](https://github.com/DefangLabs/defang) +2. (Optional) If you are using [Defang BYOC](https://docs.defang.io/docs/concepts/defang-byoc) authenticate with your cloud provider account +3. (Optional for local development) [Docker CLI](https://docs.docker.com/engine/install/) ## Development -To run your FastAPI app locally, you'll need to have Docker installed on your machine. You can then run: +To run the application locally, you can use the following command: ```bash docker compose -f compose.yaml -f compose.dev.yaml up --build ``` -That will start your FastAPI app on `http://localhost:8000` with hot reloading enabled. +## Configuration + +For this sample, you will not need to provide [configuration](https://docs.defang.io/docs/concepts/configuration). + +If you wish to provide configuration, see below for an example of setting a configuration for a value named `API_KEY`. + +```bash +defang config set API_KEY +``` + +## Deployment + +> [!NOTE] +> Download [Defang CLI](https://github.com/DefangLabs/defang) + +### Defang Playground + +Deploy your application to the Defang Playground by opening up your terminal and typing: +```bash +defang compose up +``` + +### BYOC (AWS) + +If you want to deploy to your own cloud account, you can use Defang BYOC: + +1. [Authenticate your AWS account](https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-configure.html), and check that you have properly set your environment variables like `AWS_PROFILE`, `AWS_REGION`, `AWS_ACCESS_KEY_ID`, and `AWS_SECRET_ACCESS_KEY`. +2. Run in a terminal that has access to your AWS environment variables: + ```bash + defang --provider=aws compose up + ``` ---