Skip to content
This repository was archived by the owner on Dec 10, 2018. It is now read-only.

Commit 4d84092

Browse files
committed
Merge pull request #12 from jeffmax/master
Set default mongo server on startup
2 parents 0ab625d + 0e07740 commit 4d84092

File tree

2 files changed

+18
-7
lines changed

2 files changed

+18
-7
lines changed

handlers.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,11 @@ def __init__(self, mongos):
3535
args = MongoFakeFieldStorage({"server" : host})
3636

3737
out = MongoFakeStream()
38-
39-
name = host.replace(".", "")
40-
name = name.replace(":", "")
38+
if len(mongos) == 1:
39+
name = "default"
40+
else:
41+
name = host.replace(".", "")
42+
name = name.replace(":", "")
4143

4244
self._connect(args, out.ostream, name = name)
4345

httpd.py

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ class MongoHTTPRequest(BaseHTTPRequestHandler):
7272

7373
docroot = "."
7474
mongos = []
75+
response_headers = []
7576

7677
def _parse_call(self, uri):
7778
"""
@@ -80,7 +81,6 @@ def _parse_call(self, uri):
8081
8182
returns the database, collection, and action
8283
"""
83-
8484
parts = uri.split('/')
8585

8686
# operations always start with _
@@ -114,6 +114,8 @@ def call_handler(self, uri, args):
114114
if callable(func):
115115
self.send_response(200, 'OK')
116116
self.send_header('Content-type', MongoHTTPRequest.mimetypes['json'])
117+
for header in self.response_headers:
118+
self.send_header(header[0], header[1])
117119
self.end_headers()
118120

119121
func(args, self.wfile.write, name = name, db = db, collection = collection)
@@ -137,6 +139,8 @@ def process_uri(self, method):
137139
else:
138140
self.send_response(100, "Continue")
139141
self.send_header('Content-type', MongoHTTPRequest.mimetypes['json'])
142+
for header in self.response_headers:
143+
self.send_header(header[0], header[1])
140144
self.end_headers()
141145
self.wfile.write('{"ok" : 0, "errmsg" : "100-continue msgs not handled yet"}')
142146

@@ -168,6 +172,8 @@ def do_GET(self):
168172

169173
self.send_response(200, 'OK')
170174
self.send_header('Content-type', MongoHTTPRequest.mimetypes[type])
175+
for header in self.response_headers:
176+
self.send_header(header[0], header[1])
171177
self.end_headers()
172178
self.wfile.write(fh.read())
173179

@@ -195,7 +201,6 @@ def do_POST(self):
195201
return
196202
self.call_handler(uri, args)
197203

198-
199204
@staticmethod
200205
def serve_forever(port):
201206
print "\n================================="
@@ -234,15 +239,17 @@ def setup(self):
234239

235240

236241
def usage():
237-
print "python httpd.py [-d docroot/dir] [-s certificate.pem] [-m list,of,mongods]"
242+
print "python httpd.py [-x] [-d docroot/dir] [-s certificate.pem] [-m list,of,mongods]"
243+
print "\t-x|--xorigin\tAllow cross-origin http requests"
238244
print "\t-d|--docroot\tlocation from which to load files"
239245
print "\t-s|--secure\tlocation of .pem file if ssl is desired"
240246
print "\t-m|--mongos\tcomma-separated list of mongo servers to connect to"
241247

242248
if __name__ == "__main__":
243249

244250
try:
245-
opts, args = getopt.getopt(sys.argv[1:], "d:s:m:", ["docroot=", "secure=", "mongos="])
251+
opts, args = getopt.getopt(sys.argv[1:], "xd:s:m:", ["xorigin", "docroot=",
252+
"secure=", "mongos="])
246253

247254
for o, a in opts:
248255
if o == "-d" or o == "--docroot":
@@ -253,6 +260,8 @@ def usage():
253260
MongoServer.pem = a
254261
if o == "-m" or o == "--mongos":
255262
MongoHTTPRequest.mongos = a.split(',')
263+
if o == "-x" or o == "--xorigin":
264+
MongoHTTPRequest.response_headers.append(("Access-Control-Allow-Origin","*"))
256265

257266
except getopt.GetoptError:
258267
print "error parsing cmd line args."

0 commit comments

Comments
 (0)