Skip to content

Commit 3b9c55c

Browse files
committed
add sample hello endpoint and configuration
1 parent 404ca06 commit 3b9c55c

File tree

12 files changed

+624
-0
lines changed

12 files changed

+624
-0
lines changed

.env

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
DB_URL=
2+
DB_USER=
3+
DB_PASSWORD=
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
name: Deploy to AWS ECR
2+
3+
on:
4+
push:
5+
branches:
6+
- main
7+
8+
env:
9+
AWS_REGION: "eu-central-1"
10+
11+
jobs:
12+
deploy:
13+
runs-on: ubuntu-latest
14+
15+
permissions:
16+
contents: read
17+
id-token: write
18+
19+
steps:
20+
# Checkout the code
21+
- name: Checkout Code
22+
uses: actions/checkout@v3
23+
24+
# Set up AWS credentials
25+
- name: Configure AWS credentials
26+
uses: aws-actions/configure-aws-credentials@v4
27+
with:
28+
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
29+
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
30+
aws-region: ${{ env.AWS_REGION }}
31+
32+
# Login to Amazon ECR
33+
- name: Login to Amazon ECR
34+
id: login-ecr
35+
uses: aws-actions/amazon-ecr-login@v2
36+
37+
# Build and tag the Docker image
38+
- name: Build and Tag Docker Image
39+
run: |
40+
docker build -t fast-python-mongo-demo-app .
41+
docker tag fast-python-mongo-demo-app:latest ${{ steps.login-ecr.outputs.registry }}/fast-python-mongo-demo-app:latest
42+
43+
# Push the Docker image to Amazon ECR
44+
- name: Push Docker Image to Amazon ECR
45+
run: |
46+
docker push ${{ steps.login-ecr.outputs.registry }}/fast-python-mongo-demo-app:latest

Dockerfile

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Use an official Python base image with Python 3.12
2+
FROM python:3.12-slim
3+
4+
# Working Directory
5+
WORKDIR /app
6+
7+
# Copy Files
8+
COPY . /app
9+
10+
# Step 4: Install Poetry
11+
RUN pip install poetry
12+
13+
# Step 5: Disable Virtual Environments
14+
RUN poetry config virtualenvs.create false
15+
16+
# Step 6: Install Dependencies
17+
RUN poetry install --no-dev
18+
19+
# Step 7: Expose Port
20+
EXPOSE 8000
21+
22+
# Step 8: CMD to Run App
23+
CMD ["poetry", "run", "uvicorn", "app.main:app", "--host", "0.0.0.0", "--reload"]

README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,15 @@
11
# fast-python-mongo-demo
2+
3+
## Local setup
4+
5+
### how to create docker build
6+
```
7+
docker build -t fast-python-mongo-demo-app .
8+
```
9+
10+
### how to run local env
11+
12+
```
13+
cd local
14+
docker-compose up
15+
```

__pycache__/app.cpython-312.pyc

2.65 KB
Binary file not shown.

app/__init__.py

Whitespace-only changes.
185 Bytes
Binary file not shown.
2.66 KB
Binary file not shown.

app/main.py

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import os
2+
from fastapi import FastAPI
3+
from pydantic import BaseModel
4+
import motor.motor_asyncio
5+
6+
app = FastAPI(
7+
title="Fastapi and mongodb demo API",
8+
summary="A sample application showing how to use FastAPI to add a ResT API to a MongoDB collection.",
9+
)
10+
11+
# MongoDB setup
12+
# Access MongoDB credentials from environment variables
13+
mongodb_url = os.getenv("DB_URL", "mongodb://localhost:27017")
14+
mongodb_user = os.getenv("DB_USER", "root")
15+
mongodb_password = os.getenv("DB_PASSWORD", "pw")
16+
17+
client = motor.motor_asyncio.AsyncIOMotorClient(
18+
mongodb_url,
19+
username=mongodb_user,
20+
password=mongodb_password
21+
)
22+
db = client.college
23+
word_collection = db.get_collection("word")
24+
25+
26+
class WordModel(BaseModel):
27+
"""Model for word data."""
28+
word: str
29+
30+
31+
@app.get(
32+
"/hello",
33+
response_description="Get hello world",
34+
response_model=WordModel,
35+
)
36+
async def get_helloworld():
37+
"""
38+
Returns a hello world message. If word exists in the collection, return 'Hello {word}',
39+
otherwise return 'Hello None'.
40+
"""
41+
word_document = await word_collection.find_one({}) # Find the first document in the collection
42+
43+
if word_document and "word" in word_document:
44+
word = word_document["word"]
45+
else:
46+
word = "None"
47+
48+
return WordModel(word=f"Hello {word}")
49+
50+
@app.on_event("startup")
51+
async def startup_db():
52+
"""
53+
Initialize the word collection at application startup if it is empty.
54+
"""
55+
# Check if the collection is empty
56+
existing_word = await word_collection.find_one({"word": {"$exists": True}})
57+
if not existing_word:
58+
# Initialize the word field if no document exists
59+
await word_collection.insert_one({"word": "Serverless Salad Infrapal World"})
60+
print("Initialized word collection with default word value.")

local/docker-compose.yaml

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
version: '3.8'
2+
3+
services:
4+
fastapi:
5+
image: fast-python-mongo-demo-app:latest
6+
ports:
7+
- "8000:8000"
8+
environment:
9+
- DB_URL=mongodb://mongo:27017
10+
- DB_USER=root
11+
- DB_PASSWORD=pw
12+
depends_on:
13+
- mongo
14+
networks:
15+
- app-network
16+
17+
mongo:
18+
image: mongo:latest
19+
container_name: mongo-db
20+
ports:
21+
- "27017:27017"
22+
environment:
23+
- MONGO_INITDB_ROOT_USERNAME=root
24+
- MONGO_INITDB_ROOT_PASSWORD=pw
25+
volumes:
26+
- mongo-data:/data/db
27+
networks:
28+
- app-network
29+
30+
networks:
31+
app-network:
32+
driver: bridge
33+
34+
volumes:
35+
mongo-data:
36+
driver: local

0 commit comments

Comments
 (0)