Ejemplo n.º 1
0
def init():
    import os
    from base import logger
    from etc import config

    # log setting
    logger.init_log([(n, os.path.join("logs", p), l)
                     for n, p, l in config.log_config])
    logger.AutoLog.log_path = 'logs'

    # pool setting
    smartpool.coroutine_mode = config.pool_coroutine_mode
    if config.debug and getattr(config, "pool_log", None) is not None:
        smartpool.pool_logger = logger.get(config.pool_log).info

    # mysql setting
    if config.debug and getattr(config, "db_query_log", None) is not None:
        smartpool.query_logger = logger.get(config.db_query_log).info

    for name, setting in config.db_config.items():
        smartpool.init_pool(name,
                            setting,
                            smartpool.MySQLdbConnection,
                            *config.db_conn_pool_size,
                            maxidle=config.db_connection_idle,
                            clean_interval=config.db_pool_clean_interval)
Ejemplo n.º 2
0
def decode_from_access_token(encoded):
    d = {}
    if encoded:
        try:
            d = jwt.decode(encoded, config.JWT_SECRET, algorithms=['HS256'])
        except Exception, e:
            logger.get("auth").error(e)
Ejemplo n.º 3
0
def post_content(url,
                 params=None,
                 method=const.METHOD.GET,
                 without_ua=False,
                 **kwargs):
    headers = kwargs.get("headers", {})
    if not without_ua:
        headers.setdefault('User-Agent',
                           random.choice(config.HEADER.USER_AGENT))

    kwargs["headers"] = headers
    kwargs.setdefault('timeout', config.request_timeout)
    try:
        resp = None
        if method == const.METHOD.POST:
            resp = requests.post(url, data=params, **kwargs)
        else:
            resp = requests.get(url, params=params, **kwargs)
        resp_content = resp.content
        return resp_content
    except Exception as e:
        msg = u"%s [%s]:%s" % (method, url if params is None else url +
                               join_params(**params),
                               resp.content if resp else e)
        logger.get("error-log").error(msg)
Ejemplo n.º 4
0
def decode_from_access_token(encoded):
    d = {}
    if encoded:
        try:
            d = jwt.decode(encoded, config.JWT_SECRET, algorithms=['HS256'])
        except Exception, e:
            logger.get("auth").error(e)
Ejemplo n.º 5
0
    def handle_500(error):
        import traceback
        error_str = traceback.format_exc()
        logger.get("cgi-log").error(error_str)

        if config.debug:
            return error

        msg = error_str if config.debug else "系统异常"
        return DjErrorResponse(msg).output(), 200
Ejemplo n.º 6
0
    def handle_500(error):
        import traceback
        error_str = traceback.format_exc()
        logger.get("cgi-log").error(error_str)

        if config.debug:
            return error

        msg = error_str if config.debug else "系统异常"
        return ErrorResponse(msg).output(), 200
Ejemplo n.º 7
0
    def handle_exception(error):
        import traceback
        error_str = traceback.format_exc()
        logger.get("cgi-log").error(error_str)

        if config.debug:
            from flask._compat import reraise
            handler = app.error_handler_spec[None].get(Exception)
            exc_type, exc_value, tb = sys.exc_info()
            reraise(exc_type, exc_value, tb)

        msg = error_str if config.debug else "系统异常"
        return DjErrorResponse(msg).output(), 200
Ejemplo n.º 8
0
    def handle_exception(error):
        import traceback
        error_str = traceback.format_exc()
        logger.get("cgi-log").error(error_str)

        if config.debug:
            from flask._compat import reraise
            handler = app.error_handler_spec[None].get(Exception)
            exc_type, exc_value, tb = sys.exc_info()
            reraise(exc_type, exc_value, tb)

        msg = error_str if config.debug else "系统异常"
        return ErrorResponse(msg).output(), 200
Ejemplo n.º 9
0
def get_html_soup(url, **kwargs):
    headers = kwargs.get("headers", {})
    headers.setdefault('User-Agent', random.choice(config.HEADER.USER_AGENT))
    kwargs["headers"] = headers
    try:
        html = requests.get(url, timeout=config.request_timeout, **kwargs)
        if ('content-type' in html.headers and 'charset' not in html.headers['content-type']) \
                or ('content-type' not in html.headers):
            # html.encoding = config.encoding
            html.encoding = cchardet.detect(html.content)['encoding']
        soup = BeautifulSoup(html.text, "lxml")
        return soup
    except Exception as e:
        msg = u"get [%s] : %s" % (url, e)
        logger.get("error-log").error(msg)
