Example #1
0
    def post(self):
        c = tornadoredis.Client(connection_pool=CONNECTION_POOL)
        url = self.get_argument("url")
        if not util.validate_url(url):
            self.write("not a valid url")
            self.finish()
            return

        key = util.gen_cache_key(const.CACHE_KEY_PREFIX.REVERSE_URL, url)
        short_id = redis_short_id = yield tornado.gen.Task(c.get, key)

        while not redis_short_id:
            short_id = util.gen_short_id()
            key = util.gen_cache_key(const.CACHE_KEY_PREFIX.SHORT_ID, short_id)
            ret = yield tornado.gen.Task(c.get, key)
            if ret:
                continue

            yield tornado.gen.Task(c.set, key, url)
            key = util.gen_cache_key(const.CACHE_KEY_PREFIX.REVERSE_URL, url)
            yield tornado.gen.Task(c.set, key, short_id)
            break

        self.write(urlparse.urljoin(config.SITE_URL, short_id))
        self.finish()
Example #2
0
    def post(self):
        c = tornadoredis.Client(connection_pool=CONNECTION_POOL)
        url = self.get_argument("url")
        if not util.validate_url(url):
            self.write("not a valid url")
            self.finish()
            return
        
        key = util.gen_cache_key(const.CACHE_KEY_PREFIX.REVERSE_URL, url)
        short_id = redis_short_id = yield tornado.gen.Task(c.get, key)

        while not redis_short_id:
            short_id = util.gen_short_id()
            key = util.gen_cache_key(const.CACHE_KEY_PREFIX.SHORT_ID, short_id)
            ret = yield tornado.gen.Task(c.get, key)
            if ret:
                continue

            yield tornado.gen.Task(c.set, key, url)
            key = util.gen_cache_key(const.CACHE_KEY_PREFIX.REVERSE_URL, url)
            yield tornado.gen.Task(c.set, key, short_id)
            break

        self.write(urlparse.urljoin(config.SITE_URL, short_id))
        self.finish()
Example #3
0
def delete_app_access_token(app_id):
    r = ReleaseApiRedisClient
    # k:ap
    logging.getLogger('').info('============='+str(r))
    print('d', gen_cache_key(_cache_key.AUTH_APP_ID, app_id))

    logging.getLogger('').info('============='+str(gen_cache_key(_cache_key.AUTH_APP_ID, app_id)))
    if r.exists(gen_cache_key(_cache_key.AUTH_APP_ID, app_id)):
        d = r.hget(gen_cache_key(_cache_key.AUTH_APP_ID, app_id), "access_token")

        if d:
            d = d.decode('utf-8')
            r.delete(gen_cache_key(_cache_key.AUTH_ACCESS_TOKEN, d))
            return True
Example #4
0
 def get(self, short_id):
     c = tornadoredis.Client(connection_pool=CONNECTION_POOL)
     key = util.gen_cache_key(const.CACHE_KEY_PREFIX.SHORT_ID, short_id)
     url = yield tornado.gen.Task(c.get, key)
     if url:
         self.redirect(url, permanent=True)
     else:
         raise HTTPError(404)
Example #5
0
 def get(self, short_id):
     c = tornadoredis.Client(connection_pool=CONNECTION_POOL)
     key = util.gen_cache_key(const.CACHE_KEY_PREFIX.SHORT_ID, short_id)
     url = yield tornado.gen.Task(c.get, key)
     if url:
         self.redirect(url, permanent=True)
     else:
         raise HTTPError(404)
Example #6
0
def delete_release_api_app(app_id):
    """
    删除正式的API
    :param app_id:
    :return:
    """
    try:
        screen = dict(
            app_id=app_id
        )
        # 获取旧的access_token ======================
        db = ReleaseApiMongoDBClient
        release_old_access_token = ""
        document = db.ebc_api_app.find_one(screen, {"access_token": 1})
        if document:
            release_old_access_token = document.get("access_token", "")
        # 获得旧的secret
        app_secret = ""
        document = db.ebc_api_app.find_one(screen, {"app_secret": 1})
        if document:
            app_secret = document.get("app_secret", "")

        # 删除正式的API_APP字段 ======================
        db.ebc_api_app.remove(screen)
        # 删除正式的缓存 ======================
        r = ReleaseApiRedisClient
        # k:ap
        if r.exists(gen_cache_key(_cache_key.AUTH_APP_ID, app_id)):
            r.delete(gen_cache_key(_cache_key.AUTH_APP_ID, app_id))
        # k.as
        if r.exists(gen_cache_key(_cache_key.AUTH_APP_SECRET, app_id, app_secret)):
            r.delete(gen_cache_key(_cache_key.AUTH_APP_SECRET, app_id, app_secret))
        # k:ak
        if r.exists(gen_cache_key(_cache_key.AUTH_ACCESS_TOKEN, release_old_access_token)):
            r.delete(gen_cache_key(_cache_key.AUTH_ACCESS_TOKEN, release_old_access_token))
        return True
    except Exception as e:
        logging.getLogger("").error(e)
        return False
