Darian Hickman
Darian Hickman

Reputation: 909

Mux nor http.HandleFunc is working for even a helloworld endpoint on Google App Engine

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": "[email protected]"
                }
            ],
            "subject": "Sending with SendGrid is Fun"
        }
    ],
    "from": {
        "email": "[email protected]"
    },
    "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

Upvotes: 0

Views: 623

Answers (1)

Darian Hickman
Darian Hickman

Reputation: 909

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

}

Upvotes: 2

Related Questions