Skip to content

Commit 4c0ecf6

Browse files
authored
Merge pull request #485 from DefangLabs/fastapi-postgres-pubsub
FastAPI Postgres PubSub
2 parents a329a37 + 72c65a3 commit 4c0ecf6

File tree

12 files changed

+639
-0
lines changed

12 files changed

+639
-0
lines changed

.github/workflows/deploy-changed-samples.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ jobs:
102102
TEST_SLACK_CHANNEL_ID: ${{ secrets.TEST_SLACK_CHANNEL_ID }}
103103
TEST_SLACK_TOKEN: ${{ secrets.TEST_SLACK_TOKEN }}
104104
TEST_SHARED_SECRETS: ${{ secrets.TEST_SHARED_SECRETS}}
105+
TEST_SSL_MODE: ${{ secrets.TEST_SSL_MODE }}
105106
TEST_TAVILY_API_KEY: ${{ secrets.TEST_TAVILY_API_KEY }}
106107
TEST_ALLOWED_HOSTS: ${{ secrets.TEST_ALLOWED_HOSTS }}
107108
run: |
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
FROM mcr.microsoft.com/devcontainers/python:3.12-bookworm
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"build": {
3+
"dockerfile": "Dockerfile",
4+
"context": ".."
5+
},
6+
"features": {
7+
"ghcr.io/defanglabs/devcontainer-feature/defang-cli:1.0.4": {},
8+
"ghcr.io/devcontainers/features/docker-in-docker:2": {},
9+
"ghcr.io/devcontainers/features/aws-cli:1": {}
10+
}
11+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
name: Deploy
2+
3+
on:
4+
push:
5+
branches:
6+
- main
7+
8+
jobs:
9+
deploy:
10+
environment: playground
11+
runs-on: ubuntu-latest
12+
permissions:
13+
contents: read
14+
id-token: write
15+
16+
steps:
17+
- name: Checkout Repo
18+
uses: actions/checkout@v4
19+
20+
- name: Deploy
21+
uses: DefangLabs/[email protected]
22+
with:
23+
config-env-vars: POSTGRES_PASSWORD SSL_MODE
24+
env:
25+
POSTGRES_PASSWORD: ${{ secrets.POSTGRES_PASSWORD }}
26+
SSL_MODE: ${{ secrets.SSL_MODE }}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
# FastAPI Postgres Pub/Sub Chat
2+
3+
[![1-click-deploy](https://raw.githubusercontent.com/DefangLabs/defang-assets/main/Logos/Buttons/SVG/deploy-with-defang.svg)](https://portal.defang.dev/redirect?url=https%3A%2F%2Fgithub.com%2Fnew%3Ftemplate_name%3Dsample-fastapi-postgres-pubsub-template%26template_owner%3DDefangSamples)
4+
5+
This sample pairs FastAPI with PostgreSQL `LISTEN/NOTIFY` to demonstrate real-time updates between two application containers. A minimal chat UI sends messages with a REST request, and both FastAPI instances broadcast the new message over WebSockets after Postgres notifies them.
6+
7+
## Prerequisites
8+
9+
1. Download [Defang CLI](https://github.com/DefangLabs/defang)
10+
2. (Optional) If you are using [Defang BYOC](https://docs.defang.io/docs/concepts/defang-byoc) authenticate with your cloud provider account
11+
3. (Optional for local development) [Docker CLI](https://docs.docker.com/engine/install/)
12+
13+
## Development
14+
15+
To run the application locally, you can use the following command:
16+
17+
```bash
18+
docker compose -f compose.dev.yaml up --build
19+
```
20+
21+
Once everything is running:
22+
- Visit [http://localhost:8000](http://localhost:8000) for the first FastAPI service.
23+
- Visit [http://localhost:8001](http://localhost:8001) for the second service.
24+
- Send a chat message in either window. Both pages should update immediately, proving Postgres `LISTEN/NOTIFY` fans the event across containers.
25+
26+
Stop the stack with `Ctrl+C`, then run `docker compose -f compose.dev.yaml down`.
27+
28+
## Configuration
29+
30+
For this sample, you can rely on the defaults. Override them with environment variables if needed:
31+
32+
> 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.
33+
34+
### `POSTGRES_PASSWORD`
35+
Database password (default `chat_password`).
36+
```bash
37+
defang config set POSTGRES_PASSWORD --random
38+
```
39+
40+
### `SSL_MODE`
41+
42+
Postgres SSL mode (default `disable`, should set to `require` in production).
43+
```bash
44+
defang config set SSL_MODE=require
45+
```
46+
47+
## Deployment
48+
49+
> [!NOTE]
50+
> Download [Defang CLI](https://github.com/DefangLabs/defang)
51+
52+
### Defang Playground
53+
54+
Deploy your application to the Defang Playground by opening up your terminal and typing:
55+
```bash
56+
defang compose up
57+
```
58+
59+
### BYOC
60+
61+
If you want to deploy to your own cloud account, you can [use Defang BYOC](https://docs.defang.io/docs/tutorials/deploy-to-your-cloud).
62+
63+
---
64+
65+
Title: FastAPI Postgres Pub/Sub
66+
67+
Short Description: FastAPI sample that stores messages in Postgres and streams them to two app instances via LISTEN/NOTIFY.
68+
69+
Tags: FastAPI, PostgreSQL, WebSockets, PubSub
70+
71+
Languages: Python, SQL
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.venv
2+
.env
3+
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
FROM python:3.11-slim
2+
3+
ENV PYTHONUNBUFFERED=1
4+
ENV PYTHONDONTWRITEBYTECODE=1
5+
6+
WORKDIR /app
7+
8+
RUN apt-get update \
9+
&& apt-get install -y --no-install-recommends build-essential libpq-dev curl \
10+
&& rm -rf /var/lib/apt/lists/*
11+
12+
COPY requirements.txt ./
13+
RUN pip install --no-cache-dir --upgrade pip \
14+
&& pip install --no-cache-dir -r requirements.txt
15+
16+
COPY . .
17+
18+
EXPOSE 8000
19+
20+
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8000"]

0 commit comments

Comments
 (0)