diff --git a/suffixExt_test.go b/suffixExt_test.go new file mode 100644 index 0000000..c5d875f --- /dev/null +++ b/suffixExt_test.go @@ -0,0 +1,28 @@ +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("/hello", func(w http.ResponseWriter, r *http.Request) { + fmt.Fprintf(w, "hello") + }) + + AddSuffixExt(".foo") + fmt.Println(GetSuffixExts()) + AddSuffixExt(".foo2") + fmt.Println(GetSuffixExts()) + RemoveSuffixExt(".foo") + fmt.Println(GetSuffixExts()) + + log.Fatal(http.ListenAndServe("127.0.0.1:9999", mx)) +} diff --git a/tree.go b/tree.go index 372bab6..4914194 100644 --- a/tree.go +++ b/tree.go @@ -637,3 +637,24 @@ 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 { + allowSuffixExt = append(allowSuffixExt[:k], allowSuffixExt[k+1:]...) + break + } + } +} + +// GetSuffixExts get all exist suffix exts slice +func GetSuffixExts() []string { + return allowSuffixExt +}