コード例 #1
0
ファイル: application.py プロジェクト: st0ne-c0ld/book_logs
def create_application() -> Flask:
    logging.config.dictConfig(settings.create_logging_setting())

    app = Flask(__name__)
    init_sentry()
    init_db(app, db.meta)
    init_jaeger(app)
    RequestID(app)

    @app.route('/books', methods=['GET'])
    def book():
        logger.info('got_request', extra={'endpoint': '/books'})
        book_ids = request.args.getlist('book_ids', int)
        books = model.Book.query.filter(model.Book.id.in_(book_ids)).all()

        result = {}
        for b in books:
            result[b.id] = {
                'title': b.title,
                'author_name': b.author.name,
            }

        return jsonify({'result': result})

    @app.route('/ping', methods=['GET'])
    def ping():
        return 'pong'

    return app
コード例 #2
0
def get_logger(app,
               logfile_name="app",
               log_path="/tmp/sport-data-news/",
               level=logging.DEBUG):
    '''save log to diffrent file by deffirent log level into the log path
    and print all log in console'''
    RequestID(app)
    logging.setLoggerClass(AppLogger)
    formatter = logging.Formatter(
        '%(asctime)s, %(levelname)s, %(filename)s:%(lineno)d, request_id=%(request_id)s, %(message)s',
        '%Y-%m-%d %H:%M:%S')

    log_files = {
        logging.DEBUG: os.path.join(log_path, logfile_name + '-debug.log'),
        logging.INFO: os.path.join(log_path, logfile_name + '-info.log'),
        logging.WARNING: os.path.join(log_path, logfile_name + '-warning.log'),
        logging.ERROR: os.path.join(log_path, logfile_name + '-error.log'),
        logging.CRITICAL: os.path.join(log_path,
                                       logfile_name + '-critical.log')
    }

    logger = logging.getLogger('')
    logger.setLevel(logging.DEBUG)
    for log_level, log_file in log_files.items():
        if log_level < level:
            continue
        file_handler = logging.handlers.TimedRotatingFileHandler(
            log_file, 'midnight')
        file_handler.setLevel(log_level)
        file_handler.setFormatter(formatter)
        file_handler.addFilter(RequestIDLogFilter())
        logger.addHandler(file_handler)

    return logger
コード例 #3
0
def create_app():
    FLASK_CONFIG = os.getenv('FLASK_CONFIG') or 'dev'
    print(f'Loading {FLASK_CONFIG} configurations')

    app = Flask(__name__)
    RequestID(app,
              request_id_generator=lambda: f'simulation_rid{uuid.uuid4().hex}')

    app.secret_key = "l\x9b\x0cb\x86\x96Z/-\x88Ry\x03y\xea\x9c"

    app.config.from_object(config_by_name[FLASK_CONFIG])

    app.config.SWAGGER_UI_DOC_EXPANSION = 'list'
    app.config.SWAGGER_UI_OPERATION_ID = True
    app.config.SWAGGER_UI_REQUEST_DURATION = True

    app.wsgi_app = ProxyFix(app.wsgi_app)

    # register blueprints
    register_blueprints(app)

    configure_logging(app)

    # enable cors
    CORS(app)

    return app
コード例 #4
0
def configure_app(app,
                  filename=DEFAULT_GLOBAL_CONFIG_FILE,
                  config_class=DEFAULT_CONFIG_CLASS):
    """
    Configure a Flask app
    :param app:
    :param filename:
    :param config_class:
    :return:
    """
    # Select config file
    if not os.path.isfile(filename):
        filename = DEFAULT_APPLICATION_CONFIG_FILE

    # Load proper config for app
    config = load_module(filename)
    app.config.from_object(getattr(config, config_class))
    app.config.from_pyfile('./config_local.py', silent=True)

    # Correct LOG_FOLDER config
    app.config['LOG_FOLDER'] = os.path.join(
        os.path.abspath(os.path.dirname(os.path.dirname(__file__))), 'logs')
    config.make_dir(app.config['LOG_FOLDER'])

    # Config Flask components
    RequestID(app)
    configure_route(app)
    configure_extensions(app)
    configure_blueprints(app)
    configure_log_handlers(app)
    configure_error_handlers(app)
    configure_after_request(app)
