英文:
template parsing error - "operation not permitted"
问题
我正在开发一个使用Google App Engine Go的应用程序,并且需要在其中一个包中使用一些HTML模板。当前的文件结构如下:
GOPATH/github.com/NAME/PROJECT/
app/
app.go
app.yaml
package/
package.go
Templates/
Template.html
为了包含这个包,我使用:
import "github.com/NAME/PROJECT/package"
在package.go中,我尝试以不同的方式解析我的Template.html文件:
//Template, err := template.ParseFiles("package/Templates/Template.html") //不起作用 - "系统找不到指定的路径。"
//Template, err := template.ParseFiles("github.com/NAME/PROJECT/package/Templates/Template.html") //不起作用 - "系统找不到指定的路径。"
//Template, err := template.ParseFiles("Templates/Template.html") //不起作用 - "系统找不到指定的路径。"
//Template, err := template.ParseFiles("/Templates/Template.html") //不起作用 - "系统找不到指定的路径。"
Template, err := template.ParseFiles("../package/Templates/Template.html") //在桌面上工作!
所以我选择了在我的桌面测试环境中工作的最后一个选项,将其上传到AppEngine后,我得到了一个新的错误"operation not permitted"...
如何解析上述文件配置的HTML模板,以便在App Engine和桌面上都能正常工作?
英文:
I am developing a Google App Engine Go application and need to use some HTML templates in one of my packages. The current file structure is:
GOPATH/github.com/NAME/PROJECT/
app/
app.go
app.yaml
package/
package.go
Templates/
Template.html
To include the package, I use:
import "github.com/NAME/PROJECT/package"
Inside of package.go, I try parsing my Template.html file in various ways:
//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!
So I take the last option that works on my desktop test environment, upload that to AppEngine and I get a new error of "operation not permitted"...
How do I parse HTML templates with such file configuration as shown above that works both on App Engine and on desktop?
答案1
得分: 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:] // 去掉前导斜杠
tmpl := templates.Lookup(name)
if tmpl == nil {
http.NotFound(w, r)
return
}
tmpl.Execute(w, nil)
}
templates/t1
和templates/t2
是相应的模板文件。然后,我们可以在生成的Web应用程序中访问t1/
和t2/
,并且它应该可以在App Engine上正常提供和部署。
关键是将app.yaml
放在应用程序的顶级目录下。还要注意的一点是,确保你尝试从动态应用程序中读取的任何文件都没有被静态提供或跳过。检查你的app.yaml
。如果一个文件要被静态提供,通常只有前端可以看到该文件,这意味着你的后端看不到。而被跳过的文件在部署过程中完全被忽略。
英文:
You need to have the app.yaml
at the root of your application. App Engine uses the location of the app.yaml
to figure out what files are associated with your application. You want to move this file to the top-level.
For example, let's say that we have something like:
app.go
app.yaml
templates/t1
templates/t2
where app.yaml is what you'd normally have for your application, app.go is:
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)
}
and templates/t1
and templates/t2
are appropriate template files. Then once we have this, we can visit t1/
and t2/
in the resulting webapp, and it should serve and deploy fine on App Engine.
The key is to have the app.yaml
at the toplevel directory of your application. One other caveat to keep in mind: make sure that any files you're trying to read from your dynamic application are not statically served or skipped. Check your app.yaml
. If a file is to be statically served, then usually only the front-end is allowed to see the file, which means your back-end won't. And skipped files are just ignored altogether during deployment.
答案2
得分: -1
你无法在GAE中从目录中读取内容。你可以将模板嵌入到一个go文件中,或者使用Google Blob Storage API。https://developers.google.com/appengine/docs/go/blobstore/
英文:
You can't read from a directory in GAE. Either you embed the Template inside a go file or you use the google Blob Storage API. https://developers.google.com/appengine/docs/go/blobstore/
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论