2015-12-10 10:56:59 +00:00
|
|
|
import os
|
2016-01-29 12:51:33 +00:00
|
|
|
import re
|
|
|
|
|
import ast
|
2015-12-10 10:56:59 +00:00
|
|
|
|
2016-01-19 15:32:33 +00:00
|
|
|
from flask import request, url_for
|
2015-12-10 10:56:59 +00:00
|
|
|
from flask._compat import string_types
|
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
|
|
|
|
|
|
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-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-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-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-01-13 11:04:13 +00:00
|
|
|
application.register_blueprint(template_blueprint, url_prefix="/template")
|
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'
|