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

报告相同问题?

悬赏问题

  • ¥15 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 求螺旋焊缝的图像处理
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面
  • ¥15 itunes恢复数据最后一步发生错误
  • ¥15 关于#windows#的问题:2024年5月15日的win11更新后资源管理器没有地址栏了顶部的地址栏和文件搜索都消失了