Ejemplo n.º 10
0
 async def middleware_handler(request):
     try:
         return await handler(request)
     except HTTPException as e:
         if e.status == 404:
             return ErrorResponse('Not Found').output()
         elif e.status == 500:
             import traceback
             err_msg = traceback.format_exc()
             logger.get('web-error').error(err_msg)
             return ErrorResponse(err_msg).output()
     except Exception:
         import traceback
         err_msg = traceback.format_exc()
         print(err_msg)
         logger.get('web-error').error(err_msg)
         return ErrorResponse('System Crash').output()
Ejemplo n.º 11
0
        def new_handler(*args, **kwargs):
            if config.IS_VALIDATE_SIGNATURE and validate_sign and not validate_signature():
                abort(403)

            resp = old_handler(*args, **kwargs)
            if isinstance(resp, TempResponse):
                # 添加自定义变量
                resp.context_update(
                    request=request,
                    config=config,
                    const=const,
                    ver=util.get_static_file_version,
                    random=random,
                )

            if isinstance(resp, Response):
                output = resp.output()
                if config.debug:
                    logger.get("response-log").debug(
                        u"[%s] %s" % (util.to_unicode(request.url), util.to_unicode(output.data)))
                return output
            return resp
Ejemplo n.º 12
0
        def new_handler(*args, **kwargs):
            if validate_sign and False:
                resp = ErrorResponse(u"SIGNATURE_NOT_VALID", status=403)
            else:
                resp = old_handler(*args, **kwargs)
            if isinstance(resp, TempResponse):
                # 添加自定义变量
                resp.context_update(
                    request=request,
                    config=config,
                    const=const,
                    ver=util.get_static_file_version,
                    random=random,
                )

            if isinstance(resp, Response):
                output = resp.output()
                if config.debug:
                    logger.get("response-log").debug(
                        u"[%s] %s" % (util.to_unicode(
                            request.url), util.to_unicode(output.data)))
                return output
            return resp
Ejemplo n.º 13
0
def callback_url(callback_id,
                 mode,
                 url,
                 method=const.HTTP_METHOD.GET,
                 body=None,
                 logger=logger.get("cgi-log")):
    u"""回调URL.

    @param<callback_id>: 回调在数据库中的ID(对应的表为callback_url)
    @param<mode>: 回调模式,详见const.CALLBACK_URL.MODE
    @param<url>: 回调的URL
    @param<method>: 回调的HTTP方法
    @param<body>: 如果回调方法为POST, 则为需要POST的HTTP body
    @param<logger>: 用于记日志的对象

    @return: (is_sucess,   # 回调是否成功
              resp_code,   # 回调的HTTP响应状态码
              resp_body,   # 回调的HTTP响应body
             )
    """
    http_method = requests.get

    if method == const.HTTP_METHOD.POST:
        http_method = requests.post

    # resp_code = None
    # resp_body = None

    try:
        if body is None:
            r = http_method(url)
        else:
            r = http_method(url, body)
    except Exception:
        logger.error(
            "[callback request error]: "
            "<callback_id>=><%s>, <url>=><%s>",
            callback_id,
            url,
            exc_info=True)
        is_success = False
    else:
        # resp_code = r.status_code
        # resp_body = r.content
        is_success = callback_url_resp_check(r, mode, callback_id, url, logger)

    return is_success
Ejemplo n.º 14
0
def send_sms(mobile, content):
    if not config.IS_SEND_SMS:
        return True

    # TODO 优化,将发送短信放到队列里面慢慢发
    from py4j.java_gateway import JavaGateway, Py4JNetworkError

    try:
        gateway = JavaGateway(eager_load=True)
        app = gateway.entry_point
        ret = app.sendSms(mobile, content)

        logger.get("sms-log").info("send to %s, data [%s], ret: %s", mobile, content, ret)

        return ret
    except Py4JNetworkError:
        logger.get("interface-log").error("No JVM listening")
    except Exception, e:
        logger.get("interface-log").error(e)
Ejemplo n.º 15
0
def pool_func(funcs, tag, is_log=True):
    with with_func_time(tag, logger.get("error-log").error, is_log=is_log):
        pool = Pool(config.concurrent)
        for func in funcs:
            pool.add(gevent.spawn(func.run))
        pool.join()
