本文整理匯總了Golang中github.com/gorilla/mux.Router.MatcherFunc方法的典型用法代碼示例。如果您正苦於以下問題:Golang Router.MatcherFunc方法的具體用法?Golang Router.MatcherFunc怎麽用?Golang Router.MatcherFunc使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類github.com/gorilla/mux.Router
的用法示例。
在下文中一共展示了Router.MatcherFunc方法的4個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: HandleStatic
func (self *Web) HandleStatic(router *mux.Router, dir string) (err error) {
children, err := templar.GetMatchingBlobNames(self.env == Development, "^static/.*")
if err != nil {
return
}
for _, fil := range children {
cpy := fil
self.Handle(router.MatcherFunc(func(r *http.Request, rm *mux.RouteMatch) bool {
return strings.HasSuffix(r.URL.Path, filepath.Base(cpy))
}), func(c *HTTPContext) (err error) {
if strings.HasSuffix(c.Req().URL.Path, ".css") {
c.SetContentType("text/css; charset=UTF-8", true)
} else if strings.HasSuffix(c.Req().URL.Path, ".js") {
c.SetContentType("application/javascript; charset=UTF-8", true)
} else if strings.HasSuffix(c.Req().URL.Path, ".png") {
c.SetContentType("image/png", true)
} else if strings.HasSuffix(c.Req().URL.Path, ".gif") {
c.SetContentType("image/gif", true)
} else if strings.HasSuffix(c.Req().URL.Path, ".c.Resp()off") {
c.SetContentType("application/font-c.Resp()off", true)
} else if strings.HasSuffix(c.Req().URL.Path, ".ttf") {
c.SetContentType("font/truetype", true)
} else {
c.SetContentType("application/octet-stream", true)
}
in, err := templar.GetBlob(self.env == Development, cpy)
if err != nil {
self.Errorf("%v", err)
c.Resp().WriteHeader(500)
fmt.Fprintln(c.Resp(), err)
} else {
defer in.Close()
if _, err = io.Copy(c.Resp(), in); err != nil {
return
}
}
return
})
}
return
}
示例2: handleStatic
func handleStatic(router *mux.Router, dir string) {
static, err := os.Open(dir)
if err != nil {
panic(err)
}
children, err := static.Readdirnames(-1)
if err != nil {
panic(err)
}
for _, fil := range children {
cpy := fil
router.MatcherFunc(func(r *http.Request, rm *mux.RouteMatch) bool {
return strings.HasSuffix(r.URL.Path, cpy)
}).HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if strings.HasSuffix(r.URL.Path, ".css") {
common.SetContentType(w, "text/css; charset=UTF-8", true)
} else if strings.HasSuffix(r.URL.Path, ".js") {
common.SetContentType(w, "application/javascript; charset=UTF-8", true)
} else if strings.HasSuffix(r.URL.Path, ".png") {
common.SetContentType(w, "image/png", true)
} else if strings.HasSuffix(r.URL.Path, ".gif") {
common.SetContentType(w, "image/gif", true)
} else if strings.HasSuffix(r.URL.Path, ".woff") {
common.SetContentType(w, "application/font-woff", true)
} else if strings.HasSuffix(r.URL.Path, ".ttf") {
common.SetContentType(w, "font/truetype", true)
} else {
common.SetContentType(w, "application/octet-stream", true)
}
if in, err := os.Open(filepath.Join("static", cpy)); err != nil {
w.WriteHeader(500)
} else {
defer in.Close()
if _, err := io.Copy(w, in); err != nil {
w.WriteHeader(500)
}
}
})
}
}
示例3: AddRoutes
func AddRoutes(router *mux.Router) *mux.Router {
router.Path("/")
for acceptHeader, vApi := range acceptVersionMap {
// Create a subrouter for the header/api version.
subrouter := router.MatcherFunc(
acceptOrQueryMatcherFactory(acceptHeader)).Subrouter()
// Define the path/handler relationships.
pathHandlerMap := map[string]func(http.ResponseWriter, *http.Request){
"/json.json": vApi.JsonHandler,
"/json2.json": vApi.JsonHandler2,
"/json3.json": vApi.JsonHandler3,
}
// Create a route in the subrouter for each path/handler.
for path, handler := range pathHandlerMap {
route := subrouter.HandleFunc(path, handler)
route.Name(fmt.Sprintf("%s - %s", path, handler))
}
}
return router
}
示例4: registerFastCGIHandler
func registerFastCGIHandler(r URLRoute, router *mux.Router) {
_u1, err := url.Parse(r.Path)
if err != nil {
fmt.Printf("invalid path: %s\n", r.Path)
os.Exit(-1)
}
_p := _u1.Path
switch _u1.Scheme {
case "unix":
case "tcp":
_p = _u1.Host
default:
fmt.Printf("invalid scheme: %s, only support unix, tcp", _u1.Scheme)
os.Exit(-1)
}
if r.UseRegex {
m1 := myURLMatch{regexp.MustCompile(r.URLPrefix)}
_u, _ := NewFastCGI(_u1.Scheme, _p, r.DocRoot, "")
router.MatcherFunc(m1.match).Handler(_u)
} else {
_u, _ := NewFastCGI(_u1.Scheme, _p, r.DocRoot, r.URLPrefix)
router.PathPrefix(r.URLPrefix).Handler(_u)
}
}