如何在Google Cloud Function中解析HTML文件?

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

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"

这是我的入口点函数:

  1. func (c *Ctlr) Home(w http.ResponseWriter, r *http.Request) {
  2. Log := prettylogs.Get()
  3. ctlr := InitController(*Log)
  4. defer Log.Exit(Log.Enter())
  5. t, err := template.ParseFiles("index.html")
  6. if err != nil {
  7. Log.Error("Parsing Template Error: ", err)
  8. sendResponse(w, r, http.StatusInternalServerError, "Parsing Template Error", err)
  9. }
  10. // 在这里调用内部函数,该函数将返回模板数据
  11. Data, err := ctlr.GetTemplateData()
  12. if err != nil {
  13. sendResponse(w, r, http.StatusInternalServerError, "Error in fetching data", err)
  14. }
  15. type Transfer struct {
  16. Data []AnaysisData
  17. Account Accounts
  18. }
  19. var allData Transfer
  20. allData.Data = Data
  21. allData.Account = setSession(r)
  22. err = t.Execute(w, allData)
  23. if err != nil {
  24. Log.Error("Template execution error: ", err)
  25. sendResponse(w, r, http.StatusInternalServerError, "Template Execution Error", err)
  26. }
  27. return
  28. }

请注意,这只是一个翻译,我无法提供关于代码的任何技术支持。

英文:

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,

  1. func (c *Ctlr) Home(w http.ResponseWriter, r *http.Request) {
  2. Log := prettylogs.Get()
  3. ctlr := InitController(*Log)
  4. defer Log.Exit(Log.Enter())
  5. t, err := template.ParseFiles("index.html")
  6. if err != nil {
  7. Log.Error("Parsing Template Error: ", err)
  8. sendResponse(w, r, http.StatusInternalServerError, "Parsing Template Error", err)
  9. }
  10. // Call the internal function here which will return the Template data
  11. Data, err := ctlr.GetTemplateData()
  12. if err != nil {
  13. sendResponse(w, r, http.StatusInternalServerError, "Error in fetching data", err)
  14. }
  15. type Transfer struct {
  16. Data []AnaysisData
  17. Account Accounts
  18. }
  19. var allData Transfer
  20. allData.Data = Data
  21. allData.Account = setSession(r)
  22. err = t.Execute(w, allData)
  23. if err != nil {
  24. Log.Error("Template execution error: ", err)
  25. sendResponse(w, r, http.StatusInternalServerError, "Template Execution Error", err)
  26. }
  27. return
  28. }
  29. </details>
  30. # 答案1
  31. **得分**: 2
  32. 我记得,问题在于Cloud Functions部署将你的代码放在`serverless_function_source_code`子目录中,包括模板文件,而`main.go`(因此工作目录是根目录)。当`main.go`调用处理程序时,对`index.html`的引用是错误的(`./index.html`),应该是(`./serverless_function_source_code/index.html`)。
  33. 请参阅[文件系统](https://cloud.google.com/functions/docs/concepts/exec#functions-concepts-filesystem-go)。
  34. 如果你正在使用Go Modules`go.mod`)(如果你使用的是GOPATH,则可能不同),那么你应该从该路径引用模板,例如:
  35. ```golang
  36. 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.

  1. t, err := template.ParseFiles(&quot;serverless_function_source_code/index.html&quot;)

huangapple
  • 本文由 发表于 2021年8月13日 23:23:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/68774839.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定