python
  • google-app-engine
  • bottle
  • 2015-03-25 59 views 3 likes 
    3

    我正在使用Google App Engine和bottle.py,并试图在用户访问/时提供静态HTML文件。为此我有这个在我的main.py使用Google App Engine和瓶子提供静态HTML

    bottle = Bottle() 
    
    @bottle.route('/') 
    def index(): 
        """Serve index.html.""" 
        return static_file('index.html', root='/static') 
    

    我也有我的app.yaml如下:

    handlers: 
    - url: /favicon\.ico 
        static_files: static/favicon.ico 
        upload: static/favicon\.ico 
    - url: /static 
        static_dir: static 
        application-readable: true 
    - url: /.* 
        script: main.bottle 
    

    的图标和CSS文件(无论是在static目录)用于精细,虽然没有直接供应。但是,去/会导致404错误。我对bottle.route以及app.yaml应该做什么我应该做些什么感到困惑。

    为了完整起见,我的目录结构是这样的:

    src 
    +-- main.py 
    +-- app.yaml 
    +-- static 
        +-- favicon.ico 
        +-- index.html 
        +-- stylesheet.css 
    +-- [other unimportant files] 
    

    回答

    3

    为静态文件在App Engine中,这是迄今为止最有效的(为你的用户提供更快,更便宜,如果你通过免费的每日限额),以如此直接从app.yaml做 - 只需添加

    - url:/
        static_files: static/index.html 
    

    到了 “一揽子” url: /.*指令之前app.yaml

    这样,你的应用程序不会排队等待其他人可能在等待的静态文件请求,也不需要启动和预热新实例,也不需要运行任何代码 - 它只会将Google静态文件提供给用户,就像Google知道如何(包括缓存和幕后的CDN加速)一样快(如果适用)。

    如果您可以轻松利用Google自己的服务基础架构,真的没有理由从代码提供静态文件!

    +0

    谢谢,这工作。但是需要添加'upload:static/index.html'到'app.yaml'。旋转一个实例等并没有超出我的想法。你懂得越多。 – Whonut 2015-03-25 10:38:15

    0

    下面的代码应该工作。

    bottle = Bottle() 
    
    @bottle.route('/') 
    def index(): 
        """Serve index.html.""" 
        return static_file('index.html', root=os.path.join(os.path.dirname(os.path.realpath(__file__)), "/static")) 
    
    相关问题