Go Handling HTTPS requests in Google App Engine

huangapple go评论88阅读模式
英文:

Go Handling HTTPS requests in Google App Engine

问题

在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请求,如果我不自己生成证书。

英文:

In GAE I just use a default domain name: https://*.appspot.com, so I don't need to generate self-signed certificates.

Google App Engine docs specify how app.yaml should be configured to serve SSL connections:

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

But to serve an HTTPS connection in Go I write the following code example where I need to specify the certificates' filenames:

import (
	"net/http"
)

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

I don't understand how in this case to serve SSL requests if I don't generate certificates myself.

答案1

得分: 3

你不需要在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")
}
英文:

You don't need to call http.ListenAndServeTLS on App Engine. If you have your app.yaml set up correctly, traffic will be served over SSL for you. A minimal App Engine app might be something like this:

package main

import (
    "fmt"
    "net/http"
)

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

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

huangapple
  • 本文由 发表于 2017年7月21日 16:49:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/45233208.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定