Skip to content
This repository has been archived by the owner on Mar 16, 2024. It is now read-only.

Commit

Permalink
Use a sample FastAPI application for demo
Browse files Browse the repository at this point in the history
  • Loading branch information
lucj committed Oct 15, 2023
1 parent 07ce1e2 commit 2ff70aa
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 20 deletions.
43 changes: 43 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,49 @@ $ curl k8s-test3e3d-apppubli-58f1d9adf4-5081003c9ca93235.elb.us-east-2.amazonaws
{"message":"Webpage viewed 1 time(s)"}
```

Note: This example uses the auto-generated user, which is limited to the default database, via the following env variables:

```
containers: {
app: {
build: {
context: "."
target: "dev"
}
consumes: ["db"]
ports: publish: "8000/http"
env: {
DB_HOST: "@{service.db.address}"
DB_PORT: "@{service.db.port.27017}"
DB_NAME: "@{service.db.data.dbName}" <--
DB_USER: "@{service.db.secrets.user.username}" <--
DB_PASS: "@{service.db.secrets.user.password}" <--
}
}
}
```

We could have used the admin user instead, this one does not have any limitation on the whole mongodb instance.

```
containers: {
app: {
build: {
context: "."
target: "dev"
}
consumes: ["db"]
ports: publish: "8000/http"
env: {
DB_HOST: "@{service.db.address}"
DB_PORT: "@{service.db.port.27017}"
DB_USER: "@{service.db.secrets.admin.username}" <--
DB_PASS: "@{service.db.secrets.admin.password}" <--
}
}
}
```

## Parameters

When the single *MongoDB* instance is created, a default user is created, this one only has admin access against a given database. By default:
Expand Down
31 changes: 11 additions & 20 deletions examples/Acornfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,18 @@ services: db: {

containers: {
app: {
image: "mongo:6.0.5"
entrypoint: ["/bin/sh", "-c", "/check-db.sh"]
env: {
build: {
context: "."
target: "dev"
}
consumes: ["db"]
ports: publish: "8000/http"
env: {
DB_HOST: "@{service.db.address}"
DB_NAME: "@{service.db.data.dbName}"
DB_USER: "@{service.db.secrets.user.username}"
DB_PASS: "@{service.db.secrets.user.password}"
DB_PORT: "@{service.db.port.27017}"
DB_NAME: "@{service.db.data.dbName}"
DB_USER: "@{service.db.secrets.user.username}"
DB_PASS: "@{service.db.secrets.user.password}"
}
files: "/check-db.sh": """
echo "Will try to connect to cluster"
while true; do
echo "=> testing DB connection..."
mongosh --host ${DB_HOST} -u ${DB_USER} -p ${DB_PASS} --eval "db.adminCommand('ping')"
if [ $? -eq 0 ]; then
break
else
sleep 5
fi
done
echo "connected to the DB"
sleep 3600
"""
}
}
15 changes: 15 additions & 0 deletions examples/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
FROM python:3.9 as base
WORKDIR /app

FROM base as build
COPY requirements.txt requirements.txt
RUN pip install --no-cache-dir --upgrade -r requirements.txt
COPY . .

FROM build as dev
EXPOSE 8000
CMD ["uvicorn", "main:app", "--reload", "--host", "0.0.0.0", "--port", "8000", "--proxy-headers"]

FROM build as production
EXPOSE 8000
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8000"]
33 changes: 33 additions & 0 deletions examples/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import os
from fastapi import FastAPI
from pymongo import MongoClient

app = FastAPI()

DB_HOST = os.environ.get('DB_HOST')
DB_PORT = os.environ.get('DB_PORT')
DB_USER = os.environ.get('DB_USER')
DB_PASS = os.environ.get('DB_PASS')
DB_NAME = os.environ.get('DB_NAME')

MONGO_URI = f"mongodb://{DB_USER}:{DB_PASS}@{DB_HOST}:{DB_PORT}/{DB_NAME}?authSource=admin"

client = MongoClient(MONGO_URI)
db = client[DB_NAME]
collection = db.hits_collection

@app.get('/')
def hello():
# If the counter does not exist, we initialize it with a value of 0
counter_data = collection.find_one({"_id": "page_counter"})
if not counter_data:
collection.insert_one({"_id": "page_counter", "count": 0})
counter = 0
else:
counter = counter_data["count"]

# Increment the counter
collection.update_one({"_id": "page_counter"}, {"$inc": {"count": 1}})
counter += 1

return {"message": f"Webpage viewed {counter} time(s)"}
3 changes: 3 additions & 0 deletions examples/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
fastapi == 0.101.0
uvicorn[standard] == 0.23.2
pymongo[srv]==3.12.0

0 comments on commit 2ff70aa

Please sign in to comment.