How to store uScopy data in MondoDB: Difference between revisions
From KitwarePublic
Jump to navigationJump to search
No edit summary |
No edit summary |
||
Line 10: | Line 10: | ||
import sys | import sys | ||
# add module path | |||
sys.path.append('/var/www-dj/') | sys.path.append('/var/www-dj/') | ||
# import module with singleton instance | |||
from mongo import m | from mongo import m | ||
def application(environ, start_response): | def application(environ, start_response): | ||
""" The WSGI | """ The WSGI application to retrieve images from database """ | ||
def error(error_msg): | def error(error_msg): | ||
status = "200 OK" | status = "200 OK" | ||
Line 26: | Line 28: | ||
db = m.conn['clay1'] | db = m.conn['clay1'] | ||
try: | try: | ||
str2 = environ['PATH_INFO'] | str2 = environ['PATH_INFO'] | ||
except: | except: | ||
# Give some default request if | |||
# no parameters passed in URL | |||
str2 = '/tiles3.py/n1/t.jpg' | str2 = '/tiles3.py/n1/t.jpg' | ||
Line 48: | Line 51: | ||
('Content-Length', str(len(res['file'])))] | ('Content-Length', str(len(res['file'])))] | ||
start_response(status, response_headers) | start_response(status, response_headers) | ||
return [str(res['file'])] | return [str(res['file'])] | ||
</source> | </source> | ||
Latest revision as of 20:13, 18 January 2011
Server side code
Python code <source lang="python"> """
Decodes the path info and fetches the corresponding tile from pymongo
""" import pymongo import sys
- add module path
sys.path.append('/var/www-dj/')
- import module with singleton instance
from mongo import m
def application(environ, start_response):
""" The WSGI application to retrieve images from database """ def error(error_msg): status = "200 OK" response_headers = [('Content-type', 'text/plain'), ('Content-Length', str(len(error_msg)))] start_response(status, response_headers) return [error_msg]
# emit status / headers db = m.conn['clay1']
try: str2 = environ['PATH_INFO'] except: # Give some default request if # no parameters passed in URL str2 = '/tiles3.py/n1/t.jpg'
path = str2.split('/') if len(path) != 3: #incorrect syntax return error(str(path))
col = db[path[1]] res = col.find_one({"name" : path[2]}) m.conn.end_request() if res == None: return error('Item not found in database ..')
# Create the response headers status = "200 OK" response_headers = [('Content-type', 'image/jpeg'), ('Content-Length', str(len(res['file'])))] start_response(status, response_headers) return [str(res['file'])]
</source>
Mongo module manages connection pool internally. Another simple script is used to recreate indexes after data has been added.