コード例 #5
0
def create_app():
    """
    flask app creation method
    """
    environment = os.getenv('ENV', 'dev')

    flask_app = Flask(__name__)
    RequestID(flask_app, request_id_generator=lambda: 'event_rid%s' % uuid.uuid4().hex)
    flask_app.config.from_object(Config)

    flask_app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
    flask_app.config['SQLALCHEMY_DATABASE_URI'] = (
        settings.POSTGRES_URI
    )

    if environment.upper() == 'LOCAL':
        flask_app.config['SQLALCHEMY_ECHO'] = True

    # register extensions
    register_extensions(flask_app)

    # register blueprints
    register_blueprints(flask_app)

    # configure logging
    configure_logging(flask_app)

    # enable cors
    CORS(flask_app)

    return flask_app
コード例 #6
0
def create_app():
    """Initialize Flask Application"""
    app = Flask(__name__)
    configure_app(app)
    configure_logging(app)
    RequestID(app)
    configure_hook(app)
    return app
コード例 #7
0
ファイル: extensions.py プロジェクト: j178/flask-scaffold
def init_extentions(app):
    global cache, redis

    cache = Cache(app)
    redis = Redis.from_url(app.config["CACHE_REDIS_URL"],
                           decode_responses=True)

    RequestID(app, request_id_parser=x_request_id)
コード例 #8
0
ファイル: app.py プロジェクト: areganesh94/cloud-mangment
def setup(app, **kwargs):
    global db
    db.init_app(app)
    RequestID(app)
    init_app_config(app, **kwargs)
    registered_blueprint = RegisterBlueprint(app.config['APPS'])
    for blueprint in registered_blueprint.blueprint:
        app.register_blueprint(blueprint)
    return app
コード例 #9
0
def create_app(config_filename=None):
    app = Flask(__name__,
                static_folder='../static',
                template_folder='../templates')
    app.wsgi_app = SaferProxyFix(app.wsgi_app)
    app.config.from_object(
        'XNBackend.app.default_settings.DefaultConfiguration')
    app.config.from_envvar('XN_SETTINGS')
    if config_filename:
        app.config.from_pyfile(config_filename)

    RequestID(app)
    init_logger(app)
    celery.init_app(app)

    init_cache(app)

    # flask compress
    Compress(app)

    db.init_app(app)
    JWTManager(app)
    bcrypt.init_app(app)

    add_filters(app)

    app.register_blueprint(api_bp)
    app.register_blueprint(dashboard_api_bp)

    restless_manager = flask_restless.APIManager(app, flask_sqlalchemy_db=db)
    JWTManager(app)
    app.cli.add_command(user_cli)
    app.cli.add_command(systemd)

    # flask_restless part
    restless_manager.create_api(LuxSensors, methods=["GET"])
    restless_manager.create_api(HeatMapSnapshots, methods=["GET"])
    restless_manager.create_api(Users, methods=["GET"])
    restless_manager.create_api(AppearRecords,
                                methods=["GET"],
                                preprocessors=preprocessors)
    restless_manager.create_api(LatestPosition, methods=["GET"])
    restless_manager.create_api(TrackingDevices, methods=["GET"])
    restless_manager.create_api(EnegyConsumeMonthly, methods=["GET"])
    restless_manager.create_api(EnergyConsumeDaily, methods=["GET"])
    restless_manager.create_api(IRSensors, methods=["GET"])
    restless_manager.create_api(AQISensors, methods=["GET"])
    restless_manager.create_api(LuxSensors, methods=["GET"])
    restless_manager.create_api(FireAlarmSensors, methods=["GET"])
    restless_manager.create_api(Switches, methods=["GET"])
    restless_manager.create_api(Elevators, methods=["GET"])
    restless_manager.create_api(S3FC20, methods=["GET"])
    restless_manager.create_api(Relay, methods=["GET"])

    return app
