Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

connect to mongo container #113

Open
wants to merge 3 commits into
base: backend/python
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 12 additions & 6 deletions backend/controllers/articleController.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,23 @@
from flask import jsonify
from .dummy_data import *
from database import db

#https://pymongo.readthedocs.io/en/stable/tutorial.html#getting-a-collection
articles = db["articles"]

articles.insert_one(dummy_article)
articles.insert_one(dummy_article_2)


# TODO: Implement auth middleware logic to check if user is logged in


# TODO: fetch one article from database
def get_article(id):
if id == "1":
return jsonify(dummy_article), 200
elif id == "2":
return jsonify(dummy_article_2), 200
else:
return "", 404
article = articles.find_one(filter = {"_id" : id})
if article is not None:
return jsonify(article), 200
return "", 404


# TODO: create one article
Expand Down
23 changes: 23 additions & 0 deletions backend/database/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@

from pymongo import MongoClient
from os import getenv

class MissingEnvironmentVariable(Exception):
pass

DB_PORT, DB_NAME = getenv("DB_PORT"), getenv("DB_NAME")

if DB_NAME is None:
raise MissingEnvironmentVariable("DB_NAME is missing")

if DB_PORT is None:
raise MissingEnvironmentVariable("DB_PORT is missing")
else:
DB_PORT = int(DB_PORT)
print("connecting to port : " ,DB_PORT)


client = MongoClient("mongodb", DB_PORT)

#https://pymongo.readthedocs.io/en/stable/tutorial.html#getting-a-database
db = getattr(client, DB_NAME)
Binary file not shown.
1 change: 1 addition & 0 deletions backend/requirements.txt
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
flask==2.2.3
pymongo
1 change: 1 addition & 0 deletions docker-compose-dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ services:
- ./backend/middleware:/backend/middleware:ro
- ./backend/models:/backend/models:ro
- ./backend/routes:/backend/routes:ro
- ./backend/database:/backend/database:ro

react-app-dev:
depends_on:
Expand Down