-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
11 changed files
with
131 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
FROM python:3.8-slim-buster | ||
EXPOSE 8080 | ||
|
||
ENV PYTHONUNBUFFERED 1 | ||
ENV APP_HOME /app | ||
|
||
COPY requirements.txt . | ||
|
||
RUN pip install --no-cache-dir -U pip \ | ||
&& pip install --no-cache-dir -r requirements.txt | ||
|
||
WORKDIR ${APP_HOME} | ||
|
||
COPY src src | ||
COPY main.py . | ||
COPY docker-entrypoint.sh . | ||
COPY .env . | ||
|
||
ENTRYPOINT ["/bin/bash"] | ||
CMD ["./docker-entrypoint.sh"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
version: "3.5" | ||
services: | ||
user_app: | ||
build: . | ||
container_name: cdslab_models_app | ||
env_file: .env | ||
image: cdslab_user | ||
networks: | ||
- cdslab_models | ||
ports: | ||
- 5000:5000 | ||
volumes: | ||
- ./src:/app/src | ||
|
||
user_mongo: | ||
container_name: cdslab_models_mongo | ||
environment: | ||
MONGO_INITDB_ROOT_USERNAME: cdsuser | ||
MONGO_INITDB_ROOT_PASSWORD: cdspass | ||
image: mongo:3-xenial | ||
networks: | ||
- cdslab_models | ||
ports: | ||
- 27017:27017 | ||
volumes: | ||
- /tmp/data/cdslab_models/:/data/db | ||
|
||
|
||
networks: | ||
cdslab_models: | ||
name: cdslab_user | ||
driver: bridge |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
#!/bin/bash | ||
|
||
if [ -z "${PORT}" ] ; then PORT=8080; fi | ||
|
||
if [ -z "${HOST}" ] ; then HOST=0.0.0.0; fi | ||
|
||
uvicorn main:app --port=${PORT} --host=${HOST} --reload |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,63 +1,59 @@ | ||
from typing import Generator, Tuple | ||
|
||
from pymongo import MongoClient | ||
from pymongo.database import Database | ||
from pymongo.collection import Collection | ||
from pymongo.database import Database | ||
|
||
from src.config import db_config | ||
|
||
|
||
def api_get_db_connection( | ||
db_uri: str = db_config['MONGO_URI'], | ||
) -> Generator[MongoClient, None, None]: | ||
"""Creates connection to Mongodb server. | ||
Yields | ||
------ | ||
db_connection: MongoClient | ||
Object containing the db connection. | ||
""" | ||
db_connection = MongoClient(db_uri) | ||
|
||
try: | ||
yield db_connection | ||
finally: | ||
db_connection.close() | ||
|
||
|
||
def get_db( | ||
db_uri: str = db_config['MONGO_URI'], | ||
db_name: str = db_config['MONGO_DB'] | ||
) -> Tuple[MongoClient, Database]: | ||
"""Gets Mongodb connection and database. | ||
Parameters | ||
---------- | ||
db_uri: str | ||
Mongo server URI. | ||
db_name: str | ||
Mongo database name. | ||
Returns | ||
------- | ||
db_connection : MongoClient | ||
db : pymongo.database.Database | ||
""" | ||
db_connection = MongoClient(db_uri) | ||
db: Database = db_connection[db_name] | ||
|
||
return db_connection, db | ||
|
||
|
||
def get_collection( | ||
coll_name: str = db_config['CMODELS_COLL'] | ||
) -> Tuple[MongoClient, Collection]: | ||
""" | ||
Returns | ||
------- | ||
db_connection: pymongo.MongoClient | ||
coll: pymongo.collection.Collection | ||
""" | ||
db_connection, db = get_db() | ||
coll: Collection = db[coll_name] | ||
return db_connection, coll | ||
from src.utils.patterns import Singleton | ||
|
||
|
||
class MongoConnection(metaclass=Singleton): | ||
db_uri = db_config.get('MONGO_URI') | ||
db_name = db_config.get('MONGO_DB') | ||
collection_name = db_config.get('CMODELS_COLL') | ||
|
||
def api_get_db_connection(self) -> Generator[MongoClient, None, None]: | ||
"""Creates connection to Mongodb server. | ||
Yields | ||
------ | ||
db_connection: MongoClient | ||
Object containing the db connection. | ||
""" | ||
db_connection = MongoClient(self.db_uri) | ||
|
||
try: | ||
yield db_connection | ||
finally: | ||
db_connection.close() | ||
|
||
def get_db(self) -> Tuple[MongoClient, Database]: | ||
"""Gets Mongodb connection and database. | ||
Returns | ||
------- | ||
db_connection : MongoClient | ||
db : pymongo.database.Database | ||
""" | ||
db_connection = MongoClient(self.db_uri) | ||
db: Database = db_connection[self.db_name] | ||
|
||
return db_connection, db | ||
|
||
def get_collection( | ||
self, | ||
collection_name: str = None | ||
) -> Tuple[MongoClient, Collection]: | ||
""" | ||
Returns | ||
------- | ||
db_connection: pymongo.MongoClient | ||
coll: pymongo.collection.Collection | ||
""" | ||
if not collection_name: | ||
collection_name = self.collection_name | ||
pass | ||
db_connection, db = self.get_db() | ||
coll: Collection = db[collection_name] | ||
return db_connection, coll |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,10 @@ | ||
from src.db.mongo import get_collection | ||
from src.db.mongo import MongoConnection | ||
from src.interfaces.cmodels import CModelsInterface | ||
|
||
|
||
class CmodelUseCases: | ||
@staticmethod | ||
def update_cmodels_collection(): | ||
db_connection, cmodels_coll = get_collection() | ||
db_connection, cmodels_coll = MongoConnection().get_collection() | ||
cmodels_interface = CModelsInterface(db_connection, cmodels_coll) | ||
cmodels_interface.insert_all_cmodel_documents() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
class Singleton(type): | ||
_instances = {} | ||
|
||
def __call__(cls, *args, **kwargs): | ||
if cls not in cls._instances: | ||
cls._instances[cls] = super(Singleton, cls).__call__(*args, **kwargs) | ||
return cls._instances[cls] |