英文:
How to parse HTML files in a Google Cloud Function?
问题
我有一个云函数,当用户加载入口点时,我试图在HTML页面上显示一些数据。当我在本地主机上运行时,这个函数非常好用,但是当它在线上在Google Cloud平台上时,HTML文件无法成功解析,我得到了以下错误:
"{"status":"error","code":500,"message":"Parsing Template Error","data":{"Op":"open","Path":"index.html","Err":2}}
Function panic: runtime error: invalid memory address or nil pointer dereference"
这是我的入口点函数:
func (c *Ctlr) Home(w http.ResponseWriter, r *http.Request) {
Log := prettylogs.Get()
ctlr := InitController(*Log)
defer Log.Exit(Log.Enter())
t, err := template.ParseFiles("index.html")
if err != nil {
Log.Error("Parsing Template Error: ", err)
sendResponse(w, r, http.StatusInternalServerError, "Parsing Template Error", err)
}
// 在这里调用内部函数,该函数将返回模板数据
Data, err := ctlr.GetTemplateData()
if err != nil {
sendResponse(w, r, http.StatusInternalServerError, "Error in fetching data", err)
}
type Transfer struct {
Data []AnaysisData
Account Accounts
}
var allData Transfer
allData.Data = Data
allData.Account = setSession(r)
err = t.Execute(w, allData)
if err != nil {
Log.Error("Template execution error: ", err)
sendResponse(w, r, http.StatusInternalServerError, "Template Execution Error", err)
}
return
}
请注意,这只是一个翻译,我无法提供关于代码的任何技术支持。
英文:
I have a cloud function that i a trying to display some data on a html page when ever a user loads the entry point,
This works very fine when i run it on local host, but when it is online in the google cloud platform, the html file does not parse successfully, i get this error
" {"status":"error","code":500,"message":"Parsing Template Error","data":{"Op":"open","Path":"index.html","Err":2}}
Function panic: runtime error: invalid memory address or nil pointer dereference"
Here is my entry point function,
func (c *Ctlr) Home(w http.ResponseWriter, r *http.Request) {
Log := prettylogs.Get()
ctlr := InitController(*Log)
defer Log.Exit(Log.Enter())
t, err := template.ParseFiles("index.html")
if err != nil {
Log.Error("Parsing Template Error: ", err)
sendResponse(w, r, http.StatusInternalServerError, "Parsing Template Error", err)
}
// Call the internal function here which will return the Template data
Data, err := ctlr.GetTemplateData()
if err != nil {
sendResponse(w, r, http.StatusInternalServerError, "Error in fetching data", err)
}
type Transfer struct {
Data []AnaysisData
Account Accounts
}
var allData Transfer
allData.Data = Data
allData.Account = setSession(r)
err = t.Execute(w, allData)
if err != nil {
Log.Error("Template execution error: ", err)
sendResponse(w, r, http.StatusInternalServerError, "Template Execution Error", err)
}
return
}
</details>
# 答案1
**得分**: 2
我记得,问题在于Cloud Functions部署将你的代码放在`serverless_function_source_code`子目录中,包括模板文件,而`main.go`(因此工作目录是根目录)。当`main.go`调用处理程序时,对`index.html`的引用是错误的(`./index.html`),应该是(`./serverless_function_source_code/index.html`)。
请参阅[文件系统](https://cloud.google.com/functions/docs/concepts/exec#functions-concepts-filesystem-go)。
如果你正在使用Go Modules(`go.mod`)(如果你使用的是GOPATH,则可能不同),那么你应该从该路径引用模板,例如:
```golang
t, err := template.ParseFiles("serverless_function_source_code/index.html")
英文:
IIRC, the issue is that the Cloud Functions deployment puts your deployed code in serverless_function_source_code
subdirectory, including the template file, while the main.go
(and thus the working directory is the root. When main.go
invokes the handler, the reference to index.html
is wrong (./index.html
) and should be (./serverless_function_source_code/index.html
).
See File system.
If you're using Go Modules (go.mod
) (I think this is different if you're using GOPATH), then you should reference the template from that path, i.e.
t, err := template.ParseFiles("serverless_function_source_code/index.html")
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论