当前位置: 首页>>代码示例>>Golang>>正文


Golang Router.MatcherFunc方法代码示例

本文整理汇总了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
}
开发者ID:arlm,项目名称:diplicity,代码行数:41,代码来源:web.go

示例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)
				}
			}
		})
	}
}
开发者ID:robban,项目名称:stockholm-ai,代码行数:40,代码来源:web.go

示例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
}
开发者ID:ctemplin,项目名称:go-versioned-api,代码行数:22,代码来源:main.go

示例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)
	}
}
开发者ID:fangdingjun,项目名称:gpp,代码行数:24,代码来源:routers.go


注:本文中的github.com/gorilla/mux.Router.MatcherFunc方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。