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

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

Go Google app engine - can't find template files

问题

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

func init() {
    route := gin.Default()
    route.LoadHTMLGlob("../*/views/**/*.html")
    ...
}

但是在部署之后:

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

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

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

结果相同。

我尝试获取目录:

...
dir, err := filepath.Abs(filepath.Dir(os.Args[0]))
if err != nil {
    ...
}
c.String(http.StatusOK, "Dir: ", dir)

c.String(http.StatusOK, "\nOK")
res, err := filepath.Glob(dir + "/*")
c.String(http.StatusOK, fmt.Sprintf("%v | %v\n\n", res, err))

c.String(http.StatusOK, "Dirs:")
res, err = filepath.Glob(dir + "/**/*")
c.String(http.StatusOK, fmt.Sprintf("%v | %v", res, err))
...

结果如下:

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

Dirs:[] |

哎呀,我做错了什么?

更新:

app.yaml

runtime: go
api_version: go1

handlers:
- url: /images
  static_dir: ../static/images

- url: /css
  static_dir: ../static/css

- url: /js
  static_dir: ../static/js

- url: /fonts
  static_dir: ../static/fonts

- url: /.*
  script: _go_app

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

文件夹结构

app/
    app.go
    app.yaml
static/
    ...
frontend/
    controllers/
        UserController.go
        ...
    models/
        UserModel.go
        ...
    views/
        home/
            *.html
        user/
            *.html
        anotherfolder/
            *.html
backend/
    controllers/
        MainController.go
        ...
    models/
        SomeModel.go
        ...
    views/
        main/
            *.html
        anotherfolder/
            *.html
...
英文:

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

func init() {
	route := gin.Default()
	route.LoadHTMLGlob("../*/views/**/*.html")
	...
}

But after deploy:

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

OK. I try:

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

Same result.

I try fetch dir:

...
dir, err := filepath.Abs(filepath.Dir(os.Args[0]))
if err != nil {
	...
}
c.String(http.StatusOK, "Dir: ", dir)

c.String(http.StatusOK, "\nOK")
res, err := filepath.Glob(dir + "/*")
c.String(http.StatusOK, fmt.Sprintf("%v | %v\n\n", res, err))

c.String(http.StatusOK, "Dirs:")
res, err = filepath.Glob(dir + "/**/*")
c.String(http.StatusOK, fmt.Sprintf("%v | %v", res, err))
...

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

runtime: go
api_version: go1

handlers:
- url: /images
  static_dir: ../static/images

- url: /css
  static_dir: ../static/css

- url: /js
  static_dir: ../static/js

- url: /fonts
  static_dir: ../static/fonts

- url: /.*
  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:

app/
    app.go
    app.yaml
static/
    ...
frontend/
    controllers/
        UserController.go
        ...
    models/
        UserModel.go
        ...
    views/
        home/
            *.html
        user/
            *.html
        anotherfolder/
            *.html
backend/
    controllers/
        MainController.go
        ...
    models/
        SomeModel.go
        ...
    views/
        main/
            *.html
        anotherfolder/
            *.html
...

答案1

得分: 2

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

我重新组织了结构:

app/
	static/
	    ...
	frontend/
		views/
		   home/
		       *.html
		   user/
		       *.html
		   anotherfolder/
		       *.html
	backend/
   		views/
			main/
				*.html
    		anotherfolder/
				*.html
	app.go
	app.yaml

frontend/
	controllers/
	    UserController.go
	    ...
	models/
	    UserModel.go
	    ...
backend/
	controllers/
		MainController.go
	...
	models/
		SomeModel.go
	...
...

更改:

app.yaml

runtime: go
api_version: go1

handlers:
- url: /images
  static_dir: static/images

- url: /css
  static_dir: static/css

- url: /js
  static_dir: static/js

- url: /fonts
  static_dir: static/fonts

- url: /.*
  script: _go_app

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

英文:

I found next solution.

I reorginize structure:

app/
	static/
	    ...
	frontend/
		views/
		   home/
		       *.html
		   user/
		       *.html
		   anotherfolder/
		       *.html
	backend/
   		views/
			main/
				*.html
    		anotherfolder/
				*.html
	app.go
	app.yaml

frontend/
	controllers/
	    UserController.go
	    ...
	models/
	    UserModel.go
	    ...
backend/
	controllers/
		MainController.go
	...
	models/
		SomeModel.go
	...
...

Change:

app.yaml

runtime: go
api_version: go1

handlers:
- url: /images
  static_dir: static/images

- url: /css
  static_dir: static/css

- url: /js
  static_dir: static/js

- url: /fonts
  static_dir: static/fonts

- url: /.*
  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。请参考下面的示例。希望对你有所帮助。

app/
    app.go
    app.yaml
    static/
        ...
    frontend/views/
        home/
        *.html
    anotherfolder/
        *.html

示例目录定义:

- url: /s
  static_dir: s/
  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.

app/
    app.go
    app.yaml
    static/
        ...
    frontend/views/
        home/
        *.html
    anotherfolder/
        *.html

Example Directory Definition:

- url: /s
  static_dir: s/
  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:

确定