我一直在为我的项目使用 google-app-engine 提供给我的 url,即“projectname-id.appspot.com”。我之前从 GoDaddy 购买了一个自定义域,我按照以下链接中的步骤在 Google Cloud Platform 中验证域名的所有权,将域添加到我的 Google App Engine 项目,并将我在 GoDaddy 中的 DNS 设置更新为指向列出的 CNAME/服务器组合。 https://cloud.google.com/appengine/docs/go/console/using-custom-domains-and-ssl
当我访问我的自定义域时,会提供一个网站页面,但它始终是 404。“projectname-id.appspot.com”url 仍然正常工作,当我查看 Google App Engine 中的日志语句时,它收到来 self 的自定义域和 appspot 域的请求 - 这似乎表明域 dns 已正确更新。请参见下图,其中 404 来自自定义域,200 来自 appspot url:
我还有什么需要做的吗?后端是用 Go 编写的,我们使用的是 Mux 路由器。我是否需要修改我的 app.yaml 文件或以某种方式编辑我的路线?任何建议将不胜感激。
这里我包含了一些用于初始化我的服务器的代码片段:
应用.yaml
version: alpha-001
runtime: go
api_version: go1
handlers:
- url: /.*
script: _go_app
env_variables:
PRODUCTION: 'TRUE'
DATASTORE_DATASET: 'app-id'
DATASTORE_HOST: 'http://localhost:8043'
DATASTORE_EMULATOR_HOST: 'localhost:8043'
DATASTORE_PROJECT_ID: 'app-id'
GOOGLE_APPLICATION_CREDENTIALS: './app-string.json'
初始化 WebConsole(服务器):
func init() {
// Web Server for API Endpoints
flag.Parse()
var server *web_console.WebConsole
if prod := os.Getenv("PRODUCTION"); prod == "FALSE" {
server = web_console.NewWebConsole(false)
} else {
server = web_console.NewWebConsole(true)
}
server.Run()
}
WebConsole.go
type WebConsole struct {
prod bool
Mux *mux.Router
DbMap *gorp.DbMap
Client *datastore.Client
}
func NewWebConsole(prod bool) *WebConsole {
return &WebConsole{
prod: prod,
}
}
func (w *WebConsole) Run() {
w.dbInit()
w.routesInit()
}
func (w *WebConsole) dbInit() {
// Configure SQL connection
// Code removed for privacy reasons
}
func (w *WebConsole) routesInit() {
// Configure routes with Mux
w.Mux = mux.NewRouter()
api.AddCertChallengeApis(w.Mux)
// The path "/" matches everything not matched by some other path.
// Checkout: http://stackoverflow.com/questions/26581231/google-cloud-go-handler-without-router-gorilla-mux
// for more details
http.Handle("/", w.Mux)
}
路由的Api包文件
package api
import (
"github.com/gorilla/mux"
"google.golang.org/appengine"
"google.golang.org/appengine/log"
"net/http"
"strings"
)
func AddCertChallengeApis(r *mux.Router) {
r.Schemes("http")
r.HandleFunc("/", defaultHandler())
}
func defaultHandler() http.HandlerFunc {
return func(rw http.ResponseWriter, req *http.Request) {
// Construct new app engine context
c := appengine.NewContext(req)
log.Infof(c, "App ID: %v", appengine.AppID(c))
rw.Header().Set("Content-Type", "text/plain")
rw.Write([]byte("hi, welcome to my website yo"))
log.Infof(c, "Hit the website")
}
}
最佳答案
我以前在 appengine/go 上做过自定义域,没有其他你应该做的事情。不过,我会尝试删除这一行,
r.Schemes("http")
以防与 https 相关。
关于google-app-engine - Google App Engine 自定义域 - Go 中的路由,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36944573/