85 lines
2.3 KiB
Python
85 lines
2.3 KiB
Python
# coding: utf-8
|
|
|
|
import redis
|
|
from flask import Flask, g, jsonify
|
|
|
|
from SWSCloudCore import models
|
|
from SWSCloudCore.config import config
|
|
from SWSCloudCore.models import database
|
|
|
|
# v1
|
|
from SWSCloudAPI.API.compute.v1.views import *
|
|
# v2
|
|
from SWSCloudAPI.API.compute.v2.views import *
|
|
|
|
app = Flask(__name__)
|
|
# Думал, что получится сделать автопрефик для всего приложения, но это не сработало
|
|
# app.config["APPLICATION_ROOT"] = "/api/v1"
|
|
# 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")
|
|
|
|
# V1
|
|
app.register_blueprint(api_v1_vms)
|
|
app.register_blueprint(api_v1_containers)
|
|
app.register_blueprint(api_v1_pricing)
|
|
app.register_blueprint(api_v1_datacenters)
|
|
|
|
# V2
|
|
app.register_blueprint(api_v2_token)
|
|
app.register_blueprint(api_v2_pricing)
|
|
app.register_blueprint(api_v2_datacenters)
|
|
app.register_blueprint(api_v2_vms)
|
|
|
|
|
|
@app.errorhandler(404)
|
|
def page_not_found(e):
|
|
app.logger.error(e)
|
|
return jsonify(status='error', message='resource not found')
|
|
|
|
|
|
@app.errorhandler(403)
|
|
def access_deny(e):
|
|
app.logger.error(e)
|
|
return jsonify(status='error', message='access deny'), 403
|
|
|
|
|
|
@app.errorhandler(410)
|
|
def page_not_found(e):
|
|
app.logger.error(e)
|
|
return jsonify({})
|
|
|
|
|
|
@app.errorhandler(500)
|
|
def page_not_found(e):
|
|
app.logger.error(e)
|
|
return jsonify(status='maintenance')
|
|
|
|
|
|
@app.before_request
|
|
def before_request():
|
|
g.tokens = redis.StrictRedis(config.get('Redis', 'host'), config.get('Redis', 'port'), config.get('Redis', 'db'))
|
|
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
|
|
app.logger.info('')
|
|
return response
|
|
|
|
|
|
if __name__ == '__main__':
|
|
app.run(host='0.0.0.0', port=5001, debug=True)
|