本文整理匯總了Golang中github.com/julienschmidt/httprouter.Router.Handler方法的典型用法代碼示例。如果您正苦於以下問題:Golang Router.Handler方法的具體用法?Golang Router.Handler怎麽用?Golang Router.Handler使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類github.com/julienschmidt/httprouter.Router
的用法示例。
在下文中一共展示了Router.Handler方法的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的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
}
示例2: initCMDGroup
// initCMDGroup establishes routes for automatically reloading the page on any
// assets/ change when a watcher is running (see cmd/*watcher.sh).
func initCMDGroup(router *httprouter.Router) {
cmdch := make(chan string, 10)
addconnch := make(chan *cmdConn, 10)
delconnch := make(chan *cmdConn, 10)
go cmder(cmdch, addconnch, delconnch)
router.GET("/_/cmd/ws/*cmd", func(w http.ResponseWriter,
r *http.Request, ps httprouter.Params) {
cmdch <- ps.ByName("cmd")[1:]
w.WriteHeader(http.StatusOK)
})
router.Handler("GET", "/_/cmd/ws", w.Handler(func(wsc *w.Conn) {
respch := make(chan bool)
conn := &cmdConn{ws: wsc, respch: respch}
addconnch <- conn
<-respch
delconnch <- conn
}))
}