コード例 #10
0
def setup_app(app: flask.Flask) -> None:
    """
    Setups the main flask application.
    """
    app.register_error_handler(405, handler_405)
    app.register_error_handler(404, handler_404)
    app.register_error_handler(Exception, general_exception_handler)
    app.after_request(after_all_response)

    # no json keys sorting for the responses
    app.config["JSON_SORT_KEYS"] = False

    # sets request id for the flask app logging
    RequestID(app)

    # logging configuration of the app
    config_log()
コード例 #11
0
ファイル: application.py プロジェクト: Qbin/flask-demo-celery
def create_app_by_config(conf=None, **kwargs):
    # initialize logger
    register_logger()
    # check instance path
    instance_path = os.environ.get('INSTANCE_PATH') or None
    # create and configure the app
    app = Flask("FLASK", instance_path=instance_path)
    if not conf:
        conf = get_config_object()
    app.config.from_object(conf)
    # ensure the instance folder exists
    if app.instance_path:
        try:
            os.makedirs(app.instance_path)
        except OSError:
            pass
    with app.app_context():
        # register app
        register_app(app)
        # create_mysql(app)
        create_mongo(app)
        create_redis(app)
        RequestID(app)
        app.response_class = ProphetResponse
        CORS(app, supports_credentials=True)
        if kwargs.get('celery'):
            init_celery(kwargs['celery'], app)
        # app.__call__()
        # from app.user.user_error import UserError
        # @app.before_request
        # def before_request():
        #     if "login" not in request.url:
        #         try:
        #             token = request.headers['Authorization']
        #             s = Serializer(current_app.config["SECRET_KEY"])
        #             params = s.loads(token)
        #             g.user_id = params.get("user_id")
        #             g.qid = params.get("qid")
        #         except Exception:
        #             raise UserError(UserError.USER_NOT_LOGIN, "user not login")
        #
        # @app.errorhandler(UserError)
        # def handler_error(e):
        #     return UserError(UserError.USER_NOT_LOGIN, "user not login")
    return app
コード例 #12
0
ファイル: __init__.py プロジェクト: kuldeeprishi/nfl-rushing
def create_app(config_name=None):
    app = Flask(__name__)

    env_name = os.getenv("ENV")
    if not env_name and config_name is None:
        config_name = "local"
    elif config_name is None:
        config_name = env_name
    else:
        if env_name:
            assert env_name == config_name, (
                'ENV environment variable ("%s") and config_name argument '
                '("%s") are both set and are not the same.'
                % (env_name, config_name)
            )

    print(f"Loading {env_name} configuration")

    try:
        app.config.from_object(config_by_name[config_name])
    except ImportError:
        if config_name == "local":
            app.logger.error(  # pylint: disable=no-member
                "You have to have `local` defined in the configuration in order to use "
                "the default 'local' Flask Config. Alternatively, you may set `ENV` "
                "environment variable to one of the following options: dev, qa, test, prod"
            )
            sys.exit(1)
        raise


    RequestID(app, request_id_generator=lambda: f"NFL{uuid.uuid4().hex}")

    from . import extensions
    extensions.init_app(app)

    from . import modules
    modules.init_app(app)

    return app
コード例 #13
0
def create_application() -> Flask:
    logging.config.dictConfig(settings.create_logging_setting())

    app = Flask(__name__)
    init_sentry()
    init_db(app, db.meta)
    init_jaeger(app)
    RequestID(app)

    @app.route('/ping', methods=['GET'])
    def ping():
        return 'pong'

    @app.route('/user/<int:user_id>/orders', methods=['GET'])
    def get_order(user_id):
        logger.info('got_request',
                    extra={'endpoint': '/user/<int:user_id>/orders'})
        orders = model.Order.query.filter_by(user_id=user_id).all()
        orders = marshall_order(orders)

        return jsonify({'result': {'orders': orders}})

    return app
コード例 #14
0
def create_application() -> Flask:
    logging.config.dictConfig(settings.create_logging_setting())

    app = Flask(__name__)
    init_sentry()
    init_jaeger(app)
    RequestID(app)

    @app.route('/user/<int:user_id>/orders', methods=['GET'])
    def get_orders(user_id):
        logger.info('got_request',
                    extra={'endpoint': '/user/<user_id>/orders'})

        headers = {'X-Request-ID': current_request_id()}

        response = requests.get(ORDERS_URL.format(user_id=user_id),
                                headers=headers)
        orders = response.json()['result']['orders']

        book_ids = get_book_ids_from_orders(orders)
        response = requests.get(BOOKS_URL,
                                params={'book_ids': book_ids},
                                headers=headers)
        books = response.json()['result']

        return jsonify({'result': {
            'orders': orders,
            'books': books,
        }})

    @app.route('/ping', methods=['GET'])
    def ping():
        logger.info('got_request', extra={'endpoint': '/ping'})
        return 'pong'

    return app
