html - GAE Golang template parsing error - "operation not permitted" -
i developing google app engine go application , need use html templates in 1 of packages. current file structure is:
gopath/github.com/name/project/ app/ app.go app.yaml package/ package.go templates/ template.html
to include package, use:
import "github.com/name/project/package"
inside of package.go, try parsing template.html file in various ways:
//template, err := template.parsefiles("package/templates/template.html") //doesn't work - "the system cannot find path specified." //template, err := template.parsefiles("github.com/name/project/package/templates/template.html") //doesn't work - "the system cannot find path specified." //template, err := template.parsefiles("templates/template.html") //doesn't work - "the system cannot find path specified." //template, err := template.parsefiles("/templates/template.html") //doesn't work - "the system cannot find path specified." template, err := template.parsefiles("../package/templates/template.html") //works on desktop!
so take last option works on desktop test environment, upload appengine , new error of "operation not permitted"...
how parse html templates such file configuration shown above works both on app engine , on desktop?
you need have app.yaml
@ root of application. app engine uses location of app.yaml
figure out files associated application. want move file top-level.
for example, let's have like:
app.go app.yaml templates/t1 templates/t2
where app.yaml you'd have 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 leading slash tmpl := templates.lookup(name) if tmpl == nil { http.notfound(w, r) return } tmpl.execute(w, nil) }
and templates/t1
, templates/t2
appropriate template files. once have this, can visit t1/
, t2/
in resulting webapp, , should serve , deploy fine on app engine.
the key have app.yaml
@ toplevel directory of application. 1 other caveat keep in mind: make sure files you're trying read dynamic application not statically served or skipped. check app.yaml
. if file statically served, front-end allowed see file, means back-end won't. , skipped files ignored altogether during deployment.
Comments
Post a Comment