From 0ecfcdc6085ec8b82c867e99a371f81b8475bfda Mon Sep 17 00:00:00 2001 From: Alix JEUDI--LEMOINE Date: Mon, 3 Feb 2025 16:14:21 +0100 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Add=20stub=20script=20&=20execution?= =?UTF-8?q?=20of=20it=20in=20Dockerfile?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/stub.py | 60 +++++++++++++++++++++++++++++++++++++++++++++++ docker/Dockerfile | 3 +++ 2 files changed, 63 insertions(+) create mode 100644 app/stub.py diff --git a/app/stub.py b/app/stub.py new file mode 100644 index 0000000..87c958f --- /dev/null +++ b/app/stub.py @@ -0,0 +1,60 @@ +import pymongo +from datetime import datetime +from config import MONGODB_URL, MONGODB_USERNAME, MONGODB_PASSWORD, MONGODB_DATABASE +from utils import get_password_hash + +client = pymongo.MongoClient(MONGODB_URL, username=MONGODB_USERNAME, password=MONGODB_PASSWORD) +db = client[MONGODB_DATABASE] + +def populate_data(): + users_collection = db["users"] + pins_collection = db["pins"] + + user1_id = users_collection.insert_one({ + "username": "string", + "password": get_password_hash("string") + }).inserted_id + + user2_id = users_collection.insert_one({ + "username": "test", + "password": get_password_hash("test") + }).inserted_id + + pins_collection.insert_many([ + { + "title": f"Tour Eiffel", + "description": "Description A", + "location": [48.858296, 2.294526], + "files": ["file_a"], + "owner": str(user1_id), + "created_at": datetime.utcnow() + }, + { + "title": f"Mont St Michel", + "description": "Description B", + "location": [48.636111, -1.511389], + "files": ["file_b"], + "owner": str(user1_id), + "created_at": datetime.utcnow() + }, + { + "title": f"Eiffel Tower", + "description": "Description X", + "location": [48.858296, 2.294526], + "files": ["file_x"], + "owner": str(user2_id), + "created_at": datetime.utcnow() + }, + { + "title": f"Mont Saint Michel", + "description": "Description Y", + "location": [48.636111, -1.511389], + "files": ["file_y"], + "owner": str(user2_id), + "created_at": datetime.utcnow() + } + ]) + +if __name__ == "__main__": + populate_data() + print("Data inserted.") diff --git a/docker/Dockerfile b/docker/Dockerfile index 753dc1c..bb173c9 100644 --- a/docker/Dockerfile +++ b/docker/Dockerfile @@ -16,5 +16,8 @@ COPY . . # Expose API port EXPOSE 80 +# Runs the stub script +RUN "python app/stub.py" + # Launches the uvicorn server (command from offical FastAPI documentation) CMD ["fastapi", "run", "app/main.py", "--port", "80"] \ No newline at end of file