GAE Golang模板解析错误 - “不允许操作”

时间:2014-06-10 00:52:22

标签: html google-app-engine templates path go

我正在开发Google App Engine Go应用程序,需要在我的某个软件包中使用一些HTML模板。当前的文件结构是:

GOPATH/github.com/NAME/PROJECT/
                               app/
                                   app.go
                                   app.yaml
                               package/
                                       package.go
                                       Templates/
                                                 Template.html

要包含该包,我使用:

导入" github.com/NAME/PROJECT/package"

在package.go中,我尝试以各种方式解析我的Template.html文件:

//Template, err := template.ParseFiles("package/Templates/Template.html") //doesn't work - "The system cannot find the path specified."
//Template, err := template.ParseFiles("github.com/NAME/PROJECT/package/Templates/Template.html") //doesn't work - "The system cannot find the path specified."
//Template, err := template.ParseFiles("Templates/Template.html") //doesn't work - "The system cannot find the path specified."
//Template, err := template.ParseFiles("/Templates/Template.html") //doesn't work - "The system cannot find the path specified."
Template, err := template.ParseFiles("../package/Templates/Template.html") //works on desktop!

所以我选择适用于我的桌面测试环境的最后一个选项,将其上传到AppEngine,我得到一个新错误"操作不允许" ...

如何使用上面显示的适用于App Engine和桌面的文件配置解析HTML模板?

2 个答案:

答案 0 :(得分:2)

您需要在应用程序的根目录中使用app.yaml。 App Engine使用app.yaml的位置来确定与您的应用程序关联的文件。您想将此文件移至顶级。

例如,让我们说我们有类似的东西:

app.go
app.yaml
templates/t1
templates/t2

其中 app.yaml 是您通常拥有的应用程序, app.go 是:

package app

import (
    "html/template"
    "net/http"
)

var templates = template.Must(template.ParseGlob("templates/*"))

func init() {
    http.HandleFunc("/", rootHandler)
}

func rootHandler(w http.ResponseWriter, r *http.Request) {
    name := r.URL.Path[1:]    // drop the leading slash
    tmpl := templates.Lookup(name)
    if tmpl == nil {
        http.NotFound(w, r)
        return
    }
    tmpl.Execute(w, nil)
}

templates/t1以及templates/t2是适当的模板文件。然后,一旦我们有了这个,我们就可以在生成的webapp中访问t1/t2/,它应该在App Engine上提供服务和部署。

关键是让app.yaml位于应用程序的顶层目录中。另外需要注意的一点是:确保您尝试从动态应用程序中读取的所有文件都不会被静态提供或跳过。检查您的app.yaml。如果要静态提供文件,那么通常只允许前端查看文件,这意味着您的后端不会获胜。在部署期间,完全忽略跳过的文件。

答案 1 :(得分:-1)

您无法从GAE中的目录中读取内容。您可以将模板嵌入到go文件中,也可以使用google Blob Storage API。 https://developers.google.com/appengine/docs/go/blobstore/

相关问题