2015-12-10 10:56:59 +00:00
|
|
|
import os
|
2016-02-22 17:17:29 +00:00
|
|
|
import re
|
2016-01-19 15:32:33 +00:00
|
|
|
from flask import request, url_for
|
2015-12-15 10:47:20 +00:00
|
|
|
from flask import Flask, _request_ctx_stack
|
2016-01-07 17:31:17 +00:00
|
|
|
from flask.ext.sqlalchemy import SQLAlchemy
|
2016-01-11 15:07:13 +00:00
|
|
|
from flask_marshmallow import Marshmallow
|
2015-12-15 10:47:20 +00:00
|
|
|
from werkzeug.local import LocalProxy
|
2016-01-07 13:28:56 +00:00
|
|
|
from utils import logging
|
2016-02-09 13:31:45 +00:00
|
|
|
from app.celery.celery import NotifyCelery
|
2016-02-15 16:01:14 +00:00
|
|
|
from app.clients.sms.twilio import TwilioClient
|
2016-02-17 12:57:51 +00:00
|
|
|
from app.clients.sms.firetext import FiretextClient
|
2016-02-17 17:48:23 +00:00
|
|
|
from app.clients.email.aws_ses import AwsSesClient
|
2016-02-16 15:28:30 +00:00
|
|
|
from app.encryption import Encryption
|
2015-12-10 10:56:59 +00:00
|
|
|
|
2016-01-07 17:31:17 +00:00
|
|
|
db = SQLAlchemy()
|
2016-01-11 15:07:13 +00:00
|
|
|
ma = Marshmallow()
|
2016-02-16 14:06:56 +00:00
|
|
|
notify_celery = NotifyCelery()
|
2016-02-15 16:01:14 +00:00
|
|
|
twilio_client = TwilioClient()
|
2016-02-17 12:57:51 +00:00
|
|
|
firetext_client = FiretextClient()
|
2016-02-17 17:48:23 +00:00
|
|
|
aws_ses_client = AwsSesClient()
|
2016-02-16 15:28:30 +00:00
|
|
|
encryption = Encryption()
|
2016-01-07 17:31:17 +00:00
|
|
|
|
2015-12-15 10:47:20 +00:00
|
|
|
api_user = LocalProxy(lambda: _request_ctx_stack.top.api_user)
|
|
|
|
|
|
2015-12-10 10:56:59 +00:00
|
|
|
|
2016-02-16 15:25:46 +00:00
|
|
|
def create_app():
|
2015-12-10 10:56:59 +00:00
|
|
|
application = Flask(__name__)
|
|
|
|
|
|
2016-02-16 15:25:46 +00:00
|
|
|
application.config.from_object(os.environ['NOTIFY_API_ENVIRONMENT'])
|
2015-12-10 10:56:59 +00:00
|
|
|
|
2016-02-17 09:14:37 +00:00
|
|
|
init_app(application)
|
2016-01-07 17:31:17 +00:00
|
|
|
db.init_app(application)
|
2016-01-11 15:07:13 +00:00
|
|
|
ma.init_app(application)
|
2016-02-16 15:25:46 +00:00
|
|
|
init_app(application)
|
2016-01-07 13:28:56 +00:00
|
|
|
logging.init_app(application)
|
2016-02-15 16:01:14 +00:00
|
|
|
twilio_client.init_app(application)
|
2016-02-17 12:57:51 +00:00
|
|
|
firetext_client.init_app(application)
|
2016-02-17 17:48:23 +00:00
|
|
|
aws_ses_client.init_app(application.config['AWS_REGION'])
|
2016-02-16 14:06:56 +00:00
|
|
|
notify_celery.init_app(application)
|
2016-02-16 15:28:30 +00:00
|
|
|
encryption.init_app(application)
|
2016-02-09 13:31:45 +00:00
|
|
|
|
2016-01-14 16:13:27 +00:00
|
|
|
from app.service.rest import service as service_blueprint
|
|
|
|
|
from app.user.rest import user as user_blueprint
|
|
|
|
|
from app.template.rest import template as template_blueprint
|
|
|
|
|
from app.status.healthcheck import status as status_blueprint
|
2016-01-15 15:48:05 +00:00
|
|
|
from app.job.rest import job as job_blueprint
|
2016-01-19 11:23:09 +00:00
|
|
|
from app.notifications.rest import notifications as notifications_blueprint
|
2016-01-15 15:48:05 +00:00
|
|
|
|
2016-01-08 17:51:46 +00:00
|
|
|
application.register_blueprint(service_blueprint, url_prefix='/service')
|
|
|
|
|
application.register_blueprint(user_blueprint, url_prefix='/user')
|
2016-02-22 12:55:18 +00:00
|
|
|
application.register_blueprint(template_blueprint)
|
2016-01-14 16:13:27 +00:00
|
|
|
application.register_blueprint(status_blueprint, url_prefix='/status')
|
2016-01-19 11:23:09 +00:00
|
|
|
application.register_blueprint(notifications_blueprint, url_prefix='/notifications')
|
2016-01-18 09:57:04 +00:00
|
|
|
application.register_blueprint(job_blueprint)
|
2015-12-15 17:14:13 +00:00
|
|
|
|
2015-12-10 10:56:59 +00:00
|
|
|
return application
|
|
|
|
|
|
|
|
|
|
|
2016-02-16 15:25:46 +00:00
|
|
|
def init_app(app):
|
2016-01-14 17:45:30 +00:00
|
|
|
@app.before_request
|
|
|
|
|
def required_authentication():
|
2016-01-19 15:32:33 +00:00
|
|
|
if request.path != url_for('status.show_status'):
|
|
|
|
|
from app.authentication import auth
|
|
|
|
|
error = auth.requires_auth()
|
|
|
|
|
if error:
|
|
|
|
|
return error
|
2016-01-14 17:45:30 +00:00
|
|
|
|
2016-01-20 15:28:39 +00:00
|
|
|
@app.after_request
|
|
|
|
|
def after_request(response):
|
2016-01-20 15:51:13 +00:00
|
|
|
response.headers.add('Access-Control-Allow-Origin', '*')
|
|
|
|
|
response.headers.add('Access-Control-Allow-Headers', 'Content-Type,Authorization')
|
|
|
|
|
response.headers.add('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE')
|
|
|
|
|
return response
|
2016-01-20 15:28:39 +00:00
|
|
|
|
2015-12-10 10:56:59 +00:00
|
|
|
|
2016-01-29 12:51:33 +00:00
|
|
|
def get_api_version():
|
2016-02-01 11:37:22 +00:00
|
|
|
build = 'n/a'
|
|
|
|
|
build_time = "n/a"
|
|
|
|
|
try:
|
|
|
|
|
from app import version
|
|
|
|
|
build = version.__build__
|
|
|
|
|
build_time = version.__time__
|
|
|
|
|
except:
|
|
|
|
|
pass
|
|
|
|
|
return build, build_time
|
2016-01-29 12:51:33 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def get_db_version():
|
|
|
|
|
try:
|
|
|
|
|
query = 'SELECT version_num FROM alembic_version'
|
|
|
|
|
full_name = db.session.execute(query).fetchone()[0]
|
|
|
|
|
return full_name.split('_')[0]
|
|
|
|
|
except:
|
|
|
|
|
return 'n/a'
|
2016-02-22 17:17:29 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def email_safe(string):
|
|
|
|
|
return "".join([
|
|
|
|
|
character.lower() if character.isalnum() or character == "." else ""
|
|
|
|
|
for character in re.sub("\s+", ".", string.strip())
|
|
|
|
|
])
|