5

我现在正在将我的小型 Google App Engine 应用程序迁移到 Heroku 平台。我实际上并没有使用 Bigtable,并且webapp2大大降低了我的迁移成本。

现在我坚持处理静态文件。

有什么好的做法吗?如果是这样,请带我去那里。

提前致谢。

编辑

好吧,我现在正在使用paste我的 WSGI 服务器。并且paste.StaticURLParser()应该是我实现静态文件处理程序所需要的。但是我不知道如何将它与webapp2.WSGIApplication(). 有人可以帮我吗?

也许我需要重写webapp2.RequestHandler类才能正确加载paste.StaticURLParser()

import os
import webapp2
from paste import httpserver

class StaticFileHandler(webapp2.RequestHandler):
    u"""Static file handler"""

    def __init__(self):
        # I guess I need to override something here to load
        # `paste.StaticURLParser()` properly.
        pass

app = webapp2.WSGIApplication([(r'/static', StaticFileHandler)], debug=True)


def main():
    port = int(os.environ.get('PORT', 5000))
    httpserver.serve(app, host='0.0.0.0', port=port)

if __name__ == '__main__':
    main()

任何帮助将不胜感激!

4

3 回答 3

7

下面是我如何得到这个工作。

我猜依靠级联应用程序不是最有效的选择,但它足以满足我的需求。

from paste.urlparser import StaticURLParser
from paste.cascade import Cascade
from paste import httpserver
import webapp2
import socket


class HelloWorld(webapp2.RequestHandler):
    def get(self):
        self.response.write('Hello cruel world.')

# Create the main app
web_app = webapp2.WSGIApplication([
    ('/', HelloWorld),
])

# Create an app to serve static files
# Choose a directory separate from your source (e.g., "static/") so it isn't dl'able
static_app = StaticURLParser("static/")

# Create a cascade that looks for static files first, then tries the webapp
app = Cascade([static_app, web_app])

def main():
    httpserver.serve(app, host=socket.gethostname(), port='8080')

if __name__ == '__main__':
    main()
于 2012-01-05T05:07:49.987 回答
2

考虑到我迟到了,但实际上我更喜欢 DirectoryApp。它处理的事情有点像 AppEngine。我可以在我的 src 目录中创建一个“静态”目录,然后我可以像这样在我的 HTML(或其他)中引用这些目录:http:..localhost:8080/static/js/jquery.js

static_app = DirectoryApp("static")

# Create a cascade that looks for static files first, then tries the webapp
app = Cascade([static_app,wsgi_app])

httpserver.serve(app, host='0.0.0.0',
                 port='8080')
于 2012-07-31T22:14:00.280 回答
2

这使您的代码更易于部署,因为您可以使用 nginx 在生产中提供静态媒体。

def main():
  application = webapp2.WSGIApplication(routes, config=_config, debug=DEBUG)
  if DEBUG:
    # serve static files on development
    static_media_server = StaticURLParser(MEDIA_ROOT)
    app = Cascade([static_media_server, application])
    httpserver.serve(app, host='127.0.0.1', port='8000')
else:
    httpserver.serve(application, host='127.0.0.1', port='8000')
于 2012-01-07T08:04:21.167 回答