我正在尝试使用 gcp 上的 zip 文件部署由 gin/gonic 制作的云功能。但它没有被部署,我面临一个错误。
我的代码文件是
HelloHTTP.go:
package hello
import (
"fmt"
"html"
"net/http"
"github.com/gin-gonic/gin"
)
// helloHTTP is a Gin handler function.
func HelloHTTP(c *gin.Context) {
var d struct {
Name string `json:"name"`
}
if err := c.BindJSON(&d); err != nil {
c.String(http.StatusOK, "Hello, World!")
return
}
if d.Name == "" {
c.String(http.StatusOK, "Hello, World!")
return
}
c.String(http.StatusOK, fmt.Sprintf("Hello, %s!", html.EscapeString(d.Name)))
}
go.mod:
module github.com/cap
go 1.21.1
require github.com/gin-gonic/gin v1.9.1
require (
github.com/bytedance/sonic v1.9.1 // indirect
github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311 // indirect
github.com/gabriel-vasile/mimetype v1.4.2 // indirect
github.com/gin-contrib/sse v0.1.0 // indirect
github.com/go-playground/locales v0.14.1 // indirect
github.com/go-playground/universal-translator v0.18.1 // indirect
github.com/go-playground/validator/v10 v10.14.0 // indirect
github.com/goccy/go-json v0.10.2 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/klauspost/cpuid/v2 v2.2.4 // indirect
github.com/leodido/go-urn v1.2.4 // indirect
github.com/mattn/go-isatty v0.0.19 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/pelletier/go-toml/v2 v2.0.8 // indirect
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
github.com/ugorji/go/codec v1.2.11 // indirect
golang.org/x/arch v0.3.0 // indirect
golang.org/x/crypto v0.9.0 // indirect
golang.org/x/net v0.10.0 // indirect
golang.org/x/sys v0.8.0 // indirect
golang.org/x/text v0.9.0 // indirect
google.golang.org/protobuf v1.30.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
我面临以下错误。
无法创建或更新 Cloud Run 服务您好,容器运行状况检查失败。修订版“hello-00001-gef”尚未准备好,无法提供流量。用户提供的容器无法启动并侦听 PORT=8080 环境变量提供的端口。此版本的日志可能包含更多信息。日志网址:https://console.cloud.google.com/logs/viewer?project=winter-vim-412404&resource=cloud_run_revision/service_name/hello/revision_name/hello-00001-gef&advancedFilter=resource.type%3D%22cloud_run_revision% 22%0Aresource.labels.service_name%3D%22hello%22%0Aresource.labels.revision_name%3D%22hello-00001-gef%22 有关更多故障排除指南,请参阅 https://cloud.google.com/run/docs /故障排除#容器启动失败
找不到该功能的Cloud Run服务项目/winter-vim-412404/locations/us-central1/services/hello。该功能将无法正常工作。请重新部署。
请帮我解决这个问题。
我试图部署此功能,但它没有得到部署。
您需要启动并运行gin路由器。您的示例的 main() 函数示例可能如下所示:
func main() {
router := gin.Default()
router.POST("/", HelloHTTP)
router.Run(":8080")
}
您想要做的事情是可能的,但您需要使用
github.com/GoogleCloudPlatform/functions-framework-go/functions
,如Google文档中here所述
当您上传 zip 文件时,会触发 Google Build,此包对于在代码中的端点与您为此 Google 功能设置的“入口点”之间建立链接至关重要。
我根据之前提出的问题创建了此示例代码:在 Google Cloud Functions 中初始化 Go Gin 服务器
package hello
import (
"net/http"
"github.com/gin-gonic/gin"
"github.com/GoogleCloudPlatform/functions-framework-go/functions"
)
func init(){
route := gin.Default()
route.GET("/hi", HelloHTTP)
route.GET("/bye", ByeHTTP)
functions.HTTP("HelloHTTP", route.Handler().ServeHTTP)
}
// helloHTTP is a Gin handler function.
func HelloHTTP(c *gin.Context) {
c.String(http.StatusOK, "Hello, World!")
}
// ByeHTTP is a Gin handler function.
func ByeHTTP(c *gin.Context) {
c.String(http.StatusOK, "Bye Bye!")
}
...现在您的 Google 函数有 1 个连接到的主端点
HelloHTTP
和 2 个子端点 hi
和 bye
。所以你的端点看起来像 https://.../HelloHTTP/hi
或 https://.../HelloHTTP/bye
(我添加了 2 个子端点以更好地说明路由在这里如何工作)