dparivln22034 2018-03-31 05:31
浏览 50
已采纳

Mux或http.HandleFunc都无法在Google App Engine上用于helloworld端点

I can't get a handler called emptysuccess to work. I'm turning sendgrid into an appspot micro-service. So far A call to

http://localhost:8080/emptysuccess

Returns

404 page not found

This behavior is true dev_appserver.py and also for the real appspot.com. How to get /emptysuccess to work?

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

Also to make sure all requests go to the go app my app.yaml is:

runtime: go
api_version: go1.9

handlers:

- url: /static
  static_dir: static

- url: /.*
  script: _go_app
  login: required
  secure: always
  • 写回答

1条回答 默认 最新

  • douyan1896 2018-04-01 22:45
    关注

    Here is what ultimately worked: 1. I fixed mux code like mkopriva pointed out. (Caution: http.handleFunc instead of http.Handle doesn't work). 2. I had to change main() to init() and then app engine acknowledged my mux setup.

    So basically I learned the hard way that go app engine on its own can't handle multiple handlers and I bungled through setting up mux.

    Working code:

    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")
    
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容