This repository has been archived by the owner on Mar 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use a sample FastAPI application for demo
- Loading branch information
Showing
5 changed files
with
105 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)"} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |