我花了几个可耻的时间试图解决这个问题无济于事......
问题:
我有一个我正在开发的静态网站,它是通过 Grunt & Assemble 100% 预处理的(如果你熟悉 Jekyll,它本质上是相同的概念)。它还有一个简单的静态博客组件,其中包含各种名称的类别目录。因此,我需要 app.yaml 中的全部内容来适当地路由它们。
但是,我还希望有一个自定义错误页面来代替标准的 GAE 页面状态。
您似乎无法单独在 app.yaml 中完成这两种情况的计算,因为您只能使用一次 catch-all 目标。
这是我当前 app.yaml 中的逻辑
- url: (.*)/
static_files: dist\1/index.html
upload: dist/index.html
expiration: "15m"
- url: /(.*)
static_files: dist/\1/index.html
upload: dist/(.*)/index.html
expiration: "15m"
这非常适合我的用例,因为它会将任何路径路由到索引文件(如果它存在于当前目录中)。但是,因为它使用包罗万象,我不能再将它用于以下内容
- url: /(.*)
static_files: custom_error.html
或依赖
error_handlers:
- file: custom_error.html
因为它只呈现没有匹配 url 模式的路径......
想法:
我的下一个想法是,我可以通过外部 Python 脚本通过一些高级路由来实现这一点
- url: /.*
script: main.app
但是在尝试了无数种不同的设置之后,我还没有偶然发现一种方法来实现这一点。
我使用的一系列面包屑的一个例子是
import os
import webapp2
import jinja2
# vars
jinja_environment = jinja2.Environment(loader=jinja2.FileSystemLoader('jinja'), extensions=['jinja2.ext.autoescape'], autoescape=True)
class mainHandler(webapp2.RequestHandler):
def get(self):
if (an-index-file-exists-in-this-directory)
# follow the same static file logic as the app.yaml
# static_files: dist/\1/index.html
# upload: dist/(.*)/index.html
else:
template = jinja_environment.get_template('404/index.html')
context = {
'page_title': '404',
}
self.response.out.write(template.render(context))
self.error(404)
app = webapp2.WSGIApplication([
('/.*', mainHandler)
], debug=False)
我什至不确定将它放入外部 python 文件是否有助于解决问题,但这是我尴尬的尝试。
当您的包罗万象的模式被用于另一个重要目的时,是否有人对如何实现自定义错误页面有任何想法?
更新:已解决
好的,我终于弄清楚了,但是因为 Stack Overflow 认为我不够酷,无法回答自己的问题(低点阈值?),所以我在这里发布了解决方案:
https://gist.github.com/dustintheweb/c5e6e4ee1a64d50d7f87
祝你好运!