Ejemplo n.º 1
0
def process(req, options):
    # handle 'special' requests'
    if len(req['path'])==3 and req['path'][-1] == '_reset':
        msg = '%d document(s) dropped' % len(_handlers)
        resp = {'code': 200, 'json': {'info': msg}}
        _handlers.clear()
    elif len(req['path'])==3 and req['path'][-1] == '_exit':
        json.dump({'code': 200, 'json': {'info': 'goodbye'}}, sys.stdout)
        sys.stdout.write("\n")
        sys.stdout.flush()
        sys.exit(0)
    else:
        try:
            handler = get_api_handler(options, req)
        except APILoadError, exc:
            log("%s", exc.msg)
            resp = {'code': 400, 'json': {'error': 400, 'reason': exc.msg}}
        else:
Ejemplo n.º 2
0
def main():
    # for now, so we know the couch defaults
    cfg = config.init_config().couches['local']
    parser = optparse.OptionParser("%prog [options]",
                                   description="the raindrop api runner")
    parser.add_option("", "--couchdb-host",
                help="Specifies the hostname to use for couchdb requests",
                default=cfg['host'])

    parser.add_option("", "--couchdb-port",
                help="Specifies the port number to use for couchdb requests",
                default=cfg['port'])

    parser.add_option("", "--show-perf", action="store_true",
                help="Reports some information about how long some things take")

    options, args = parser.parse_args()

    # no args used
    if len(args) != 0:
        print >> sys.stderr, __doc__
        print >> sys.stderr, "this program takes no arguments"
        sys.exit(1)

    if options.show_perf:
        global note_timeline
        note_timeline = do_note_timeline
    CouchDB.default_host = options.couchdb_host
    CouchDB.default_port = options.couchdb_port

    log("raindrops keep falling on my api...")
    # and now the main loop.
    while True:
        line = sys.stdin.readline()
        if not line:
            break
        req = json.loads(line)
        resp = process(req, options)

        json.dump(resp, sys.stdout)
        sys.stdout.write("\n")
        sys.stdout.flush()
    log("raindrop api runner terminating normally")