Static file with Google App Engine and Go

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

Static file with Google App Engine and Go

问题

我有一个问题。

我无法访问我的静态目录中的任何文件。

app.yaml:

application: campana-web-1
version: 1
runtime: go
api_version: go1

handlers:
- url: /hello
  script: _go_app
- url: /.*
  static_dir: web

目录结构:

campana-web-1:
  +-- src:
      +-- hello.go
  +-- web:
      +-- index.html
      +-- test.jpg
  +-- app.yaml

我使用 goapp deploy .

当我访问 http://website.com/hello 时,它可以工作,但是当我将hello替换为test.jpg或index.html时,我得到了以下错误:

错误:未找到

服务器上找不到请求的URL。

我漏掉了什么吗?

谢谢。

英文:

I have a problem.

I can't to access to any file from my static dir.

app.yaml:

application: campana-web-1
version: 1
runtime: go
api_version: go1

handlers:
- url: /hello
  script: _go_app
- url: /.*
  static_dir: web

Structure:

campana-web-1:
  +-- src:
      +-- hello.go
  +-- web:
      +-- index.html
      +-- test.jpg
  +-- app.yaml

I use goapp deploy .

When I go to http://website.com/hello it work, but not when I replace hello by test.jpg or index.html I have

Error: Not Found

The requested URL / was not found on this server.

I miss something?

Thank you.

答案1

得分: 1

static_dir功能将目录映射到目录,但不会将文件映射到目录,就像你试图做的那样。

如果你想要使用通配符映射一组文件(而不是包含目录),那么可以使用static_filesupload的组合。

对于你的情况,可以这样写:

  • url: /(.)
    static_files: web/\1
    upload: web/.

但你也可以使用static_dir,只是不要使用通配符部分:

  • url: /
    static_dir: web

这样也可以解决问题。

再次查看文档中的静态目录处理程序静态文件模式部分,特别是关于static_dir的部分,它说:“匹配的URL模式结束后的所有内容都会附加到static_dir,形成所请求文件的完整路径。”这就是为什么你的方法不起作用的原因:static_dir功能的范围比你想象的要小得多。

英文:

The static_dir feature maps directories to directories, but not files to directories as you're trying to do.

If you want to map a collection of files with globs (and no containing directory), then use a combination of static_files and upload instead.

For your case, it'd be:

- url: /(.*)
  static_files: web/
  upload: web/.*

But you can use static_dir, just don't use the glob part:

- url: /
  static_dir: web

This, too, should do the trick.

See the Static Directory Handlers and Static File Patterns sections in the documentation again, and in particular, the section about static_dir that says: "Everything after the end of the matched url pattern is appended to static_dir to form the full path to the requested file." That's why what you're doing doesn't work: the static_dir feature is much more limited in scope than what you're thinking.

huangapple
  • 本文由 发表于 2014年8月25日 23:15:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/25489078.html
匿名

发表评论

匿名网友

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

确定