I have deployed one simple app via go language in Cloud Function.
*A robots.txt file is also included when distributing the app.
In this regard, a simple app normally shows the image below.
But it shows 404 page not found even though the robots.txt file is normal.
Does anyone know if the robots.txt file itself can't be served by Cloud Function?
##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: /
Thank you in advance to those who have responded.
Comments
Cloud Functions is not a web server.
You cannot just add files and expect those will be served as you would you do it with an NGINX or Apache web server.
When you reach your CF endpoint the code you added will be executed and you'll get a result from that and in any case the files you add will be used by the code but not served.
I'd suggest to first understand what Cloud Functions is intended for and as an alternative you may want to go with App Engine Standard.
Another way to go is to use a workaround to handle the routes and that all stuff as guillaume shows in this article for Python + Flask:
And it is important what he mentions at the end:
Here's a sample for Go:
Cloud Functions isn't a webserver and you can't serve file directly like that. You need to process them in Go.
For that you need to know the code structure of the functions. All the original files are stored in
/workspace/serverless_function_source_code
directory. So, you can simply serve them using the URL path like thatAdd Comment