Example #7
0
def reset_api_app_secret(app_id, new_app_secret, ):
    """
    重置app_secret
    :param app_id:
    :param old_app_secret:
    :return:
    """
    try:
        screen = dict(
            app_id=app_id
        )
        new_access_token = gen_app_access_token()
        # 获取沙箱旧的 access_token
        db = SandboxApiMongoDBClient
        old_access_token = ""
        document = db.ebc_api_app.find_one(screen, {"access_token": 1})
        if document:
            old_access_token = document.get("access_token", "")
        # 获得旧的secret
        old_app_secret = ""
        document = db.ebc_api_app.find_one(screen, {"app_secret": 1})
        if document:
            old_app_secret = document.get("app_secret", "")

        # 更新沙箱的API_APP字段 Secret =====================
        db.ebc_api_app.update(screen, {"$set": {"app_secret": new_app_secret, "access_token": new_access_token}})
        # 更新沙箱的缓存 =====================
        r = SandboxApiRedisClient
        # k:ap
        if r.exists(gen_cache_key(_cache_key.AUTH_APP_ID, app_id)):
            r.hset(gen_cache_key(_cache_key.AUTH_APP_ID, app_id), "access_token", new_access_token)
        # k.as
        if r.exists(gen_cache_key(_cache_key.AUTH_APP_SECRET, app_id, old_app_secret)):
            r.rename(gen_cache_key(_cache_key.AUTH_APP_SECRET, app_id, old_app_secret),
                     gen_cache_key(_cache_key.AUTH_APP_SECRET, app_id, new_app_secret))
        # k:ak
        if r.exists(gen_cache_key(_cache_key.AUTH_ACCESS_TOKEN, old_access_token)):
            r.rename(gen_cache_key(_cache_key.AUTH_ACCESS_TOKEN, old_access_token),
                     gen_cache_key(_cache_key.AUTH_ACCESS_TOKEN, new_access_token))
            r.hset(gen_cache_key(_cache_key.AUTH_ACCESS_TOKEN, new_access_token), "access_token", new_access_token)
        # 获取正式旧的 access_token
        db = ReleaseApiMongoDBClient
        old_access_token = ""
        document = db.ebc_api_app.find_one(screen, {"access_token": 1})
        if document:
            old_access_token = document.get("access_token", "")
        # 获得正式旧的 secret
        old_app_secret = ""
        document = db.ebc_api_app.find_one(screen, {"app_secret": 1})
        if document:
            old_app_secret = document.get("app_secret", "")
        # 更新正式的API_APP字段 Secret =====================
        db.ebc_api_app.update(screen, {"$set": {"app_secret": new_app_secret, "access_token": new_access_token}})
        # 更新正式的缓存 =====================
        r = ReleaseApiRedisClient
        # k:ap
        if r.exists(gen_cache_key(_cache_key.AUTH_APP_ID, app_id)):
            r.hset(gen_cache_key(_cache_key.AUTH_APP_ID, app_id), "access_token", new_access_token)
        # k.as
        if r.exists(gen_cache_key(_cache_key.AUTH_APP_SECRET, app_id, old_app_secret)):
            r.rename(gen_cache_key(_cache_key.AUTH_APP_SECRET, app_id, old_app_secret),
                     gen_cache_key(_cache_key.AUTH_APP_SECRET, app_id, new_app_secret))
        # k:ak
        if r.exists(gen_cache_key(_cache_key.AUTH_ACCESS_TOKEN, old_access_token)):
            r.rename(gen_cache_key(_cache_key.AUTH_ACCESS_TOKEN, old_access_token),
                     gen_cache_key(_cache_key.AUTH_ACCESS_TOKEN, new_access_token))
            r.hset(gen_cache_key(_cache_key.AUTH_ACCESS_TOKEN, new_access_token), "access_token", new_access_token)
        return True
    except Exception as e:
        logging.getLogger("").error(e)