コード例 #15
0
ファイル: main.py プロジェクト: gfffbjtu/DockerManager
"""启动配置类"""
from flask import Flask
import logging
from flask_log_request_id import RequestID, RequestIDLogFilter
from logging.handlers import TimedRotatingFileHandler
from api.project_api import project_blue
from api.docker_api import docker_blue
from api.image_api import image_blue
from db.base_model import db

app = Flask(__name__)

RequestID(app)
LOG_PATH = 'log/app.log'
HANDLER = TimedRotatingFileHandler(LOG_PATH,
                                   when="D",
                                   interval=1,
                                   backupCount=30)
FORMATTER = logging.Formatter(
    "[%(asctime)s][%(request_id)s] [%(pathname)s:%(lineno)d] %(levelname)s - %(message)s"
)
HANDLER.setFormatter(FORMATTER)
HANDLER.addFilter(RequestIDLogFilter())
app.logger.addHandler(HANDLER)
app.logger.setLevel(logging.DEBUG)

# url的格式为:数据库的协议://用户名:密码@ip地址:端口号(默认可以不写)/数据库名
app.config[
    "SQLALCHEMY_DATABASE_URI"] = "mysql://*****:*****@127.0.0.1/dockermanager"
# app.config["SQLALCHEMY_DATABASE_URI"] = "mysql://*****:*****@127.0.0.1/dockermanager"
# 动态追踪数据库的修改. 性能不好. 且未来版本中会移除. 目前只是为了解决控制台的提示才写的
コード例 #16
0
def bootstrap_app(debug=False, multiclient=False):
    app = Flask(__name__)
    app.debug = debug
    app.multiclient = multiclient

    # Indexing some session data by the USER_HASH will help to avoid maintaining
    # sessions between two different databases. This could lead to some errors in
    # the POS in which the user making the sale does not exist.
    app.config['SECRET_KEY'] = get_user_hash()
    app.config['PROPAGATE_EXCEPTIONS'] = True
    app.config['RESTFUL_JSON'] = {
        'cls': JsonEncoder,
    }
    flask_api = Api(app)
    if RequestID:
        RequestID(app)

    register_routes(flask_api)

    signal('StoqTouchStartupEvent').send()

    @app.errorhandler(Exception)
    def unhandled_exception(e):
        traceback_info = "\n".join(traceback.format_tb(e.__traceback__))
        traceback_hash = hashlib.sha1(
            traceback_info.encode('utf-8')).hexdigest()[:8]
        traceback_exception = traceback.format_exception_only(type(e), e)[-1]
        timestamp = localnow().strftime('%Y%m%d-%H%M%S')

        logger.exception(
            'Unhandled Exception: {timestamp} {error} {traceback_hash}'.format(
                timestamp=timestamp, error=e, traceback_hash=traceback_hash))

        sentry_report(type(e),
                      e,
                      e.__traceback__,
                      traceback_hash=traceback_hash)

        return Response(json.dumps({
            'error': _('bad request!'),
            'timestamp': timestamp,
            'exception': traceback_exception,
            'traceback_hash': traceback_hash
        }),
                        500,
                        mimetype='application/json')

    if not is_developer_mode():
        sentry.raven_client = Sentry(app, dsn=SENTRY_URL, client=raven_client)

    @app.after_request
    def after_request(response):
        # Add all the CORS headers the POS needs to have its ajax requests
        # accepted by the browser
        origin = request.headers.get('origin')
        if not origin:
            origin = request.args.get('origin',
                                      request.form.get('origin', '*'))
        response.headers['Access-Control-Allow-Origin'] = origin
        response.headers[
            'Access-Control-Allow-Methods'] = 'POST, GET, OPTIONS, DELETE'
        response.headers[
            'Access-Control-Allow-Headers'] = 'Authorization, Content-Type'
        response.headers['Access-Control-Allow-Credentials'] = 'true'
        return response

    return app
