From baf02e2b86f3dd63b5a66a6621243713cb397a6b Mon Sep 17 00:00:00 2001 From: wilbertom Date: Thu, 18 Jul 2013 12:12:38 -0500 Subject: [PATCH 1/4] Switch from getopt to argparse --- sleepymongoose/httpd.py | 42 ++++++++++++++++++++--------------------- 1 file changed, 21 insertions(+), 21 deletions(-) diff --git a/sleepymongoose/httpd.py b/sleepymongoose/httpd.py index 53629d5..21134cc 100644 --- a/sleepymongoose/httpd.py +++ b/sleepymongoose/httpd.py @@ -24,7 +24,7 @@ import os.path, socket import urlparse import cgi -import getopt +import argparse import sys try: @@ -262,26 +262,26 @@ def usage(): def main(): - try: - opts, args = getopt.getopt(sys.argv[1:], "xd:s:m:", ["xorigin", "docroot=", - "secure=", "mongos="]) - - for o, a in opts: - if o == "-d" or o == "--docroot": - if not a.endswith('/'): - a = a+'/' - MongoHTTPRequest.docroot = a - if o == "-s" or o == "--secure": - MongoServer.pem = a - if o == "-m" or o == "--mongos": - MongoHTTPRequest.mongos = a.split(',') - if o == "-x" or o == "--xorigin": - MongoHTTPRequest.response_headers.append(("Access-Control-Allow-Origin","*")) - - except getopt.GetoptError: - print "error parsing cmd line args." - usage() - sys.exit(2) + parser = argparse.ArgumentParser() + parser.add_argument('-d', '--docroot') + parser.add_argument('-s', '--secure') + parser.add_argument('-m', '--mongos') + parser.add_argument('-x', '--xorigin') + + args = parser.parse_args() + if args.docroot: + if not args.docroot.endswith('/'): + args.docroot = args.docroot + '/' + MongoHTTPRequest.docroot = args.docroot + + if args.secure: + MongoServer.pem = args.secure + + if args.mongos: + MongoHTTPRequests.mongos = args.mongos.split(',') + + if args.xorigin: + MongoHTTPRequest.response_headers.append(("Access-Control-Allow-Origin","*")) MongoHTTPRequest.serve_forever(27080) if __name__ == "__main__": From d7ff0c0cafe07d7847cb7144da9c7d5d7a9cf92c Mon Sep 17 00:00:00 2001 From: wilbertom Date: Thu, 18 Jul 2013 12:30:15 -0500 Subject: [PATCH 2/4] Help text on arguments --- sleepymongoose/httpd.py | 17 ++++------------- 1 file changed, 4 insertions(+), 13 deletions(-) diff --git a/sleepymongoose/httpd.py b/sleepymongoose/httpd.py index 21134cc..b79eabf 100644 --- a/sleepymongoose/httpd.py +++ b/sleepymongoose/httpd.py @@ -252,21 +252,12 @@ def setup(self): self.rfile = socket._fileobject(self.request, "rb", self.rbufsize) self.wfile = socket._fileobject(self.request, "wb", self.wbufsize) - -def usage(): - print "python httpd.py [-x] [-d docroot/dir] [-s certificate.pem] [-m list,of,mongods]" - print "\t-x|--xorigin\tAllow cross-origin http requests" - print "\t-d|--docroot\tlocation from which to load files" - print "\t-s|--secure\tlocation of .pem file if ssl is desired" - print "\t-m|--mongos\tcomma-separated list of mongo servers to connect to" - - def main(): parser = argparse.ArgumentParser() - parser.add_argument('-d', '--docroot') - parser.add_argument('-s', '--secure') - parser.add_argument('-m', '--mongos') - parser.add_argument('-x', '--xorigin') + parser.add_argument('-d', '--docroot',help="allows you to specify the location of your files. Defaults to the sleepy.mongoose directory") + parser.add_argument('-s', '--secure', help="location of .pem file if ssl is desired") + parser.add_argument('-m', '--mongos', help="comma-separated list of mongo servers to connect to") + parser.add_argument('-x', '--xorigin', help="allow cross-origin http requests") args = parser.parse_args() if args.docroot: From 1b485867809472a46b18c88f4a58b9ac506f3510 Mon Sep 17 00:00:00 2001 From: wilbertom Date: Thu, 18 Jul 2013 15:01:13 -0500 Subject: [PATCH 3/4] Comments to help understand _parse_call --- sleepymongoose/httpd.py | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/sleepymongoose/httpd.py b/sleepymongoose/httpd.py index b79eabf..ed1ce0e 100644 --- a/sleepymongoose/httpd.py +++ b/sleepymongoose/httpd.py @@ -78,21 +78,28 @@ class MongoHTTPRequest(BaseHTTPRequestHandler): def _parse_call(self, uri): """ this turns a uri like: /foo/bar/_query into properties: using the db - foo, the collection bar, executing a query. + foo, the collection bar, executing a query. The last uri segment must start with '_' returns the database, collection, and action """ parts = uri.split('/') - # operations always start with _ + # here the returned values follow the format of (database, collection, action/func_name) if parts[-1][0] != '_': + # if the last segment of the uri doesn't end with an underscore return None for all return (None, None, None) - if len(parts) == 1: + # if only one folder down ex: http://localhost:port/_hello then the database is admin and the action is the last segment; + # in our example _hello return ("admin", None, parts[0]) elif len(parts) == 2: + # if url is like http:localhost:port/_greetings/_goodbye, the database is _greetings and the action is _goodbye return (parts[0], None, parts[1]) else: + # if the uri is longer than two levels deep and the last segment ends with an '_' the database is the first segment, + # the collection is the second segment up to the but not including the last one joined by periods, + # and the action is the last segment + # example: http://localhost:27080/foo/bar/bazz/sazz/_hello returns ('foo', 'bar.bazz.sazz', '_hello') return (parts[0], ".".join(parts[1:-1]), parts[-1]) @@ -277,4 +284,3 @@ def main(): MongoHTTPRequest.serve_forever(27080) if __name__ == "__main__": main() - From 88dff9a98579f2a7c4251b6f73f5953aa26f01a3 Mon Sep 17 00:00:00 2001 From: wilbertom Date: Thu, 18 Jul 2013 15:07:33 -0500 Subject: [PATCH 4/4] Port argument for better control --- sleepymongoose/httpd.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/sleepymongoose/httpd.py b/sleepymongoose/httpd.py index ed1ce0e..3cc7097 100644 --- a/sleepymongoose/httpd.py +++ b/sleepymongoose/httpd.py @@ -244,7 +244,7 @@ def serve_forever(port): MongoHandler.mh = MongoHandler(MongoHTTPRequest.mongos) - print "listening for connections on http://localhost:27080\n" + print "listening for connections on http://localhost:%s\n" % (port) try: server.serve_forever() except KeyboardInterrupt: @@ -265,7 +265,7 @@ def main(): parser.add_argument('-s', '--secure', help="location of .pem file if ssl is desired") parser.add_argument('-m', '--mongos', help="comma-separated list of mongo servers to connect to") parser.add_argument('-x', '--xorigin', help="allow cross-origin http requests") - + parser.add_argument('-p', '--port', help="port where the interface will be served on", type=int) args = parser.parse_args() if args.docroot: if not args.docroot.endswith('/'): @@ -280,7 +280,7 @@ def main(): if args.xorigin: MongoHTTPRequest.response_headers.append(("Access-Control-Allow-Origin","*")) - - MongoHTTPRequest.serve_forever(27080) + + MongoHTTPRequest.serve_forever(args.port or 27080) if __name__ == "__main__": main()