代码之家  ›  专栏  ›  技术社区  ›  Darian Hickman

Mux或http。HandleFunc甚至还在为Google App Engine上的helloworld端点工作

  •  0
  • Darian Hickman  · 技术社区  · 6 年前

    我无法让名为EmptySucces的处理程序工作。我正在将sendgrid转变为appspot微服务。目前为止 打电话给

    http://localhost:8080/emptysuccess

    退货

    404找不到页面

    此行为为true dev\u appserver。py和真实的appspot。com。如何获得/清空成功?

    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 my app。yaml是:

    runtime: go
    api_version: go1.9
    
    handlers:
    
    - url: /static
      static_dir: static
    
    - url: /.*
      script: _go_app
      login: required
      secure: always
    
    1 回复  |  直到 6 年前
        1
  •  2
  •   Darian Hickman    6 年前

    以下是最终奏效的方法: 我修复了mkopriva指出的mux代码。(注意:http.handleFunc而不是http.Handle不起作用)。 我不得不将main()改为init(),然后应用引擎确认了我的mux设置。

    因此,基本上我学到了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")
    
    }