Beispiel #1
0
def save_service(context: 'micro_context',
                 service_id: int,
                 name: str,
                 cookie_url: str,
                 cors: str):
    with context.session as session:
        service = session.query(model.Service).get(service_id)
        if service:
            service.name = name
            service.cookie_url = cookie_url
            service.cors = cors
            service.token = gen_token()
            signal = "service-changed"
        else:
            service = model.Service(id=service_id,
                                    name=name,
                                    cookie_url=cookie_url,
                                    cors=cors,
                                    token=gen_token())
            signal = "service-added"
            session.add(service)
        session.commit()
        result = serialize(service)
        context.broadcast(signal, result)
        return result
Beispiel #2
0
def main():
    load_config()

    handlers = [
        (r"/websocket", ChatHandler),
        (r"/", MainHandler)
    ]
    settings = {
        "debug": True,
        "chat_clients": [],
        "server_id": gen_token(8)
    }

    if options.CLOUDAMQP_URL:
        def broadcast(body):
            for client in settings.get('chat_clients'):
                client.write_message(body)
        queue = PikaTopic(options.CLOUDAMQP_URL,
                          broadcast, 'chat-messages')
        settings['broadcast_queue'] = queue
        logging.info("broadcast_queue %s", options.CLOUDAMQP_URL)
        queue.connect()

    def keep_alive():
        msg = str(time.time()).encode("utf8")
        for client in settings["chat_clients"]:
            client.ping(msg)
    tornado.ioloop.PeriodicCallback(keep_alive, 30000).start()

    application = tornado.web.Application(handlers, **settings)
    application.listen(options.PORT)
    logging.info("listening on port %s", options.PORT)

    tornado.ioloop.IOLoop.current().start()
Beispiel #3
0
def gen_access_token(service_id, user_id):
    ''' save a timeout and return token '''
    global tokens
    token = gen_token()
    timeout = (token, service_id, user_id, datetime.datetime.now())
    tokens.append(timeout)

    # set a timeout in ioloop to remove old tokens
    IOLoop.current().add_timeout(datetime.timedelta(seconds=3),
                                 functools.partial(remove_timeout,
                                                   timeout))
    return token
Beispiel #4
0
def image_upload(context: 'micro_context', file: 'file') -> dict:
    ''' uploads and image to s3 and stores its details '''
    s3path = gen_token(16)
    files = context.files
    aws_config = AWSConfig('AKIAJ3LFZNJ7PVKED43A',
                           os.getenv('s3_config'))
    bucket = Bucket.get_bucket_by_name(aws_config,
                                       'blueshed-blogs')
    result = {}
    with context.session as session:
        for key in files:
            for fileinfo in files[key]:
                fname = fileinfo['filename']
                _, fExt = os.path.splitext(fname)
                fType, fMime = TYPE_MAP.get(fExt.lower(), (None, None))
                if fType is None:
                    raise Exception("File Type not accepted: {}".format(fType))
                key_path = "{}/original{}".format(
                    s3path, fExt) if s3path else "original{}".format(fExt)
                s3key = bucket.add(fileinfo['body'], key=key_path, meta={
                                   "original_name": fname,
                                   "content-type": fMime})
                original = bucket.gen_abs_url(s3key)
                result[key] = {
                    "name": fname,
                    "key": s3key,
                    "original": original
                }
                result[key]["small"] = _make_one(bucket,
                                                 fileinfo['body'],
                                                 THUMB_SIZE,
                                                 fType, fMime,
                                                 s3path,
                                                 "small",
                                                 fExt)
                result[key]["thumb"] = _make_one(bucket,
                                                 fileinfo['body'],
                                                 THUMB_SIZE,
                                                 fType, fMime,
                                                 s3path,
                                                 "thumbnail",
                                                 fExt)
                session.add(model.Image(name=fname, path=original))
    return result