Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
P
PROG2900
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Sara Savanovic Djordjevic
PROG2900
Commits
954577b4
Commit
954577b4
authored
1 year ago
by
Sara Savanovic Djordjevic
Browse files
Options
Downloads
Patches
Plain Diff
add: initial database connection attempt
parent
d02b1d9b
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
README.md
+2
-0
2 additions, 0 deletions
README.md
image-1.png
+0
-0
0 additions, 0 deletions
image-1.png
image.png
+0
-0
0 additions, 0 deletions
image.png
server/.gitignore
+2
-0
2 additions, 0 deletions
server/.gitignore
server/main.py
+44
-0
44 additions, 0 deletions
server/main.py
with
48 additions
and
0 deletions
README.md
+
2
−
0
View file @
954577b4
...
...
@@ -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,

\ No newline at end of file
This diff is collapsed.
Click to expand it.
image-1.png
0 → 100644
+
0
−
0
View file @
954577b4
48.2 KiB
This diff is collapsed.
Click to expand it.
image.png
0 → 100644
+
0
−
0
View file @
954577b4
34.9 KiB
This diff is collapsed.
Click to expand it.
server/.gitignore
0 → 100644
+
2
−
0
View file @
954577b4
# MongoDB connection certificate
X509-cert-2722575229130410888.pem
\ No newline at end of file
This diff is collapsed.
Click to expand it.
server/main.py
+
44
−
0
View file @
954577b4
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
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment