Magiq
Magiq

Reputation: 116

Goji SubRouter returns 404

Here is some code

package main

import (
    "fmt"
    "net/http"

    "github.com/zenazn/goji"
    "github.com/zenazn/goji/web"
    "github.com/zenazn/goji/web/middleware"
)

type handler struct{}

func (h handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
    subMux := web.New()
    subMux.Use(middleware.SubRouter)
    subMux.Post("/:id", func(c web.C, w http.ResponseWriter, r *http.Request) {
        fmt.Fprint(w, "OK")
    })

    subMux.ServeHTTP(w, r)
}

func main() {
    goji.Handle("/inner/*", handler{})
    goji.Serve()
}

The main idea around this to encapsulate handler routes and use standart net/http Handler interface. So why the following code produce 404 and not use subrouter ?

curl -X POST http://localhost:8000/inner/5
404 page not found

Upvotes: 0

Views: 158

Answers (1)

sh.seo
sh.seo

Reputation: 1610

If you change it like this, you can get post data.

package main

import (
    "fmt"
    "net/http"

    "github.com/zenazn/goji"
    "github.com/zenazn/goji/web"
    "github.com/zenazn/goji/web/middleware"
)

type handler struct{}

func (h handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
    fmt.Fprint(w, "OK")
}

func main() {
    subMux := web.New()
    subMux.Use(middleware.SubRouter)
    subMux.Post("/:id", handler{})

    goji.Handle("/inner/*", subMux)
    goji.Serve()
}

Upvotes: 0

Related Questions