0

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
Magiq
  • 116
  • 3

1 Answers1

0

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()
}
sh.seo
  • 1,482
  • 13
  • 20
  • I need ServeHTTP define his routes, since it's module responsobility to set his own routes – Magiq Mar 19 '19 at 19:07
  • You need not ServeHTTP for routes. goji' middleware.SubRouter has ServeHTTP. middlewre.SubRouter code is https://github.com/zenazn/goji/blob/4a0a089f56dffba35ba29a37303bbc3e3e496f96/web/middleware/subrouter.go. – sh.seo Mar 20 '19 at 01:06