Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions suffixExt_test.go
Original file line number Diff line number Diff line change
@@ -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))
}
21 changes: 21 additions & 0 deletions tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}