コード例 #17
0
def initialise_flask(name):
    app = Flask(name.split('.')[0])
    RequestID(app)
    return app
コード例 #18
0
ファイル: application.py プロジェクト: Creditas/flask-toolkit
def create_application(name='app-python',
                       config=dict(),
                       db=None,
                       config_logging=dict()):
    app = Flask(name)

    app.config.update(config)

    @app.after_request
    def append_request_id(response):
        response.headers.add('X-REQUEST-ID', current_request_id())

        data = request.get_data()
        try:
            data = data.decode('utf-8')
        except Exception as e:
            pass

        user_agent = request.headers.get('User-Agent', None)
        remote_addr = request.environ.get('HTTP_X_REAL_IP',
                                          request.remote_addr)

        telemetry(method=request.method,
                  path=request.path,
                  args=request.args.to_dict(),
                  status=response.status,
                  ip=remote_addr,
                  user_agent=user_agent,
                  payload=data)

        return response

    @app.errorhandler(404)
    def default_page_not_found(error):
        return 'This page does not exist', 404

    @app.errorhandler(ObjectDoesNotExistException)
    def page_not_found(error):
        return 'This page does not exist', 404

    @app.errorhandler(ObjectAlreadyExistException)
    def conflict(error):
        return 'This object already exists', 409

    @app.errorhandler(ForbiddenException)
    def forbidden(error):
        app.logger.error(error)
        return 'This request is forbidden', 403

    @app.errorhandler(BadRequestException)
    def bad_request(error):
        app.logger.error(error)
        return 'Bad request', 400

    @app.errorhandler(InvalidDomainConditions)
    def invalid_domain_conditions(error):
        return jsonify({'errors': [str(error)]}), 422

    CORS(app)
    RequestID(app)

    if config_logging:
        setup_logging(app, config_logging)

    if db:
        Migrate(app, db)
        db.init_app(app)
        Storage().setup_instance(db)

    return app
コード例 #19
0
ファイル: __init__.py プロジェクト: fakegit/eclogue
def create_app(schedule=True):
    dictConfig(config.logging)
    # root_path = os.path.join(config.home_path, 'public')
    root_path = config.home_path

    instance = Flask(__name__,
                     root_path=root_path,
                     static_folder='public',
                     static_url_path='')
    instance.json_encoder = JSONEncoder
    instance.config.from_object(config)
    instance.config['LOG_REQUEST_ID_LOG_ALL_REQUESTS'] = True
    RequestID(app=instance)
    Middleware(instance)
    bp = router_v1(routes)
    instance.register_blueprint(bp)
    # instance.register_blueprint(static())
    if schedule:
        scheduler.start()
    # api.add_resource(Menus, '/menus')

    @instance.route('/', methods=['get'])
    def index():
        return instance.send_static_file('index.html')

    @instance.errorhandler(404)
    def not_found(error):
        return jsonify({
            'message': 'not found',
            'code': 404,
            'error': str(error)
        }), 404

    @instance.errorhandler(500)
    def server_error(error):

        return jsonify({
            'message': 'server error',
            'code': 500,
            'error': str(error)
        }), 500

    @instance.errorhandler(405)
    def metho_not_allow(error):
        return jsonify({
            'message': 'method not allow',
            'code': 405,
            'error': str(error)
        }), 405

    @instance.errorhandler(HTTPException)
    def handle_exception(e):
        """Return JSON instead of HTML for HTTP errors."""
        # start with the correct headers and status code from the error
        # replace the body with JSON
        log_info = {
            "code": e.code,
            "title": e.name,
            "description": e.description,
        }
        get_logger().error('api server error %s' % e.description,
                           extra=log_info)

        return jsonify({'code': 500, 'message': 'api server error'})

    return instance
コード例 #20
0
 def __init__(self, app):
     RequestID(app)
     create_logger(log=app.logger,
                   config=app.config,
                   handler_filter=RequestIDLogFilter())
     AppLogger(app)
