Esempio n. 1
0
    def inject_json_data(request):
        """
        内部content json
        """

        logger.debug('endpoint: %s, box: %s', request.endpoint, request.box)

        # 先赋值None
        request.json_data = None

        json_body = request.box.get_json()
        if not json_body:
            return

        data = json_body.get('data')
        sign = json_body.get('sign')

        calc_sign = hashlib.md5('|'.join(
            [current_app.config['SECRET_KEY'], data])).hexdigest()

        if calc_sign != sign:
            logger.error('sign not equal. sign: %s, calc_sign: %s', sign,
                         calc_sign)
            return

        request.json_data = json.loads(data)
Esempio n. 2
0
def heartbeat(request):
    """
    心跳
    :param request:
    :return:
    """
    logger.debug("uid: %s, userdata: %s", request.gw_box.uid, request.gw_box.userdata)

    if current_app.config["REDIS_ONLINE_SAVE"]:
        # 有效
        try:
            save_redis_online(request.gw_box.uid, request.gw_box.userdata)
        except:
            logger.error("exc occur. request: %s", request, exc_info=True)

    request.write_to_client(dict(ret=0))
Esempio n. 3
0
def alloc_server():
    """
    # 方便测试
    if not request.json_data:
        return jsonify(
            ret=proto.RET_INVALID_PARAMS
        )
    """

    appinfo = get_appinfo_by_appkey(request.json_data['appkey'])
    logger.debug("appinfo: %s", appinfo)

    if appinfo is None:
        # 报错
        jsonify(
            ret=proto.RET_INVALID_PARAMS
        )
        return

    user = create_or_update_user(dict(
        appid=appinfo['appid'],
        channel=request.json_data['channel'],
        device_id=request.json_data['device_id'],
        device_name=request.json_data.get('device_name'),
        os=request.json_data.get('os'),
        os_version=request.json_data.get('os_version'),
        sdk_version=request.json_data.get('sdk_version'),
    ))

    server_list = current_app.config['SERVER_LIST']

    # 取模
    server = server_list[user['uid'] % len(server_list)]

    return current_app.response_class(pack_data(
        dict(
            ret=0,
            user=dict(
                uid=user['uid'],
                key=user['key'],
            ),
            server=dict(
                host=server['outer_host'],
                port=server['outer_port'],
            )
        )
    ), mimetype='application/json')
Esempio n. 4
0
def heartbeat(request):
    """
    心跳
    :param request:
    :return:
    """
    logger.debug('uid: %s, userdata: %s', request.gw_box.uid, request.gw_box.userdata)

    if current_app.config['REDIS_ONLINE_SAVE']:
        # 有效
        try:
            save_redis_online(request.gw_box.uid, request.gw_box.userdata)
        except:
            logger.error('exc occur. request: %s', request, exc_info=True)

    request.write_to_client(dict(
        ret=0
    ))
Esempio n. 5
0
    def inject_json_data(request):
        """
        内部content json
        """

        logger.debug("endpoint: %s, box: %s", request.endpoint, request.box)

        # 先赋值None
        request.json_data = None

        json_body = request.box.get_json()
        if not json_body:
            return

        data = json_body.get("data")
        sign = json_body.get("sign")

        calc_sign = hashlib.md5("|".join([current_app.config["SECRET_KEY"], data])).hexdigest()

        if calc_sign != sign:
            logger.error("sign not equal. sign: %s, calc_sign: %s", sign, calc_sign)
            return

        request.json_data = json.loads(data)