google-app-engine - Mux 和 http.HandleFunc 都适用于 Google App Engine 上的 helloworld 端点

标签 google-app-engine go router mux

我无法让名为 emptysuccess 的处理程序工作。我正在将 sendgrid 变成一个 appspot 微服务。迄今为止 调用

http://localhost:8080/emptysuccess

返回

404 page not found

这个行为是真实的 dev_appserver.py 和真正的 appspot.com。如何让/emptysuccess 工作?

package sendmail

import (
    "fmt"
    "github.com/sendgrid/sendgrid-go"
    "net/http"
    "google.golang.org/appengine"
    "github.com/gorilla/mux"
    "google.golang.org/appengine/log"

)

func main() {
    r := mux.Router{}
    r.HandleFunc("/send", sendhardcoded)
    r.HandleFunc("/emptysuccess", emptysuccess)
    //appengine.Main() // Starts the server to receive requests
}

func emptysuccess(w http.ResponseWriter, r *http.Request) {
    fmt.Println(w, "Hello Success")

}

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

    log.Infof(ctx, "Running Sendhardcoded")
    request := sendgrid.GetRequest("SG.OYPDF6hA.zk_XibKbJEUVLQfrkY-SBu5FejFakeC9ODFv1bE", "/v3/mail/send", "https://api.sendgrid.com")
    request.Method = "POST"
    request.Body = []byte(` {
    "personalizations": [
        {
            "to": [
                {
                    "email": "darian.hickman@gmail.info"
                }
            ],
            "subject": "Sending with SendGrid is Fun"
        }
    ],
    "from": {
        "email": "darian.hickman@ccc.com"
    },
    "content": [
        {
            "type": "text/plain",
            "value": "and easy to do anywhere, even with Go"
        }
    ]
}`)
    response, err := sendgrid.API(request)
    if err != nil {
        log.Errorf(ctx, "Problems: %v" ,err)
    } else {
        fmt.Println(w, response.StatusCode)
        fmt.Println(w, response.Body)
        fmt.Println(w, response.Headers)
        fmt.Println(w, "Sendmail should have worked")
    }
}

还要确保所有请求都转到 go 应用程序,我的 app.yaml 是:

runtime: go
api_version: go1.9

handlers:

- url: /static
  static_dir: static

- url: /.*
  script: _go_app
  login: required
  secure: always

最佳答案

这是最终起作用的: 1. 我像 mkopriva 指出的那样修复了多路复用器代码。 (注意:http.handleFunc 而不是 http.Handle 不起作用)。 2. 我不得不将 main() 更改为 init(),然后 App Engine 确认了我的多路复用器设置。

所以基本上我了解到 go 应用程序引擎本身无法处理多个处理程序的困难方法,并且我通过设置 mux 搞砸了。

工作代码:

package sendmail

import (
    "fmt"
    _"github.com/sendgrid/sendgrid-go"
    "net/http"
    "google.golang.org/appengine"
    "github.com/gorilla/mux"
    "google.golang.org/appengine/log"

)

func init() {
    r := mux.NewRouter()
    r.HandleFunc("/send", sendhardcoded)
    r.HandleFunc("/emptysuccess", emptysuccess)
    //appengine.Main() // Starts the server to receive requests
    http.Handle("/", r)
}

func emptysuccess(w http.ResponseWriter, r *http.Request) {
    fmt.Println(w, "Hello Success")

}

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

    log.Infof(ctx, "Running Sendhardcoded")

}

关于google-app-engine - Mux 和 http.HandleFunc 都适用于 Google App Engine 上的 helloworld 端点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49584588/

相关文章:

go - 如何将特定 channel 分配给空接口(interface) channel

go - 操作数中出现意外的 "="

javascript - 将 "Route children"和 "cloneElement"转换为 RouterV4

java - Google App Engine 和 dos.xml

java - 如何从使用 JDO 持久化的 Set 中删除项目?

google-app-engine - GAE Go “The request failed because the instance could not start successfully”

python - 如何使用 OAuth 从 Python 应用程序访问 Google App Engine 端点 API?

java - Go 是否会受到与 Java 相同的微妙内存泄漏的影响?

linux - 使用 ifconfig 进行持久更改

angular - 通过 routerLink 定位命名 socket 会添加无关的 "/"