87 lines
2.6 KiB
Python
87 lines
2.6 KiB
Python
# coding: utf-8
|
|
|
|
from flask import Flask, g
|
|
from flask_babel import Babel
|
|
from flaskext.markdown import Markdown
|
|
|
|
from SWSCloudCore import models
|
|
from SWSCloudCore.config import config
|
|
from SWSCloudCore.models import database
|
|
from SWSCloudAdministrator.Administrator import viewAdministrator
|
|
from SWSCloudAdministrator.Administrator.users import view_administrator_users
|
|
from SWSCloudAdministrator.Administrator.tasks import view_administrator_tasks
|
|
from SWSCloudAdministrator.Administrator.compute import view_administrator_compute_vms
|
|
from SWSCloudAdministrator.Administrator.compute import view_administrator_compute_containers
|
|
|
|
app = Flask(__name__, static_folder='static', static_url_path='')
|
|
# 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")
|
|
Markdown(app)
|
|
babel = Babel(app)
|
|
|
|
app.register_blueprint(viewAdministrator)
|
|
app.register_blueprint(view_administrator_tasks)
|
|
app.register_blueprint(view_administrator_users)
|
|
app.register_blueprint(view_administrator_compute_vms)
|
|
app.register_blueprint(view_administrator_compute_containers)
|
|
|
|
# @app.errorhandler(404)
|
|
# def page_not_found(e):
|
|
# return render_template('errors/404.html'), 404
|
|
|
|
|
|
# @app.errorhandler(403)
|
|
# def page_not_found(e):
|
|
# return render_template('errors/403.html'), 403
|
|
|
|
|
|
# @app.errorhandler(410)
|
|
# def page_not_found(e):
|
|
# return render_template('errors/410.html'), 410
|
|
|
|
|
|
# @app.errorhandler(500)
|
|
# def page_not_found(e):
|
|
# print(e)
|
|
# return render_template('errors/500.html'), 500
|
|
|
|
|
|
@app.before_request
|
|
def before_request():
|
|
g.settings = dict()
|
|
# извлекаем настройки и определяем их в глобальную переменную
|
|
settings = models.Settings.select(
|
|
models.Settings.key,
|
|
models.Settings.val
|
|
).execute()
|
|
for setting in settings:
|
|
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)
|
|
# TODO: code to email alert
|
|
# g.endpoint = request.endpoint.replace('.', '/')
|
|
# return render_template('errors/500.html'), 500
|
|
return 'maintenance', 500
|
|
|
|
|
|
@app.after_request
|
|
def after_request(response):
|
|
# app.logger.debug("db.close")
|
|
# try:
|
|
# database.close()
|
|
# except Exception as e:
|
|
# app.logger.error(e)
|
|
# TODO: code to email alert
|
|
# pass
|
|
return response
|
|
|
|
|
|
if __name__ == '__main__':
|
|
app.run(host='0.0.0.0', port=5000, debug=True)
|