Cloud Function robots.txt 404 页面未找到问题
Cloud Function robots.txt 404 page not found issue
我在 Cloud Function 中通过 go 语言部署了一个简单的应用程序。
*分发应用程序时还包含一个 robots.txt 文件。
在这方面,一个简单的应用程序通常会显示下图。
但是robots.txt文件正常,却显示404页面未找到。
有人知道 Cloud Function 是否无法提供 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: /
预先感谢那些回复的人。
Cloud Functions 不是 Web 服务器。
您不能只添加文件并期望这些文件会像您使用 NGINX 或 Apache 网络服务器那样得到服务。
当您到达您的 CF 端点时,您添加的代码将被执行,您将从中得到一个结果,并且在任何情况下,您添加的文件将被代码使用但不会被提供。
我建议首先了解 Cloud Functions 是什么 intended for and as an alternative you may want to go with App Engine Standard。
另一种方法是使用变通方法来处理路由,并且 guillaume 中显示的所有内容 article for Python + Flask :
from flask import Flask, request
#Define an internal Flask app
app = Flask("internal")
#Define the internal path, idiomatic Flask definition
@app.route('/user/<string:id>', methods=['GET', 'POST'])
def users(id):
print(id)
return id, 200
#Comply with Cloud Functions code structure for entry point
def my_function(request):
#Create a new app context for the internal app
internal_ctx = app.test_request_context(path=request.full_path,
method=request.method)
#Copy main request data from original request
#According to your context, parts can be missing. Adapt here!
internal_ctx.request.data = request.data
internal_ctx.request.headers = request.headers
#Activate the context
internal_ctx.push()
#Dispatch the request to the internal app and get the result
return_value = app.full_dispatch_request()
#Offload the context
internal_ctx.pop()
#Return the result of the internal app routing and processing
return return_value
最后他提到的很重要:
However, keep in mind that’s a workaround, even a hack, and Cloud Functions aren’t designed for this purpose.
这是一个示例 for Go:
package function
import (
"net/http"
)
var mux = newMux()
//F represents cloud function entry point
func F(w http.ResponseWriter, r *http.Request) {
mux.ServeHTTP(w, r)
}
func newMux() *http.ServeMux {
mux := http.NewServeMux()
mux.HandleFunc("/one", one)
mux.HandleFunc("/two", two)
mux.HandleFunc("/subroute/three", three)
return mux
}
func one(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("hello from one"))
}
func two(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("hello from two"))
}
func three(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("hello from three"))
}
Cloud Functions 不是网络服务器,您不能像那样直接提供文件。你需要在 Go 中处理它们。
为此,您需要了解函数的代码结构。所有原始文件都存储在 /workspace/serverless_function_source_code
目录中。因此,您可以简单地使用 URL 路径为他们服务
var functionSourceCodeDir = "/workspace/serverless_function_source_code"
func ServeFile(w http.ResponseWriter, r *http.Request) {
file := r.URL.Path
if file == "/" {
w.WriteHeader(http.StatusBadRequest)
fmt.Fprintf(w, "you must provide a file pathname")
return
}
http.ServeFile(w, r, functionSourceCodeDir+file)
}
我在 Cloud Function 中通过 go 语言部署了一个简单的应用程序。
*分发应用程序时还包含一个 robots.txt 文件。
在这方面,一个简单的应用程序通常会显示下图。
但是robots.txt文件正常,却显示404页面未找到。
有人知道 Cloud Function 是否无法提供 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: /
预先感谢那些回复的人。
Cloud Functions 不是 Web 服务器。
您不能只添加文件并期望这些文件会像您使用 NGINX 或 Apache 网络服务器那样得到服务。
当您到达您的 CF 端点时,您添加的代码将被执行,您将从中得到一个结果,并且在任何情况下,您添加的文件将被代码使用但不会被提供。
我建议首先了解 Cloud Functions 是什么 intended for and as an alternative you may want to go with App Engine Standard。
另一种方法是使用变通方法来处理路由,并且 guillaume 中显示的所有内容 article for Python + Flask :
from flask import Flask, request
#Define an internal Flask app
app = Flask("internal")
#Define the internal path, idiomatic Flask definition
@app.route('/user/<string:id>', methods=['GET', 'POST'])
def users(id):
print(id)
return id, 200
#Comply with Cloud Functions code structure for entry point
def my_function(request):
#Create a new app context for the internal app
internal_ctx = app.test_request_context(path=request.full_path,
method=request.method)
#Copy main request data from original request
#According to your context, parts can be missing. Adapt here!
internal_ctx.request.data = request.data
internal_ctx.request.headers = request.headers
#Activate the context
internal_ctx.push()
#Dispatch the request to the internal app and get the result
return_value = app.full_dispatch_request()
#Offload the context
internal_ctx.pop()
#Return the result of the internal app routing and processing
return return_value
最后他提到的很重要:
However, keep in mind that’s a workaround, even a hack, and Cloud Functions aren’t designed for this purpose.
这是一个示例 for Go:
package function
import (
"net/http"
)
var mux = newMux()
//F represents cloud function entry point
func F(w http.ResponseWriter, r *http.Request) {
mux.ServeHTTP(w, r)
}
func newMux() *http.ServeMux {
mux := http.NewServeMux()
mux.HandleFunc("/one", one)
mux.HandleFunc("/two", two)
mux.HandleFunc("/subroute/three", three)
return mux
}
func one(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("hello from one"))
}
func two(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("hello from two"))
}
func three(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("hello from three"))
}
Cloud Functions 不是网络服务器,您不能像那样直接提供文件。你需要在 Go 中处理它们。
为此,您需要了解函数的代码结构。所有原始文件都存储在 /workspace/serverless_function_source_code
目录中。因此,您可以简单地使用 URL 路径为他们服务
var functionSourceCodeDir = "/workspace/serverless_function_source_code"
func ServeFile(w http.ResponseWriter, r *http.Request) {
file := r.URL.Path
if file == "/" {
w.WriteHeader(http.StatusBadRequest)
fmt.Fprintf(w, "you must provide a file pathname")
return
}
http.ServeFile(w, r, functionSourceCodeDir+file)
}