Go在Google App Engine中处理HTTPS请求

时间:2017-07-21 08:49:18

标签: google-app-engine ssl go https ssl-certificate

在GAE中,我只使用默认域名: https://*.appspot.com ,因此我不需要生成自签名证书。

Google App Engine文档指定应如何配置app.yaml以提供SSL连接:

https://cloud.google.com/appengine/docs/standard/go/config/appref#handlers_secure

但是要在Go中提供HTTPS连接,请编写以下代码示例,我需要在其中指定证书'文件名:

import (
    "net/http"
)

func main() {
    go http.ListenAndServeTLS(Address, "cert.pem", "key.pem", nil)
}

如果我自己不生成证书,我不明白在这种情况下如何提供SSL请求。

1 个答案:

答案 0 :(得分:2)

您无需在App Engine上调用http.ListenAndServeTLS。如果您已正确设置app.yaml,则会通过SSL为您提供流量。最小的App Engine应用程序可能是这样的:

package main

import (
    "fmt"
    "net/http"
)

func init() {
    http.HandleFunc("/", handler)
}

func handler(w http.ResponseWriter, r *http.Request) {
    fmt.Fprint(w, "Hi")
}