如果我在控制台中部署例子:你好世界,url/触发器就能工作。如果我从命令行部署,它在云函数控制台中看起来是完全相同的代码/属性,但是url是404。我找不出区别/问题。
如果从命令行以这种方式部署,则部署的触发器/url显示-- "404页未找到“,用于下面的hello world示例。
gcloud functions deploy hellogo --entry-point=HelloWorld --trigger-http --region=us-central1 --memory=128MB --runtime=go116 --allow-unauthenticated
// Package p contains an HTTP Cloud Function.
package p
import (
"encoding/json"
"fmt"
"html"
"io"
"log"
"net/http"
)
// HelloWorld prints the JSON encoded "message" field in the body
// of the request or "Hello, World!" if there isn't one.
func HelloWorld(w http.ResponseWriter, r *http.Request) {
var d struct {
Message string `json:"message"`
}
if err := json.NewDecoder(r.Body).Decode(&d); err != nil {
switch err {
case io.EOF:
fmt.Fprint(w, "Hello World!")
return
default:
log.Printf("json.NewDecoder: %v", err)
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
return
}
}
if d.Message == "" {
fmt.Fprint(w, "Hello World!")
return
}
fmt.Fprint(w, html.EscapeString(d.Message))
}
发布于 2022-02-15 11:17:14
谢谢大家,我被困的项目比这个函数有更多的代码。在试图在大型项目中部署单个函数/文件之后,我走上了这条道路。如果我简化为只有一个hello.go和go.mod的文件夹,那么它确实可以:-/从命令行部署它:
gcloud functions deploy hellogo --entry-point=HelloWorld --trigger-http --region=us-central1 --memory=128MB --runtime=go116 --allow-unauthenticated
// go.mod
module github.com/nickfoden/hello
go 1.16感谢您的快速答复和帮助。与其尝试在现有项目中使用更大的go.sum、多个文件夹、现有的服务器/api等创建单个函数,我将从这里开始使用一个带有云函数的文件,并在此基础上构建,并查看在哪一点/是否再次陷入卡住状态。
https://stackoverflow.com/questions/71087844
复制相似问题