Migrating from Google App Engine to Heroku (missing YAML)

匿名 (未验证) 提交于 2019-12-03 01:49:02

问题:

Overnight hack, trying to create an environment where GAE code (using Python libs/packages) could be easily ported over to Heroku with minimal editing.

EDIT
Q: YAML offers static file sharing with only 3 lines of code, I'm trying to figure out how to implement this file sharing with _minimal_editing_ (keyword).

For example, to share the 'static/' folder. One solution is to implement a number of classes found in http://docs.webob.org/en/latest/file-example.html - not an elegant answer.

The big picture is to empower the developer with the freedom of choice to choose a (hopefully) better/cheaper cloud provider, follow steps 1,2,3... and the app will be up and running with minimal fuss. Hope this clears up the confusion.


In case anyone inquires, my code is as follows...

The "main.py" file:

import jinja2 import webapp2 import os  jinja_environment = jinja2.Environment(     loader=jinja2.FileSystemLoader(         os.path.join(os.path.dirname(__file__), 'templates')))   class HelloWebapp2(webapp2.RequestHandler):     def get(self):         template_values = { 'test': 'Hello World!!'}         template = jinja_environment.get_template('jinja2_test.html')         return self.response.out.write(template.render(template_values))   app2 = webapp2.WSGIApplication([     ('/', HelloWebapp2) ], debug=True)  def main():     from paste import httpserver     port = int(os.environ.get("PORT", 5000))     httpserver.serve(app2, host='0.0.0.0', port=port)  if __name__ == '__main__':     main() 

In the "requirements.txt" file:

Jinja2==2.6 webapp2==2.3 paste==1.7.5.1 webob==1.1.1 

The output file "templates/jinja2_test.html":

{{test}} 

The default "procfile":

web: python main.py 

回答1:

It sounds to me like the problem you're trying to solve is serving static pages using Django on Heroku.

collectstatic seems to be the right answer for this: web: python my_django_app/manage.py collectstatic --noinput; bin/gunicorn_django --workers=4 --bind=0.0.0.0:$PORT my_django_app/settings.py

http://matthewphiong.com/managing-django-static-files-on-heroku



回答2:

On heroku platform, with high volume apps you're supposed to use amazon s3 for static asset storage: https://devcenter.heroku.com/articles/s3

For development purposes I created a simple "catch all" handler (it should be the last handler in the list) that serves static files that end with certain suffixes from the project directory. Adding this is quite simple. But remember, it's not very effective, and it's a waste of the web dynos resources.

import webapp2 import re import os  class FileHandler(webapp2.RequestHandler):   def get(self,path):      if re.search('html$',path):       self.response.headers['Content-Type'] = 'text/html'      elif re.search('css$',path):       self.response.headers['Content-Type'] = 'text/css'      elif re.search('js$',path):       self.response.headers['Content-Type'] = 'application/javascript'      elif re.search('gif$',path):       self.response.headers['Content-Type'] = 'image/gif'      elif re.search('png$',path):       self.response.headers['Content-Type'] = 'image/png'      else:       self.abort(403)     try:       f=open("./"+path,'r')     except IOError:       self.abort(404)     self.response.write(f.read())     f.close  app = webapp2.WSGIApplication([     (r'/(.*)', FileHandler), ], debug=True)  def main():     from paste import httpserver     port = int(os.environ.get('PORT', 8080))     httpserver.serve(app, host='0.0.0.0', port=port)  if __name__ == '__main__':     main() 


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!