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


Golang Router.Handle方法代码示例

本文整理汇总了Golang中github.com/julienschmidt/httprouter.Router.Handle方法的典型用法代码示例。如果您正苦于以下问题:Golang Router.Handle方法的具体用法?Golang Router.Handle怎么用?Golang Router.Handle使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在github.com/julienschmidt/httprouter.Router的用法示例。


在下文中一共展示了Router.Handle方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。

示例1: configure

// configure registers the API's routes on a router. If the passed router is nil, we create a new one and return it.
// The nil mode is used when an API is run in stand-alone mode.
func (a *API) configure(router *httprouter.Router) *httprouter.Router {

	if router == nil {
		router = httprouter.New()
	}

	for i, route := range a.Routes {

		if err := route.parseInfo(route.Path); err != nil {
			logging.Error("Error parsing info for %s: %s", route.Path, err)
		}
		a.Routes[i] = route
		h := a.handler(route)

		pth := a.FullPath(route.Path)

		if route.Methods&GET == GET {
			logging.Info("Registering GET handler %v to path %s", h, pth)
			router.Handle("GET", pth, h)
		}
		if route.Methods&POST == POST {
			logging.Info("Registering POST handler %v to path %s", h, pth)
			router.Handle("POST", pth, h)

		}

	}

	chain := buildChain(a.SwaggerMiddleware...)
	if chain == nil {
		chain = buildChain(a.swaggerHandler())
	} else {
		chain.append(a.swaggerHandler())
	}

	// Server the API documentation swagger
	router.GET(a.FullPath("/swagger"), a.middlewareHandler(chain, nil, nil))

	chain = buildChain(a.TestMiddleware...)
	if chain == nil {
		chain = buildChain(a.testHandler())
	} else {
		chain.append(a.testHandler())
	}

	router.GET(path.Join("/test", a.root(), ":category"), a.middlewareHandler(chain, nil, nil))

	// Redirect /$api/$version/console => /console?url=/$api/$version/swagger
	uiPath := fmt.Sprintf("/console?url=%s", url.QueryEscape(a.FullPath("/swagger")))
	router.Handler("GET", a.FullPath("/console"), http.RedirectHandler(uiPath, 301))

	return router

}
开发者ID:sguzwf,项目名称:vertex,代码行数:56,代码来源:api.go

示例2: addHandlers

func addHandlers(router *httprouter.Router, midl *idl.Idl) error {
	count := 0
	re := regexp.MustCompile(`\{(\w+)\}`)
	for _, svc := range allServices(midl) {
		for _, mth := range svc.Methods {
			count++
			op, err := rest.ReadOp(mth)
			if err != nil {
				log.Fatal("Cannot process %s.%s: %s", svc.Name, mth.Name, err)
			}
			if !op.Hide {
				routePath := re.ReplaceAllString(path.Join(conf.RestPath, op.Path), ":$1")
				handle, err := makeHandler(midl, svc, mth)
				if err != nil {
					return err
				}
				router.Handle(op.Method.String(), routePath, handle)
				log.Printf("%s %s -> %s", op.Method.String(), path.Join(conf.RestPath, op.Path), path.Join(conf.BabelPath, svc.Name, mth.Name))
			}
			babelPath := path.Join(conf.BabelPath, svc.Name, mth.Name)
			if !strings.HasPrefix(babelPath, "/") {
				babelPath = "/" + babelPath
			}
			handle, err := makeBabelHandler(midl, svc, mth)
			if err != nil {
				return err
			}
			router.Handle("POST", babelPath, handle)
			log.Printf("%s %s -> %s", op.Method.String(), babelPath, babelPath)
		}
	}
	if count == 0 {
		log.Fatal("No services to process")
	}

	return nil
}
开发者ID:babelrpc,项目名称:babel,代码行数:37,代码来源:handlers.go

示例3: RegisterRoutes

// Registers an array of route handlers to gorilla/mux
func RegisterRoutes(rtr *httprouter.Router, controller WebController) {
	for _, route := range controller.GetRoutes() {
		rtr.Handle(route.GetMethod(), route.GetPath(), route.GetHandler())
	}
}
开发者ID:byrnedo,项目名称:apibase,代码行数:6,代码来源:web.go


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