去Gae NewContext

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

Go Gae NewContext

问题

我正在使用GAE上的Go语言。我的一个任务是发送电子邮件。我正在成功地使用这个文档进行测试。

然而,如果我不想使用请求处理程序:

func confirm(w http.ResponseWriter, r *http.Request) {
    c := appengine.NewContext(r)
    ...
}

而是通过一个本地函数发送电子邮件:

func confirm() {
    ...
}

在Go语言中,有没有办法在GAE上实现这个?我认为我想要的是一种调用的方式:

c := appengine.NewContext(r)

但没有请求处理程序,或者以任何可能的方式绕过它(我正在阅读的可能不可能)。

我在考虑一个解决方法可能是从我的应用程序向我的应用程序发出HTTP请求,但这太丑陋了!

resp, err := http.Get("http://localhost:8080/sendMail?to=...")

注意:尝试这个丑陋的解决方法后,我得到了以下错误:
在App Engine中,http.DefaultTransporthttp.DefaultClient不可用。请参阅https://cloud.google.com/appengine/docs/go/urlfetch/

因此,这个解决方法实际上不是一个解决方法,因为urlfetch(GAE使用http.GET的方式)仍然需要c := appengine.NewContext(r)

英文:

I'm using Go on GAE. One of my tasks is sending an email. I am using this documentation successfully to test.

However, what if I do not want a request handler:

func confirm(w http.ResponseWriter, r *http.Request) {
        c := appengine.NewContext(r)
                    ...

But rather, send the email through a local func:

func confirm() {
...

Is there any way in Go to do this with GAE? I think what Im looking for is a way to call:

c := appengine.NewContext(r)

But without a request handler, or bypass it in any way possible (which Im reading is probably not possible)

I am thinking a work around could be making an http request from my application to my application - but wow is that ugly!

resp, err := http.Get("http://localhost:8080/sendMail?to=...")

NOTE: After attempting this -- ugly work around I get:
http.DefaultTransport and http.DefaultClient are not available in App Engine. See https://cloud.google.com/appengine/docs/go/urlfetch/

So this workaround is in fact NOT a work around in that urlfetch which is how GAE uses http.GET once again... requires c := appengine.NewContext(r)

答案1

得分: 2

你需要一个appengine.Context来与外部服务进行交互,包括电子邮件和urlfetch。你需要将Context实例传递给你的confirm函数。

英文:

You need an appengine.Context to interact with external services, including email and urlfetch. You will have to pass the Context instance into your confirm function.

huangapple
  • 本文由 发表于 2015年4月11日 12:42:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/29574207.html
匿名

发表评论

匿名网友

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

确定