julienschmidt / httprouter

A high performance HTTP request router that scales well
https://pkg.go.dev/github.com/julienschmidt/httprouter
BSD 3-Clause "New" or "Revised" License
16.63k stars 1.47k forks source link

panic: conflict with wildcard route #6

Closed egonzo closed 10 years ago

egonzo commented 10 years ago

When I use these 2 distinct routes, I get a panic. Seems to me like this is a bug? Am I missing something?

router.GET("/api/v1/:user_id/buoys/:id/show/", Hello)
router.GET("/api/v1/:user_id/buoys/:name/search/", Hello)

Sample program below:

package main

import (
    "fmt"
    "log"
    "net/http"

    "github.com/OutCast-IO/httprouter"
)

func Index(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
    fmt.Fprint(w, "Welcome!\n")
}

func Hello(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
    fmt.Fprintf(w, "hello")
}

func main() {
    router := httprouter.New()

    router.GET("/api/v1/:user_id/buoys/:id/show/", Hello)
    router.GET("/api/v1/:user_id/buoys/:name/search/", Hello)

    log.Fatal(http.ListenAndServe(":8080", router))
}
julienschmidt commented 10 years ago

The reason is that you are using two different parameter names for the same path segment.

Using either

router.GET("/api/v1/:user_id/buoys/:id/show/", Hello)
router.GET("/api/v1/:user_id/buoys/:id/search/", Hello)

or

router.GET("/api/v1/:user_id/buoys/:name/show/", Hello)
router.GET("/api/v1/:user_id/buoys/:name/search/", Hello)

should work as expected.

I'll try to improve the error messages soon.