谷歌应用引擎 – 找不到模板文件

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

Go Google app engine - can't find template files

问题

我正在使用Gin框架。
在本地开发模式下:goapp serve,一切正常。

  1. func init() {
  2. route := gin.Default()
  3. route.LoadHTMLGlob("../*/views/**/*.html")
  4. ...
  5. }

但是在部署之后:

panic: html/template: pattern matches no files: ../*/views/**/*.html

好的,我尝试了以下代码:

  1. func init() {
  2. route := gin.Default()
  3. dir, _ := filepath.Abs(filepath.Dir(os.Args[0]))
  4. route.LoadHTMLGlob(dir + "/../*/views/**/*.html")
  5. ...
  6. }

结果相同。

我尝试获取目录:

  1. ...
  2. dir, err := filepath.Abs(filepath.Dir(os.Args[0]))
  3. if err != nil {
  4. ...
  5. }
  6. c.String(http.StatusOK, "Dir: ", dir)
  7. c.String(http.StatusOK, "\nOK")
  8. res, err := filepath.Glob(dir + "/*")
  9. c.String(http.StatusOK, fmt.Sprintf("%v | %v\n\n", res, err))
  10. c.String(http.StatusOK, "Dirs:")
  11. res, err = filepath.Glob(dir + "/**/*")
  12. c.String(http.StatusOK, fmt.Sprintf("%v | %v", res, err))
  13. ...

结果如下:

Dir: %!(EXTRA string=/base/data/home/apps/tmp-LEIYJC/_ah)
OK[/base/data/home/apps/tmp-LEIYJC/_ah/exe] |

Dirs:[] |

哎呀,我做错了什么?

更新:

app.yaml

  1. runtime: go
  2. api_version: go1
  3. handlers:
  4. - url: /images
  5. static_dir: ../static/images
  6. - url: /css
  7. static_dir: ../static/css
  8. - url: /js
  9. static_dir: ../static/js
  10. - url: /fonts
  11. static_dir: ../static/fonts
  12. - url: /.*
  13. script: _go_app