コード例 #21
0
ファイル: app.py プロジェクト: ivica-k/request_id_example
import os

from flask import Flask

from flask_log_request_id import RequestID, RequestIDLogFilter
import logging

req_id = RequestID()


def setup_blueprints(app):
    from blueprints import simple_blueprint

    app.register_blueprint(simple_blueprint)


def setup_logging():
    handler = logging.StreamHandler()
    logging.getLogger("werkzeug").handlers = []
    handler.setFormatter(
        logging.Formatter(
            "%(asctime)s - %(name)s - %(levelname)s - request_id=%(request_id)s - %(message)s"
        ))
    handler.addFilter(RequestIDLogFilter())
    logging.getLogger().addHandler(handler)


def create_app():
    app = Flask(__name__)

    setup_blueprints(app)
コード例 #22
0
ファイル: __init__.py プロジェクト: ichar/webperso.python
def create_app(config_name):
    app = Flask(__name__)
    app.config.from_object(config[config_name])
    config[config_name].init_app(app)

    app.register_error_handler(403, forbidden)

    bootstrap.init_app(app)
    babel.init_app(app)
    mail.init_app(app)
    #moment.init_app(app)
    db.init_app(app)
    login_manager.init_app(app)
    #pagedown.init_app(app)

    #if not app.debug and not app.testing and not app.config['SSL_DISABLE']:
    #    from flask.ext.sslify import SSLify
    #    sslify = SSLify(app)

    RequestID(app)

    from .bankperso import bankperso as bankperso_blueprint
    app.register_blueprint(bankperso_blueprint)

    from .cards import cards as cards_blueprint
    app.register_blueprint(cards_blueprint)

    from .configurator import configurator as configurator_blueprint
    app.register_blueprint(configurator_blueprint)

    from .persostation import persostation as persostation_blueprint
    app.register_blueprint(persostation_blueprint)

    from .calculator import calculator as calculator_blueprint
    app.register_blueprint(calculator_blueprint)

    from .diamond import diamond as diamond_blueprint
    app.register_blueprint(diamond_blueprint)

    from .provision import provision as provision_blueprint
    app.register_blueprint(provision_blueprint)

    """
    from .stock import stock as stock_blueprint
    app.register_blueprint(stock_blueprint)

    from .preload import preload as preload_blueprint
    app.register_blueprint(preload_blueprint)

    from .orderstate import orderstate as orderstate_blueprint
    app.register_blueprint(orderstate_blueprint)
    """
    from .auth import auth as auth_blueprint
    app.register_blueprint(auth_blueprint, url_prefix='/auth')

    from .admin import admin as admin_blueprint
    app.register_blueprint(admin_blueprint, url_prefix='/admin')

    from .semaphore import semaphore as semaphore_blueprint
    app.register_blueprint(semaphore_blueprint, url_prefix='/semaphore')

    #from .api import api as api_blueprint
    #app.register_blueprint(api_blueprint, url_prefix='/api')

    return app
コード例 #23
0
ファイル: init.py プロジェクト: kinecosystem/migration-server

def req_id_generator() -> str:
    """
    Generate a unique request id, used when the request id cannot be fetched from the elb headers
    """
    # 8 chars long should be long enough, add the 'Generated' prefix to know not to search for this id in the elb logs
    return f'Generated-{str(uuid.uuid4())[:8]}'


# Setup app
app = flask.Flask(__name__)
# Allow CORS
CORS(app)
# Inject request id to a request's context
RequestID(app, request_id_parser=amazon_elb_trace_id, request_id_generator=req_id_generator)

# Setup logging
handler = logging.StreamHandler()
handler.setFormatter(logging.Formatter('%(asctime)s | level=%(levelname)s | request_id=%(request_id)s | %(message)s'))
handler.addFilter(RequestIDLogFilter())
logging.root.addHandler(handler)
logger = logging.getLogger('migration')
logger.setLevel('INFO')

kin_logger = logging.getLogger('kin')
kin_logger.setLevel('ERROR')

# Setup DogStatsd
statsd = DogStatsd(host=config.STATSD_HOST, port=config.STATSD_PORT, namespace='migration')