From 1d129bcd5b0858a443ebf5ed7f5f6832e80d73fe Mon Sep 17 00:00:00 2001 From: ZetaChow Date: Fri, 26 May 2017 15:20:41 +0800 Subject: [PATCH 1/4] =?UTF-8?q?add=203=20function=20=20for=20modifying=20?= =?UTF-8?q?=E2=80=98allowSuffixExt=E2=80=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tree.go | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/tree.go b/tree.go index 372bab6..2bbe015 100644 --- a/tree.go +++ b/tree.go @@ -637,3 +637,20 @@ func pathClean(path string) string { } return strings.Replace(path, `//`, "/", -1) } + +func AddSuffixExt(extString string) { + allowSuffixExt = append(allowSuffixExt, extString) +} + +func RemoveSuffixExt(extString string) { + for k, v := range allowSuffixExt { + if extString == v { + allowSuffixExt = append(allowSuffixExt[:k], allowSuffixExt[k+1:]...) + break + } + } +} + +func GetSuffixExts() []string { + return allowSuffixExt +} From 93956bf96edf619c1c7a6bb137d5dd71aff57c41 Mon Sep 17 00:00:00 2001 From: ZetaChow Date: Sat, 27 May 2017 18:22:47 +0800 Subject: [PATCH 2/4] add comments for AddSuffixExt,RemoveSuffixExt,GetAddSuffixExts --- tree.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/tree.go b/tree.go index 2bbe015..4914194 100644 --- a/tree.go +++ b/tree.go @@ -638,10 +638,13 @@ func pathClean(path string) string { return strings.Replace(path, `//`, "/", -1) } +// AddSuffixExt add new suffix ext +// mux.AddSuffixExt(".foo") will match /any/path.foo func AddSuffixExt(extString string) { allowSuffixExt = append(allowSuffixExt, extString) } +// RemoveSuffixExt remove exist suffix ext func RemoveSuffixExt(extString string) { for k, v := range allowSuffixExt { if extString == v { @@ -651,6 +654,7 @@ func RemoveSuffixExt(extString string) { } } +// GetSuffixExts get all exist suffix exts slice func GetSuffixExts() []string { return allowSuffixExt } From 5eb4742cc80879790edd5de5f534d35701a5a334 Mon Sep 17 00:00:00 2001 From: ZetaChow Date: Sat, 27 May 2017 18:24:11 +0800 Subject: [PATCH 3/4] add comments for AddSuffixExt,RemoveSuffixExt,GetSuffixExts --- bench_test.go | 2 +- main_test.go | 31 +++++++++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 1 deletion(-) create mode 100644 main_test.go diff --git a/bench_test.go b/bench_test.go index 238c4e9..cb16766 100644 --- a/bench_test.go +++ b/bench_test.go @@ -8,7 +8,7 @@ import ( "runtime" "testing" - "github.com/beego/mux" + "github.com/ZetaChow/mux" "github.com/dimfeld/httptreemux" "github.com/julienschmidt/httprouter" ) diff --git a/main_test.go b/main_test.go new file mode 100644 index 0000000..7188c42 --- /dev/null +++ b/main_test.go @@ -0,0 +1,31 @@ +package mux + +import ( + "fmt" + "log" + "net/http" + "testing" +) + +func TestMain(t *testing.T) { + mx := New() + mx.Get("/", func(w http.ResponseWriter, r *http.Request) { + w.Write([]byte("hello, beego mux")) + }) + mx.Get("/abc/:id", func(w http.ResponseWriter, r *http.Request) { + fmt.Fprintf(w, "hello, abc page %s", Param(r, ":id")) + }) + + mx.Get("/hello", func(w http.ResponseWriter, r *http.Request) { + fmt.Fprintf(w, "hello") + }) + + AddSuffixExt(".hahahahah") + fmt.Println(GetSuffixExts()) + AddSuffixExt(".xixixixixi") + fmt.Println(GetSuffixExts()) + RemoveSuffixExt(".hahahahah") + fmt.Println(GetSuffixExts()) + + log.Fatal(http.ListenAndServe("127.0.0.1:9999", mx)) +} From f3fbeaa9e03ba2539554a876056a0c17a65200ca Mon Sep 17 00:00:00 2001 From: ZetaChow Date: Sat, 27 May 2017 18:31:13 +0800 Subject: [PATCH 4/4] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E6=B5=8B=E8=AF=95?= =?UTF-8?q?=E6=96=87=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bench_test.go | 2 +- main_test.go => suffixExt_test.go | 9 +++------ 2 files changed, 4 insertions(+), 7 deletions(-) rename main_test.go => suffixExt_test.go (67%) diff --git a/bench_test.go b/bench_test.go index cb16766..238c4e9 100644 --- a/bench_test.go +++ b/bench_test.go @@ -8,7 +8,7 @@ import ( "runtime" "testing" - "github.com/ZetaChow/mux" + "github.com/beego/mux" "github.com/dimfeld/httptreemux" "github.com/julienschmidt/httprouter" ) diff --git a/main_test.go b/suffixExt_test.go similarity index 67% rename from main_test.go rename to suffixExt_test.go index 7188c42..c5d875f 100644 --- a/main_test.go +++ b/suffixExt_test.go @@ -12,19 +12,16 @@ func TestMain(t *testing.T) { mx.Get("/", func(w http.ResponseWriter, r *http.Request) { w.Write([]byte("hello, beego mux")) }) - mx.Get("/abc/:id", func(w http.ResponseWriter, r *http.Request) { - fmt.Fprintf(w, "hello, abc page %s", Param(r, ":id")) - }) mx.Get("/hello", func(w http.ResponseWriter, r *http.Request) { fmt.Fprintf(w, "hello") }) - AddSuffixExt(".hahahahah") + AddSuffixExt(".foo") fmt.Println(GetSuffixExts()) - AddSuffixExt(".xixixixixi") + AddSuffixExt(".foo2") fmt.Println(GetSuffixExts()) - RemoveSuffixExt(".hahahahah") + RemoveSuffixExt(".foo") fmt.Println(GetSuffixExts()) log.Fatal(http.ListenAndServe("127.0.0.1:9999", mx))