Hussein El-Hadidy commited on
Commit
75431df
Β·
1 Parent(s): ae3bfb2

Connected to database

Browse files
Files changed (4) hide show
  1. __pycache__/main.cpython-313.pyc +0 -0
  2. app.py +23 -0
  3. main.py +30 -0
  4. requirements.txt +1 -0
__pycache__/main.cpython-313.pyc ADDED
Binary file (1.46 kB). View file
 
app.py CHANGED
@@ -1,7 +1,30 @@
1
  from fastapi import FastAPI
 
 
2
 
3
  app = FastAPI()
4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5
  @app.get("/")
6
  def greet_json():
7
  return {"Hello": "World!"}
 
 
 
 
 
 
 
 
 
1
  from fastapi import FastAPI
2
+ from pymongo.mongo_client import MongoClient
3
+ from pymongo.server_api import ServerApi
4
 
5
  app = FastAPI()
6
 
7
+ # βœ… MongoDB connection string (hardcoded)
8
+ uri = "mongodb+srv://husseinelhadidy03:<your_password>@cluster0.ycuagnj.mongodb.net/?retryWrites=true&w=majority"
9
+
10
+ # βœ… Connect to MongoDB
11
+ client = MongoClient(uri, server_api=ServerApi('1'))
12
+
13
+ try:
14
+ client.admin.command('ping')
15
+ print("βœ… Successfully connected to MongoDB!")
16
+ except Exception as e:
17
+ print("❌ MongoDB connection failed:", e)
18
+
19
+ # βœ… Test route
20
  @app.get("/")
21
  def greet_json():
22
  return {"Hello": "World!"}
23
+
24
+ # βœ… Optional: example route to count documents
25
+ @app.get("/count")
26
+ def count_docs():
27
+ db = client["my_database"]
28
+ collection = db["my_collection"]
29
+ count = collection.count_documents({})
30
+ return {"document_count": count}
main.py ADDED
@@ -0,0 +1,30 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from fastapi import FastAPI
2
+ from pymongo.mongo_client import MongoClient
3
+ from pymongo.server_api import ServerApi
4
+
5
+ app = FastAPI()
6
+
7
+ # βœ… MongoDB connection string (hardcoded)
8
+ uri = "mongodb+srv://husseinelhadidy03:<your_password>@cluster0.ycuagnj.mongodb.net/?retryWrites=true&w=majority"
9
+
10
+ # βœ… Connect to MongoDB
11
+ client = MongoClient(uri, server_api=ServerApi('1'))
12
+
13
+ try:
14
+ client.admin.command('ping')
15
+ print("βœ… Successfully connected to MongoDB!")
16
+ except Exception as e:
17
+ print("❌ MongoDB connection failed:", e)
18
+
19
+ # βœ… Test route
20
+ @app.get("/")
21
+ def greet_json():
22
+ return {"Hello": "World!"}
23
+
24
+ # βœ… Optional: example route to count documents
25
+ @app.get("/count")
26
+ def count_docs():
27
+ db = client["my_database"]
28
+ collection = db["my_collection"]
29
+ count = collection.count_documents({})
30
+ return {"document_count": count}
requirements.txt CHANGED
@@ -1,2 +1,3 @@
1
  fastapi
2
  uvicorn[standard]
 
 
1
  fastapi
2
  uvicorn[standard]
3
+ pymongo