【问题标题】:Google App Engine Custom Domain - Routing in GoGoogle App Engine 自定义域 - Go 中的路由
【发布时间】:2016-08-24 23:35:30
【问题描述】:

我一直在为我的项目使用 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”网址仍然可以正常工作,并且当我查看 Google App Engine 中的日志语句时,它接收来自我的自定义域和应用程序域的请求 - 这似乎表明域 dns 已正确更新。见下图,其中 404 来自自定义域,200 来自 apppot url:

Google App Engine Logs

我还有什么需要做的吗?后端是用 Go 编写的,我们使用的是 Mux 路由器。我是否需要修改我的 app.yaml 文件或以某种方式编辑我的路线?任何建议将不胜感激。

这里我包含了一些用于初始化服务器的代码 sn-ps:

App.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")
    }
}

【问题讨论】:

    标签: google-app-engine go dns subdomain


    【解决方案1】:

    我之前在 appengine/go 上做过自定义域,你不需要做任何其他事情。不过,我会尝试删除这条线,

        r.Schemes("http")
    

    如果它与 https 相关。

    【讨论】:

    • 我是否必须在 GoDaddy DNS 注册商端添加任何类型的转发(CNAME 条目除外)?你有任何可共享的、有效的 gae 自定义域代码示例吗?
    猜你喜欢
    • 2017-04-08
    • 1970-01-01
    • 2012-06-10
    • 2014-07-05
    • 1970-01-01
    • 1970-01-01
    • 2015-11-07
    • 2019-08-29
    • 2014-11-14
    相关资源
    最近更新 更多