Gcloud函数部署找不到Golang模板文件 [英] Gcloud functions deployment doesn't find Golang template files

查看:90
本文介绍了Gcloud函数部署找不到Golang模板文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经编写了一些Golang代码,这些代码在我的本地计算机上进行测试时可以工作.当我将其部署为Google Cloud函数时,它失败了,因为它无法打开模板文件.代码失败的行是:

    t, err := template.New("list.gohtml").ParseFiles("list.gohtml")

此呼叫后err设置为open list.gohtml: no such file or directory

该文件与go源文件位于同一目录中,并且未在.gcloudignore.gitignore中列出. gcloud函数文档说,除非在其中一个忽略文件中列出,否则目录中的所有文件都将上载;如果我运行gcloud meta list-files-for-upload,则文件list.gohtml 会显示在显示的列表中.

是否有一些魔术文件夹布局可以实现此目的,或者是gcloud functions deploy命令的选项?

解决方案

基于@DazWilkin的回复,我现在在服务函数的开头调用以下函数.

与其将路径硬连接到模板文件名中(这会使它在本地测试时失败),还不如检查 import "os" const gcloudFuncSourceDir = "serverless_function_source_code" func fixDir() { fileInfo, err := os.Stat(gcloudFuncSourceDir) if err == nil && fileInfo.IsDir() { _ = os.Chdir(gcloudFuncSourceDir) } }

I have written some Golang code which works when tested on my local machine. When I deploy this as a Google Cloud function it fails because it cannot open a template file. The line of code failing is:

    t, err := template.New("list.gohtml").ParseFiles("list.gohtml")

After this call err is set to open list.gohtml: no such file or directory

The file is in the same directory as the go source file and is not listed in .gcloudignore or .gitignore. The gcloud functions documentation says all files in the directory will be uploaded unless listed in one of those ignore files and if I run gcloud meta list-files-for-upload then the file list.gohtml is included in the list displayed.

Is there some magic folder layout to make this work, or an option to the gcloud functions deploy command?

解决方案

Based on @DazWilkin's reply, I now call the function below at the start of the serving function.

Rather than hardwiring the path into the template file names (which would make it fail on when tested locally) this simply checks for the presence of the Gcloud source file directory below the current one, and if present, makes it the working directory, so file resolution will now happen exactly as it does when tested locally.

import "os"

const gcloudFuncSourceDir = "serverless_function_source_code"

func fixDir() {
    fileInfo, err := os.Stat(gcloudFuncSourceDir)
    if err == nil && fileInfo.IsDir() {
        _ = os.Chdir(gcloudFuncSourceDir)
    }
}

这篇关于Gcloud函数部署找不到Golang模板文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