Reputation: 222
I've got a Google App Engine app using Goji and defined the following routes:
func init() {
mux := web.New()
http.Handle("/api/list", mux)
mux.Use(middleware.EnvInit)
mux.Use(middleware.Logger)
mux.Get( "/api/list", list.HandleListGetAll)
mux.Post("/api/list", list.HandleListNewList)
mux.Get( "/api/list/:id", list.HandleListGetSingle)
}
I can GET and POST to /api/list but GETing /api/list/0 just results in a 404, I think from Goji itself.
Does anyone have any idea what I'm doing wrong?
Upvotes: 0
Views: 336
Reputation: 24300
The 404 isn't being returned by Goji—any 404 from Goji should be logged to console (stdout) by the Logger
middleware. The upstream handler passing requests to the Goji router is bouncing anything that isn't /api/list
.
You can fix this with a more lenient match:
package main
import (
"fmt"
"net/http"
"github.com/zenazn/goji/web"
"github.com/zenazn/goji/web/middleware"
)
func main() {
mux := web.New()
http.Handle("/api/", mux)
mux.Use(middleware.EnvInit)
mux.Use(middleware.Logger)
mux.Get("/api/list/:id", debugHandler)
mux.Get("/api/list", debugHandler)
mux.Post("/api/list", debugHandler)
// If Goji's router 404's, we should call our custom handler, and it
// should also be apparent in the logs.
mux.NotFound(notFoundHandler)
http.ListenAndServe(":8000", nil)
}
func debugHandler(c web.C, w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "%v\n", r.URL.Path)
}
func notFoundHandler(c web.C, w http.ResponseWriter, r *http.Request) {
http.Error(w, fmt.Sprintf("Goji 404: %s", r.URL.Path), 404)
}
Note that I've changed the upstream http.Handle
to http.Handle("/api/", mux)
to provide a more lenient match. The net/http
router is very simple and because /api/list/131313
(e.g. with an id) doesn't match /api/list
it 404's before it even hits the Goji mux instance you created.
Hope that helps.
Upvotes: 1