Ejemplo n.º 16
0
def init(app):
    # log setting
    logger.init_log([(n, os.path.join("logs", p), l)
                     for n, p, l in config.log_config])

    # pool setting
    smartpool.coroutine_mode = config.pool_coroutine_mode
    if config.debug and getattr(config, "pool_log", None) is not None:
        smartpool.pool_logger = logger.get(config.pool_log).info

    # mysql setting
    if config.debug and getattr(config, "db_query_log", None) is not None:
        poolmysql.query_logger = logger.get(config.db_query_log).info

    for name, setting in config.db_config.iteritems():
        smartpool.init_pool(
            name, setting, poolmysql.MySQLdbConnection, *config.db_conn_pool_size,
            maxidle=config.db_connection_idle, clean_interval=config.db_pool_clean_interval
        )

    app.debug = config.debug
    app.config.update(config.app_config)

    # cache
    cache.init_app(app)

    # # redis session
    # app.session_interface = RedisSessionInterface(redis.StrictRedis(
    #     connection_pool=redis.ConnectionPool()))

    app.session_interface = ItsdangerousSessionInterface()

    # reg filters
    from base import jinja_filter
    for name, func in jinja_filter.mapping.iteritems():
        app.template_filter(name)(func)

    app.jinja_env.globals['url_for'] = url_for

    # reg exception handler
    @app.errorhandler(500)
    def handle_500(error):
        import traceback
        error_str = traceback.format_exc()
        logger.get("cgi-log").error(error_str)

        if config.debug:
            return error

        msg = error_str if config.debug else "系统异常"
        return DjErrorResponse(msg).output(), 200

    @app.errorhandler(Exception)
    def handle_exception(error):
        import traceback
        error_str = traceback.format_exc()
        logger.get("cgi-log").error(error_str)

        if config.debug:
            from flask._compat import reraise
            handler = app.error_handler_spec[None].get(Exception)
            exc_type, exc_value, tb = sys.exc_info()
            reraise(exc_type, exc_value, tb)

        msg = error_str if config.debug else "系统异常"
        return DjErrorResponse(msg).output(), 200

    @app.errorhandler(404)
    def page_not_found(error):
        return DjErrorResponse('Not Found').output(), 404

    @app.errorhandler(405)
    def method_not_allow(error):
        return DjErrorResponse(str(error)).output(), 405

    _paragraph_re = re.compile(r'(?:\r\n|\r|\n){2,}')

    @app.template_filter()
    @evalcontextfilter
    def nl2br(eval_ctx, value):
        result = u'\n\n'.join(u'<p>%s</p>' % p.replace('\n', Markup('<br/>\n'))
                              for p in _paragraph_re.split(escape(value)))
        if eval_ctx.autoescape:
            result = Markup(result)
        return result
Ejemplo n.º 17
0
def init(app):
    # log setting
    logger.init_log([(n, os.path.join("logs", p), l)
                     for n, p, l in config.log_config])

    # pool setting
    smartpool.coroutine_mode = config.pool_coroutine_mode
    if config.debug and getattr(config, "pool_log", None) is not None:
        smartpool.pool_logger = logger.get(config.pool_log).info

    # mysql setting
    if config.debug and getattr(config, "db_query_log", None) is not None:
        poolmysql.query_logger = logger.get(config.db_query_log).info

    for name, setting in config.db_config.iteritems():
        smartpool.init_pool(name,
                            setting,
                            poolmysql.MySQLdbConnection,
                            *config.db_conn_pool_size,
                            maxidle=config.db_connection_idle,
                            clean_interval=config.db_pool_clean_interval)

    app.debug = config.debug
    app.config.update(config.app_config)

    # cache
    cache.init_app(app)

    # # redis session
    # app.session_interface = RedisSessionInterface(redis.StrictRedis(
    #     connection_pool=redis.ConnectionPool()))

    app.session_interface = ItsdangerousSessionInterface()

    # reg filters
    from base import jinja_filter
    for name, func in jinja_filter.mapping.iteritems():
        app.template_filter(name)(func)

    app.jinja_env.globals['url_for'] = url_for

    # reg exception handler
    @app.errorhandler(500)
    def handle_500(error):
        import traceback
        error_str = traceback.format_exc()
        logger.get("cgi-log").error(error_str)

        if config.debug:
            return error

        msg = error_str if config.debug else "系统异常"
        return ErrorResponse(msg).output(), 200

    @app.errorhandler(Exception)
    def handle_exception(error):
        import traceback
        error_str = traceback.format_exc()
        logger.get("cgi-log").error(error_str)

        if config.debug:
            from flask._compat import reraise
            handler = app.error_handler_spec[None].get(Exception)
            exc_type, exc_value, tb = sys.exc_info()
            reraise(exc_type, exc_value, tb)

        msg = error_str if config.debug else "系统异常"
        return ErrorResponse(msg).output(), 200

    @app.errorhandler(404)
    def page_not_found(error):
        return ErrorResponse('Not Found').output(), 404

    @app.errorhandler(405)
    def method_not_allow(error):
        return ErrorResponse(str(error)).output(), 405

    _paragraph_re = re.compile(r'(?:\r\n|\r|\n){2,}')

    @app.template_filter()
    @evalcontextfilter
    def nl2br(eval_ctx, value):
        result = u'\n\n'.join(u'<p>%s</p>' % p.replace('\n', Markup('<br/>\n'))
                              for p in _paragraph_re.split(escape(value)))
        if eval_ctx.autoescape:
            result = Markup(result)
        return result