【问题标题】:Cloud Function robots.txt 404 page not found issueCloud Function robots.txt 404 页面未找到问题
【发布时间】: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


【解决方案1】:

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)
}

【讨论】:

  • 感谢您的友好回复。我们将根据您的回答对其进行测试。
【解决方案2】:

Cloud Functions 不是网络服务器

您不能只添加文件并期望这些文件会像使用 NGINX 或 Apache Web 服务器那样提供。

当您到达您的 CF 端点时,您添加的代码将被执行,您将从中获得结果,并且无论如何您添加的文件将被代码使用但不会被提供。

我建议首先了解 Cloud Functions 是什么 intended for,作为替代方案,您可能想要使用 App Engine Standard

另一种方法是使用解决方法来处理路由,并且 guillaume 中显示的所有内容都显示在 Python + Flask 的 article 中:

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

他最后提到的很重要:

但是,请记住,这是一种变通方法,甚至是一种 hack,而 Cloud Functions 并不是为此目的而设计的。

这是一个示例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"))
}

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2014-09-12
  • 1970-01-01
  • 2015-07-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多