backend/src/app.py

83 lines
2.0 KiB
Python
Raw Normal View History

2020-11-24 17:05:02 +01:00
#!/usr/bin/env python3
import logging
from flask import Flask
from flask_restful import Api
import sentry_sdk
from sentry_sdk.integrations.flask import FlaskIntegration
from sentry_sdk.integrations.sqlalchemy import SqlalchemyIntegration
from healthcheck import HealthCheck
from config import *
from db import db
2020-11-24 20:26:02 +01:00
from jwtman import jwtman
from fbcrypt import bcrypt
2020-11-24 17:05:02 +01:00
from marshm import ma
from healthchecks import health_database_status
2020-11-24 20:26:02 +01:00
from resources import SignupApi, LoginApi
2020-11-24 17:05:02 +01:00
"""
Main Flask RESTful API
"""
__author__ = "@tormakris"
__copyright__ = "Copyright 2020, videON Team"
__module_name__ = "app"
__version__text__ = "1"
if SENTRY_DSN:
sentry_sdk.init(
dsn=SENTRY_DSN,
integrations=[FlaskIntegration(), SqlalchemyIntegration()],
traces_sample_rate=1.0,
send_default_pii=True,
release=RELEASE_ID,
environment=RELEASEMODE,
_experiments={"auto_enabling_integrations": True}
)
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] =\
f"postgresql://{POSTGRES_USERNAME}:{POSTGRES_PASSWORD}@{POSTGRES_HOSTNAME}:5432/{POSTGRES_DB}"
2020-11-24 20:26:02 +01:00
app.config['JWT_SECRET_KEY'] = JWT_SECRET_KEY
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
2020-11-24 17:05:02 +01:00
api = Api(app)
health = HealthCheck(app, "/healthz")
db.init_app(app)
ma.init_app(app)
2020-11-24 20:26:02 +01:00
bcrypt.init_app(app)
jwtman.init_app(app)
2020-11-24 17:05:02 +01:00
with app.app_context():
db.create_all()
2020-11-24 20:26:02 +01:00
2020-11-24 17:05:02 +01:00
formatter = logging.Formatter(
fmt="%(asctime)s - %(levelname)s - %(module)s - %(message)s"
)
handler = logging.StreamHandler()
handler.setFormatter(formatter)
logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)
logger.addHandler(handler)
# api.add_resource(SampleResource, "/sample")
2020-11-24 20:26:02 +01:00
api.add_resource(SignupApi, '/api/auth/signup')
api.add_resource(LoginApi, '/api/auth/login')
2020-11-24 17:05:02 +01:00
health.add_check(health_database_status)
2020-11-24 20:26:02 +01:00
@app.errorhandler(404)
def page_not_found(e):
return {'status': 'error', 'message': 'page not found'}, 404
2020-11-24 17:05:02 +01:00
if __name__ == "__main__":
app.run(
debug=bool(DEBUG),
host="0.0.0.0",
port=int(PORT),
)