__init__.py 2.04 KB
Newer Older
wanli's avatar
wanli committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14
#!/usr/bin/env python
# -*- coding: utf_8 -*-

import logging
import traceback
from flask import Flask, Response, jsonify
from flask_cors import CORS
from werkzeug.exceptions import HTTPException, InternalServerError
from .api import api
from .files import file_api
from .login import login_api
from .user import user_api
from .annex import annex_api
from .apps import apps_api
wanli's avatar
wanli committed
15
from .device import device_api
wanli's avatar
wanli committed
16
from .download import download_api
wanli's avatar
wanli committed
17
from .app_logs import appLogs_api
wanli's avatar
wanli committed
18 19 20 21 22 23 24
from .ws import NotifyHandler, ThreadNotifyHandler
from model import fullStackDB
from fullstack.response import ResponseCode, response_result

from app import config, signalManager
from flask_login import LoginManager

wanli's avatar
wanli committed
25
logger = logging.getLogger(__name__)
wanli's avatar
wanli committed
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47

class JsonResponse(Response):
    @classmethod
    def force_type(cls, response, environ=None):
        if isinstance(response, (list, dict)):
            response = jsonify(response)
        return super(Response, cls).force_type(response, environ)

class FlaskAPP(Flask):
    response_class = JsonResponse

    def unauthorized(self, message):
        return response_result(ResponseCode.AUTHORIZATION_ERROR, msg=message)

def create_app():
    app = FlaskAPP(__name__)
    app.register_blueprint(api)
    app.register_blueprint(login_api)
    app.register_blueprint(file_api)
    app.register_blueprint(user_api)
    app.register_blueprint(annex_api)
    app.register_blueprint(apps_api)
wanli's avatar
wanli committed
48
    app.register_blueprint(download_api)
wanli's avatar
wanli committed
49
    app.register_blueprint(device_api)
wanli's avatar
wanli committed
50
    app.register_blueprint(appLogs_api)
wanli's avatar
wanli committed
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70

    @app.errorhandler(InternalServerError)
    def handle_500(e):
        logger.error(str(e))
        traceback.print_exc()
        return response_result(ResponseCode.SERVER_ERROR, msg=str(e))

    @app.errorhandler(Exception)
    def handle_exception(e):
        logger.error(str(e))
        traceback.print_exc()
        return response_result(ResponseCode.SERVER_ERROR, msg=str(e))

    CORS(app, supports_credentials=True, origins='*')
    app.config.update(config)
    fullStackDB(app, drop_tables=[])

    return app

app = create_app()