我将app.yaml放在子目录中,因为如果不这样做,会出现另一个问题:
[https://groups.google.com/forum/#!topic/google-appengine-go/dNhqV6PBqVc

文件夹结构

  1. app/
  2. app.go
  3. app.yaml
  4. static/
  5. ...
  6. frontend/
  7. controllers/
  8. UserController.go
  9. ...
  10. models/
  11. UserModel.go
  12. ...
  13. views/
  14. home/
  15. *.html
  16. user/
  17. *.html
  18. anotherfolder/
  19. *.html
  20. backend/
  21. controllers/
  22. MainController.go
  23. ...
  24. models/
  25. SomeModel.go
  26. ...
  27. views/
  28. main/
  29. *.html
  30. anotherfolder/
  31. *.html
  32. ...
英文:

I using Gin framework.
At local development mode: goapp serve
all works fine.

  1. func init() {
  2. route := gin.Default()
  3. route.LoadHTMLGlob("../*/views/**/*.html")
  4. ...
  5. }

But after deploy:

> panic: html/template: pattern matches no files: ../*/views/**/*.html

OK. I try:

  1. func init() {
  2. route := gin.Default()
  3. dir, _ := filepath.Abs(filepath.Dir(os.Args[0]))
  4. route.LoadHTMLGlob(dir + "/../*/views/**/*.html")
  5. ...
  6. }

Same result.

I try fetch dir:

  1. ...
  2. dir, err := filepath.Abs(filepath.Dir(os.Args[0]))
  3. if err != nil {
  4. ...
  5. }
  6. c.String(http.StatusOK, "Dir: ", dir)
  7. c.String(http.StatusOK, "\nOK")
  8. res, err := filepath.Glob(dir + "/*")
  9. c.String(http.StatusOK, fmt.Sprintf("%v | %v\n\n", res, err))
  10. c.String(http.StatusOK, "Dirs:")
  11. res, err = filepath.Glob(dir + "/**/*")
  12. c.String(http.StatusOK, fmt.Sprintf("%v | %v", res, err))
  13. ...

Result:

> Dir: %!(EXTRA string=/base/data/home/apps/tmp-LEIYJC/_ah)
> OK[/base/data/home/apps/tmp-LEIYJC/_ah/exe] | <nil>
>
> Dirs:[] | <nil>

Ooops. What I done wrong?

UPD:

app.yaml

  1. runtime: go
  2. api_version: go1
  3. handlers:
  4. - url: /images
  5. static_dir: ../static/images
  6. - url: /css
  7. static_dir: ../static/css
  8. - url: /js
  9. static_dir: ../static/js
  10. - url: /fonts
  11. static_dir: ../static/fonts
  12. - url: /.*
  13. script: _go_app

I put app.yaml to subdirectory, cause without that another problem:
[https://groups.google.com/forum/#!topic/google-appengine-go/dNhqV6PBqVc

Folder structure:

  1. app/
  2. app.go
  3. app.yaml
  4. static/
  5. ...
  6. frontend/
  7. controllers/
  8. UserController.go
  9. ...
  10. models/
  11. UserModel.go
  12. ...
  13. views/
  14. home/
  15. *.html
  16. user/
  17. *.html
  18. anotherfolder/
  19. *.html
  20. backend/
  21. controllers/
  22. MainController.go
  23. ...
  24. models/
  25. SomeModel.go
  26. ...
  27. views/
  28. main/
  29. *.html
  30. anotherfolder/
  31. *.html
  32. ...

答案1

得分: 2

我找到了下一个解决方案。

我重新组织了结构:

  1. app/
  2. static/
  3. ...
  4. frontend/
  5. views/
  6. home/
  7. *.html
  8. user/
  9. *.html
  10. anotherfolder/
  11. *.html
  12. backend/
  13. views/
  14. main/
  15. *.html
  16. anotherfolder/
  17. *.html
  18. app.go
  19. app.yaml
  20. frontend/
  21. controllers/
  22. UserController.go
  23. ...
  24. models/
  25. UserModel.go
  26. ...
  27. backend/
  28. controllers/
  29. MainController.go
  30. ...
  31. models/
  32. SomeModel.go
  33. ...
  34. ...

更改:

app.yaml

  1. runtime: go
  2. api_version: go1
  3. handlers:
  4. - url: /images
  5. static_dir: static/images
  6. - url: /css
  7. static_dir: static/css
  8. - url: /js
  9. static_dir: static/js
  10. - url: /fonts
  11. static_dir: static/fonts
  12. - url: /.*
  13. script: _go_app

这是一个非常奇怪的 GAE 决策。我不能将 app.yaml 放在应用程序的根目录,因为会出现重复导入的恐慌消息。而且我不能将模板放在根目录,因为它不在范围内,只能在 app.yaml 的父目录中。

英文:

I found next solution.

I reorginize structure:

  1. app/
  2. static/
  3. ...
  4. frontend/
  5. views/
  6. home/
  7. *.html
  8. user/
  9. *.html
  10. anotherfolder/
  11. *.html
  12. backend/
  13. views/
  14. main/
  15. *.html
  16. anotherfolder/
  17. *.html
  18. app.go
  19. app.yaml
  20. frontend/
  21. controllers/
  22. UserController.go
  23. ...
  24. models/
  25. UserModel.go
  26. ...
  27. backend/
  28. controllers/
  29. MainController.go
  30. ...
  31. models/
  32. SomeModel.go
  33. ...
  34. ...

Change:

app.yaml

  1. runtime: go
  2. api_version: go1
  3. handlers:
  4. - url: /images
  5. static_dir: static/images
  6. - url: /css
  7. static_dir: static/css
  8. - url: /js
  9. static_dir: static/js
  10. - url: /fonts
  11. static_dir: static/fonts
  12. - url: /.*
  13. script: _go_app

It's really strange ecision GAE. I can't put app.yaml to root directory for application because I got panic message for duplication import. And I can't put templates to root directory because that not under scope, only at directory parent to app.yaml

答案2

得分: 1

如果我没记错的话,你的所有目录都应该位于根目录下,也就是你的 app.yaml 所在的目录。所以你需要像这样设置目录结构。此外,当你想让应用程序访问静态文件目录时,你需要在该目录的定义中添加 application_readable: true。请参考下面的示例。希望对你有所帮助。

  1. app/
  2. app.go
  3. app.yaml
  4. static/
  5. ...
  6. frontend/views/
  7. home/
  8. *.html
  9. anotherfolder/
  10. *.html

示例目录定义:

  1. - url: /s
  2. static_dir: s/
  3. application_readable: true
英文:

If I'm not mistaken all of your directories should live within the root directory where your app.yaml exists. So you would want something like this. Also, when you want your application to access a static file directory you need to add application_readable: true to that directories definition. See the next example down. Hope this helps.

  1. app/
  2. app.go
  3. app.yaml
  4. static/
  5. ...
  6. frontend/views/
  7. home/
  8. *.html
  9. anotherfolder/
  10. *.html

Example Directory Definition:

  1. - url: /s
  2. static_dir: s/
  3. application_readable: true

huangapple
  • 本文由 发表于 2016年5月25日 18:45:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/37434777.html
匿名

发表评论

匿名网友

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

确定