-
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.
🔀 Merge pull request #46 from cnr-ibba/devel
🔖 release v0.3.0
- Loading branch information
Showing
43 changed files
with
1,264 additions
and
1,358 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
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,3 +1,17 @@ | ||
{ | ||
"esbonio.sphinx.confDir": "${workspaceFolder}/flask-data/smarter/docs/source" | ||
"esbonio.sphinx.confDir": "${workspaceFolder}/flask-data/smarter/docs/source", | ||
"cSpell.words": [ | ||
"affymetrix", | ||
"exitfirst", | ||
"iname", | ||
"INITDB", | ||
"jsonify", | ||
"mongodump", | ||
"mongorestore", | ||
"pytest", | ||
"reqparse", | ||
"showlocals", | ||
"uwsgi", | ||
"uwsgitop" | ||
] | ||
} |
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,7 +1,5 @@ | ||
|
||
# User/password credentials are stored in .env file | ||
version: '3.8' | ||
|
||
services: | ||
|
||
mongo: | ||
|
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 |
---|---|---|
|
@@ -13,7 +13,7 @@ | |
__copyright__ = "Copyright 2021, IBBA-CNR" | ||
__credits__ = ["Paolo Cozzi, ..."] | ||
__license__ = "GNU General Public License v3" | ||
__version__ = "0.2.2" | ||
__version__ = "0.3.0.dev0" | ||
__maintainer__ = "Paolo Cozzi" | ||
__email__ = '[email protected]' | ||
__status__ = "Development" |
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 |
---|---|---|
|
@@ -6,25 +6,24 @@ | |
@author: Paolo Cozzi <[email protected]> | ||
""" | ||
|
||
import os | ||
|
||
from bson import ObjectId | ||
import logging | ||
from logging.config import dictConfig | ||
from logging.handlers import SMTPHandler | ||
|
||
from flask import Flask, redirect, url_for | ||
from decouple import config | ||
from flask import Flask, redirect, url_for, has_request_context, request | ||
from flask.logging import default_handler | ||
from flask_restful import Api | ||
from flask_bcrypt import Bcrypt | ||
from flask_jwt_extended import JWTManager | ||
from flask.json import JSONEncoder | ||
from flask_cors import CORS | ||
from flasgger import Swagger | ||
|
||
from database.db import initialize_db, DB_ALIAS | ||
from resources.errors import errors | ||
from resources.routes import initialize_routes | ||
from commands import usersbp | ||
|
||
__version__ = "0.2.2" | ||
__version__ = "0.3.0.dev0" | ||
|
||
# https://flask.palletsprojects.com/en/2.0.x/logging/#basic-configuration | ||
dictConfig({ | ||
|
@@ -43,6 +42,43 @@ | |
} | ||
}) | ||
|
||
mail_handler = SMTPHandler( | ||
mailhost=( | ||
config('EMAIL_HOST', default='localhost'), | ||
config('EMAIL_PORT', cast=int, default=1025) | ||
), | ||
fromaddr=config('DEFAULT_FROM_EMAIL', default="[email protected]"), | ||
toaddrs=[email.strip() for email in config( | ||
'ADMINS', default="[email protected]").split(',')], | ||
subject='SMARTER-backend Application Error', | ||
credentials=( | ||
config('EMAIL_HOST_USER', default=None), | ||
config('EMAIL_HOST_PASSWORD', default=None) | ||
), | ||
secure=() | ||
) | ||
mail_handler.setLevel(logging.ERROR) | ||
|
||
|
||
class RequestFormatter(logging.Formatter): | ||
def format(self, record): | ||
if has_request_context(): | ||
record.url = request.url | ||
record.remote_addr = request.remote_addr | ||
else: | ||
record.url = None | ||
record.remote_addr = None | ||
|
||
return super().format(record) | ||
|
||
|
||
formatter = RequestFormatter( | ||
'[%(asctime)s] %(remote_addr)s requested %(url)s\n' | ||
'%(levelname)s in %(module)s: %(message)s' | ||
) | ||
mail_handler.setFormatter(formatter) | ||
default_handler.setFormatter(formatter) | ||
|
||
|
||
class CustomJSONEncoder(JSONEncoder): | ||
def default(self, obj): | ||
|
@@ -54,33 +90,30 @@ def default(self, obj): | |
|
||
|
||
# https://stackoverflow.com/a/56474420/4385116 | ||
def create_app(config={}): | ||
def create_app(): | ||
"""This function create Flask app. Is required by wsgi because it need to | ||
be called after service is started and forked, not when importing the | ||
module during initialization. To start the flask app, first import | ||
the module and then create all the stuff by invoking this function | ||
You need call the run method on the returned values to start acception | ||
You need call the run method on the returned values to start accepting | ||
requests | ||
Args: | ||
config (dict): pass parameters to this app (not yet defined) | ||
Returns: | ||
Flask: a flask initialized application | ||
""" | ||
|
||
app = Flask(__name__) | ||
CORS(app) | ||
api = Api(app, errors=errors) | ||
Bcrypt(app) | ||
JWTManager(app) | ||
|
||
# check debug mode | ||
if config('DEBUG', cast=bool, default=True): | ||
# in debug mode, the default logging will be set to DEBUG level | ||
app.debug = True | ||
|
||
# deal with ObjectId in json responses | ||
app.json_encoder = CustomJSONEncoder | ||
|
||
# workaround to make flasgger deal with jwt-token headers | ||
app.config["JWT_AUTH_URL_RULE"] = True | ||
|
||
# Swagger stuff | ||
swagger_template = { | ||
"swagger": "2.0", | ||
|
@@ -115,38 +148,33 @@ def create_app(config={}): | |
|
||
# http://docs.mongoengine.org/projects/flask-mongoengine/en/latest/#configuration | ||
app.config['MONGODB_SETTINGS'] = { | ||
'host': 'mongodb://mongo/smarter', | ||
'username': os.getenv("MONGODB_SMARTER_USER"), | ||
'password': os.getenv("MONGODB_SMARTER_PASS"), | ||
'host': config( | ||
'MONGODB_SMARTER_DB', | ||
default='mongodb://mongo/smarter' | ||
), | ||
'username': config('MONGODB_SMARTER_USER'), | ||
'password': config("MONGODB_SMARTER_PASS"), | ||
'authentication_source': 'admin', | ||
'alias': DB_ALIAS, | ||
# NOTE: This fixes "UserWarning: MongoClient opened before fork." | ||
# I'm not aware of side effects yet. Default value is/was "True" | ||
'connect': False | ||
} | ||
|
||
# override configuration with custom values | ||
if 'host' in config: | ||
app.logger.error(f"Setting custom host: {config['host']}") | ||
app.config['MONGODB_SETTINGS']['host'] = config['host'] | ||
|
||
# https://flask-jwt-extended.readthedocs.io/en/stable/basic_usage/ | ||
app.config["JWT_SECRET_KEY"] = os.getenv('JWT_SECRET_KEY') | ||
|
||
# connect to database | ||
initialize_db(app) | ||
|
||
app.logger.debug("Database initialized") | ||
app.logger.debug(f"Got encoder {app.json_encoder}") | ||
|
||
# you MUST register the blueprint | ||
app.register_blueprint(usersbp) | ||
|
||
# add resources | ||
initialize_routes(api) | ||
|
||
app.logger.debug("Routes initialized") | ||
|
||
if not app.debug: | ||
app.logger.addHandler(mail_handler) | ||
|
||
# add a redirect for the index page | ||
@app.route('/smarter-api/') | ||
def index(): | ||
|
This file was deleted.
Oops, something went wrong.
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
Oops, something went wrong.