beego / mux Goto Github PK
View Code? Open in Web Editor NEWA high performance and powerful trie based url path router for Go.
License: Apache License 2.0
A high performance and powerful trie based url path router for Go.
License: Apache License 2.0
there are two conflict router:
GET /v1/team/:tid/duty
GET /v1/team/:svc/info
the secode route will not match
package main
import (
"log"
"net/http"
"github.com/beego/mux"
)
func main() {
m := mux.New()
m.Get("/v1/team/:tid/duty", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("hello, beego mux\n"))
})
m.Get("/v1/team/:svc/info", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("hello, beego mux 2\n"))
})
log.Fatal(http.ListenAndServe("127.0.0.1:2222", m))
}
curl:
mozhata@pc:try$ curl http://127.0.0.1:2222/v1/team/ididid/duty
hello, beego mux
mozhata@pc:try$ curl http://127.0.0.1:2222/v1/team/ididid/info
"/v1/team/ididid/info" not implemented
but replace :svc
to :tid
, these two routes all works
My test enviroment:
os:
mac os x and ubuntu 16.04
go:
$go version
go version go1.8 darwin/amd64
beego/mux version:
commit 38fddcbf1be964bbc26c0cf7b91157719cfdb56f
test code:
package main
import (
"log"
"net/http"
"github.com/beego/mux"
)
func Register(method string, pattern string, handler http.HandlerFunc) {
mx.Handler(method, pattern, handler)
}
func addServer(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("addServer"))
}
func getServer(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("getServer"))
}
func delServer(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("delServer"))
}
var mx *mux.Mux
func main() {
mx = mux.New()
Register("POST", "/api/v1/servers/:server_name", addServer)
Register("DELETE", "/api/v1/servers/:server_name", delServer)
Register("GET", "/api/v1/servers/:server_name", getServer)
log.Fatal(http.ListenAndServe("127.0.0.1:9999", mx))
}
result:
$curl -X POST http://127.0.0.1:9999/api/v1/servers/yy-pd.com
addServer
$curl http://127.0.0.1:9999/api/v1/servers/yy-pd.com
"GET" not allowed in "/api/v1/servers/yy-pd.com"
$curl -X DELETE http://127.0.0.1:9999/api/v1/servers/yy-pd.com
"DELETE" not allowed in "/api/v1/servers/yy-pd.com"
expected: all curl command should return right result.
Is there any plan to support middleware?
RT。
如果继续的话,能增加表单,或者 query 参数的读取吗?
I add some test code in the test file https://github.com/beego/mux/blob/master/mux_test.go#L21
t.Run("router with wildcard pattern2", func(t *testing.T) {
assert := assert.New(t)
mux := New()
mux.Get("/a/:a/b", func(w http.ResponseWriter, r *http.Request) {
params := Params(r)
w.WriteHeader(200)
w.Write([]byte(params[":a"]))
})
mux.Get("/a/:a/b/:b", func(w http.ResponseWriter, r *http.Request) {
params := Params(r)
w.WriteHeader(200)
w.Write([]byte(params[":a"]))
})
mux.Get("/a/:a/b/:b/c", func(w http.ResponseWriter, r *http.Request) {
params := Params(r)
w.WriteHeader(200)
w.Write([]byte(params[":a"]))
})
ts := httptest.NewServer(mux)
defer ts.Close()
res, err := Request("GET", ts.URL+"/a/a1/b/b1/c", nil)
assert.Nil(err)
assert.Equal(200, res.StatusCode)
body, _ := ioutil.ReadAll(res.Body)
assert.Equal("a1", string(body))
res.Body.Close()
})
1 .It didn't pass the test, "/a/a1/b/b1/c" not match pattern "/a/:a/b/:b/c"
2. I change the route order, let pattern "/a/:a/b/:b/c" to be first defined router, and it matches, but
"/a/a1/b/b1" not match pattern "/a/:a/b/:b"
It just passes ok in httprouter , is that a bug of mux?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.