Skip to content
Snippets Groups Projects
Commit 954577b4 authored by Sara Savanovic Djordjevic's avatar Sara Savanovic Djordjevic
Browse files

add: initial database connection attempt

parent d02b1d9b
No related branches found
No related tags found
No related merge requests found
......@@ -41,3 +41,5 @@ This project requires the use of MongoDB. To use the application, a MongoDB Atla
11. You have now created a database deployment, but you will still have to configure the connection to the database.
### Database connection
1. To create a connection with the database,
![alt text](image.png)
\ No newline at end of file
image-1.png

48.2 KiB

image.png 0 → 100644
image.png

34.9 KiB

# MongoDB connection certificate
X509-cert-2722575229130410888.pem
\ No newline at end of file
from flask import Flask, jsonify
from pymongo import MongoClient
from pymongo.server_api import ServerApi
app = Flask(__name__)
# Default route
@app.route('/')
def home():
return jsonify(message='Welcome to the API!')
# Example endpoint
@app.route('/api/data', methods=['GET'])
def get_data():
data = {'example_data': [1, 2, 3, 4, 5]}
return jsonify(data)
# Initialise database connection
def init_db():
# Initialise database connection
uri = "mongodb+srv://icemapcluster.i02epob.mongodb.net/?authSource=%24external&authMechanism=MONGODB-X509&retryWrites=true&w=majority"
try:
client = MongoClient(uri,
tls=True,
tlsCertificateKeyFile='server/X509-cert-2722575229130410888.pem',
server_api=ServerApi('1'))
db = client['IceMapDatabase']
collection = db['GeoDataCollection']
# Ping to check connection
client.admin.command('ping')
print("Connected to MongoDB!")
except ConnectionFailure as e:
print(f"Failed to connect to MongoDB: {e}")
if __name__ == "__main__":
init_db()
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment