I am creating a site using OpenShift with Tornado and I need to use persistent storage for images and other files. Documentation says, what persistent storage path stored in $OPENSHIFT_DATA_DIR. Here is app file system overview:
|-- app-root
| -- data # <--$OPENSHIFT_DATA_DIR
| -- repo #
| -- wsgi
| -- static
| -- img
| -- templates
| -- ...
| -- runtime
| -- data
| -- repo
| -- ...deployed application code
And here I have a problem: I tried to serve images and other files from $OPENSHIFT_DATA_DIR, but it's doesn't work, I always get 404 because $OPENSHIFT_DATA_DIR placed outside of wsgi path. I founded what I'm not alone and Django/Java/Flask developers using OpenShift having same problems.
I tried to serve files from /static folder, but that was bad idea: making git push will delete all new files founded here.
I tried to do something like this, but it doesn't work too for me. Maybe I doing something wrong and it need to rewrite something else to use this trick for Tornado?
Can someone help me? Thanks!
EDIT
Tried to use tornado.web.StaticFileHandler, add it to handlers like this:
__UPLOADS__ = str(os.environ.get('OPENSHIFT_DATA_DIR'))+"/uploads" ...
handlers = [(r'/',MainHandler,),
(r'/media/(.*)', tornado.web.StaticFileHandler, {'path': __UPLOADS__}),
...,]
Find what this way Tornado replace __UPLOADS__+/media/img.jpg to wsgi/static/img.jpg
Here is application file, maybe it can make something clear:
#!/usr/bin/env python
import os
import sys
if 'OPENSHIFT_REPO_DIR' in os.environ:
sys.path.append(os.path.join(os.environ['OPENSHIFT_REPO_DIR'], 'wsgi',))
virtenv = os.environ['OPENSHIFT_PYTHON_DIR'] + '/virtenv/venv'
os.environ['PYTHON_EGG_CACHE'] = os.path.join(virtenv, 'lib/python3.3/site-packages')
virtualenv = os.path.join(virtenv, 'bin/activate_this.py')
try:
exec(compile(open(virtualenv).read(), virtualenv, 'exec'),dict(__file__ = virtualenv))
except IOError:
pass
import tornado.wsgi
from openshift import handlers
if 'OPENSHIFT_REPO_DIR' in os.environ:
settings = {
'cookie_secret': 'TOP_SECRET',
'template_path' : os.path.join(os.environ['OPENSHIFT_REPO_DIR'], 'wsgi/templates'),
'xsrf_cookies': True,
'debug': True,
'login_url': '/login',
}
else:
settings = {
'cookie_secret': 'TOP_SECRET',
'template_path' : os.path.join(os.getcwd(), 'wsgi/templates'),
'xsrf_cookies': True,
'debug': True,
'login_url': '/login',
}
application = tornado.wsgi.WSGIApplication(handlers, **settings)