【发布时间】:2021-10-27 08:31:42
【问题描述】:
我在 Cloud Function 中通过 go 语言部署了一个简单的应用程序。
*分发应用时还包含 robots.txt 文件。
但即使 robots.txt 文件正常,它也会显示 404 页面未找到。
有谁知道云函数不能提供 robots.txt 文件本身吗?
##Function.go
// 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))
}
##go.mod
module example.com/cloudfunction
##robots.txt
User-agent: *
Disallow: /
User-agent: Mediapartners-Google
Allow: /
提前感谢那些回复的人。
【问题讨论】:
-
可以在这里分享你的云功能代码
-
@Prany 感谢您的回复。我在本帖中输入了功能码,请查收。
-
我Golang不好但是看不到txt文件的文件路径。在他们试图读取文件的地方看到这个答案 - stackoverflow.com/questions/43117124/…
-
Cloud Functions 不是 Web 服务器。你为什么关心 robots.txt?如果您的端点是公共的,您将收到大量对不存在的 URL 的请求。如果您确实想要处理发送 robots.txt 的响应,请查看 @Ferregina 的回答,了解如何将路由添加到您的应用程序。此外,请查看充当 Web 服务器与 API 服务器的应用程序之间的差异。
标签: google-cloud-platform google-cloud-functions robots.txt