66 lines
1.6 KiB
Python
66 lines
1.6 KiB
Python
|
# coding: utf-8
|
||
|
|
||
|
from flask import Flask, g, jsonify
|
||
|
from SWSCloudAPI.API import api
|
||
|
from SWSCloudCore import models
|
||
|
from SWSCloudCore.models import database
|
||
|
from SWSCloudCore.config import config
|
||
|
|
||
|
app = Flask(__name__)
|
||
|
# app.config['SERVER_NAME'] = settings.get('Application', 'SERVER_NAME')
|
||
|
app.config['DEBUG'] = config.getboolean('Application', 'DEBUG')
|
||
|
app.config['SECRET_KEY'] = config.get("Application", "SECRET_KEY")
|
||
|
|
||
|
app.register_blueprint(api)
|
||
|
|
||
|
|
||
|
@app.errorhandler(404)
|
||
|
def page_not_found(e):
|
||
|
app.logger.error(e)
|
||
|
return jsonify(dict(message='resource not found')), 404
|
||
|
|
||
|
|
||
|
@app.errorhandler(403)
|
||
|
def access_deny(e):
|
||
|
app.logger.error(e)
|
||
|
return jsonify(dict(message='access deny')), 403
|
||
|
|
||
|
|
||
|
@app.errorhandler(410)
|
||
|
def page_not_found(e):
|
||
|
app.logger.error(e)
|
||
|
return jsonify(dict()), 410
|
||
|
|
||
|
|
||
|
@app.errorhandler(500)
|
||
|
def page_not_found(e):
|
||
|
app.logger.error(e)
|
||
|
return jsonify(dict(message='maintenance')), 500
|
||
|
|
||
|
|
||
|
@app.before_request
|
||
|
def before_request():
|
||
|
g.settings = dict()
|
||
|
# извлекаем настройки и определяем их в глобальную переменную
|
||
|
for setting in models.Settings.select(models.Settings.key, models.Settings.val).execute():
|
||
|
g.settings[setting.key] = setting.val
|
||
|
|
||
|
|
||
|
@app.before_first_request
|
||
|
def before_first_request():
|
||
|
try:
|
||
|
database.connect()
|
||
|
except Exception as e:
|
||
|
app.logger.error(e)
|
||
|
return jsonify({}), 500
|
||
|
|
||
|
|
||
|
@app.after_request
|
||
|
def after_request(response):
|
||
|
# TODO: report about access
|
||
|
return response
|
||
|
|
||
|
|
||
|
if __name__ == '__main__':
|
||
|
app.run(host='0.0.0.0', port=5001, debug=True)
|