app.yaml:URL中的通配符与static_dir一起使用吗?

huangapple go评论86阅读模式
英文:

app.yaml : wildcard in URL with static_dir?

问题

我在app.yaml中尝试使用正则表达式作为目录名进行匹配,但不起作用:

- url: /v1_.*
  static_dir: static/v1
  expiration: "364d"

尽管官方规范中说支持正则表达式语法,但有没有办法让它起作用?

也就是说,/v1_2014-01-29/img/logo.png应该匹配静态文件/static/v1/img/logo.png

小知识

我使用Google App Engine来提供Go webapp。

我希望最大限度地延长浏览器缓存的寿命,减少请求数量,并仍然提供最新版本的css/js/png文件,我相信文件名版本控制是实现这一目标的最佳实践。此外,由于添加一个变量查询字符串(/v1/img/logo.png?2014-01-29)可能会导致代理和缓存问题,我更喜欢显示一个变量目录名(/v1_2014-01-29/img/logo.png),指向相同的底层服务器目录。

英文:

My attempt at matching a regex as directory name in app.yaml doesn't work :

- url: /v1_.*
  static_dir: static/v1
  expiration: "364d"

Although this official spec says regex syntax is supported. Is there a way to make this work ?

I.e. /v1_2014-01-29/img/logo.png should match the static file /static/v1/img/logo.png.

Trivia

I use Google App Engine to serve a Go webapp.

I'd like to maximize browser cache longevity, minimize number of requests and still serve the fresh versions of my css/js/png, and I believe revving filenames is a best practice to achieve this. Also as adding a variable querystring (/v1/img/logo.png?2014-01-29) might cause proxy and cache problems, I prefer to show a variable directory name (/v1_2014-01-29/img/logo.png), pointing to the same underlying server dir.

答案1

得分: 2

似乎无论URL的哪个部分超出了URL定义的匹配范围(从开头开始匹配),都会附加到static_dir上。

因此,如果文件路径为static/v1/img/logo.png,则以下处理程序应该匹配/v1_2014-01-29/img/logo.png(使用Python尝试过):

  • url: /v1_(\d+-?)+
    static_dir: static/v1
英文:

Seems to me that whatever part of the URL that is beyond the match of the url definition (which matches from the start) is appended to the static_dir.

So the following handler should match /v1_2014-01-29/img/logo.png if the file path is static/v1/img/logo.png (tried with Python):

- url: /v1_(\d+-?)+
  static_dir: static/v1

答案2

得分: 0

在olivierdm的回答之后,我将我的yaml更改为:

- url: /v1_.*_
  static_dir: static/v1
  expiration: "364d"

并且我将我的HTML模板更改为生成/v1_2014-01-29_/img/logo.png

基本上,额外的任意字符下划线_强制.*匹配2014-01-29,而不是空字符串。

现在,每当我想要访问者重新加载静态文件时,我只需更改模板中的日期(我不再触碰app.yaml)。此外,对于任何意外请求到一个“过时”的URL,仍然会成功并提供新鲜的资源。

英文:

After olivierdm's answer I changed my yaml into :

- url: /v1_.*_
  static_dir: static/v1
  expiration: "364d"

and my html templates to produce /v1_2014-01-29_/img/logo.png.

Basically, the extra arbitrary character underscore _ forces .* to match 2014-01-29, not the empty string.

Now every time I want the visitors to reload the static files, I just change the date in the tempating (I don't touch the app.yaml anymore). Also, any accidental request to an "outdated" URL will still succeed and serve the fresh resource.

huangapple
  • 本文由 发表于 2014年1月30日 06:06:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/21443688.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定