• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

Golang cookoo.Router类代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了Golang中github.com/Masterminds/cookoo.Router的典型用法代码示例。如果您正苦于以下问题:Golang Router类的具体用法?Golang Router怎么用?Golang Router使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。



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

示例1: Serve

// Serve creates a new Cookoo web server.
//
// Important details:
//
// 	- A URIPathResolver is used for resolving request names.
// 	- The following datasources are added to the Context:
// 	  * url: A URLDatasource (Provides access to parts of the URL)
// 	  * path: A PathDatasource (Provides access to parts of a path. E.g. "/foo/bar")
// 	  * query: A QueryParameterDatasource (Provides access to URL query parameters.)
// 	  * post: A FormValuesDatasource (Provides access to form data or the body of a request.)
// 	- The following context variables are set:
// 	  * http.Request: A pointer to the http.Request object
// 	  * http.ResponseWriter: The response writer.
// 	  * server.Address: The server's address and port (NOT ALWAYS PRESENT)
// 	- The handler includes logic to redirect "not found" errors to a path named "@404" if present.
//
// Context Params:
//
// 	- server.Address: If this key exists in the context, it will be used to determine the host/port the
//   server runes on. EXPERIMENTAL. Default is ":8080".
//
// Example:
//
//    package main
//
//    import (
//      //This is the path to Cookoo
//      "github.com/Masterminds/cookoo"
//      "github.com/Masterminds/cookoo/web"
//      "fmt"
//    )
//
//    func main() {
//      // Build a new Cookoo app.
//      registry, router, context := cookoo.Cookoo()
//
//      // Fill the registry.
//      registry.Route("GET /", "The index").Does(web.Flush, "example").
//        Using("content").WithDefault("Hello World")
//
//      // Create a server
//      web.Serve(reg, router, cookoo.SyncContext(cxt))
//    }
//
// Note that we synchronize the context before passing it into Serve(). This
// is optional because each handler gets its own copy of the context already.
// However, if commands pass the context to goroutines, the context ought to be
// synchronized to avoid race conditions.
//
// Note that copies of the context are not synchronized with each other.
// So by declaring the context synchronized here, you
// are not therefore synchronizing across handlers.
func Serve(reg *cookoo.Registry, router *cookoo.Router, cxt cookoo.Context) {
	addr := cxt.Get("server.Address", ":8080").(string)

	handler := NewCookooHandler(reg, router, cxt)

	// MPB: I dont think there's any real point in having a multiplexer in
	// this particular case. The Cookoo handler is mux enough.
	//
	// Note that we can always use Cookoo with the built-in multiplexer. It
	// just doesn't make sense if Cookoo's the only handler on the app.
	//http.Handle("/", handler)

	server := &http.Server{Addr: addr}

	// Instead of mux, set a single default handler.
	// What we might be losing:
	// - Handling of non-conforming paths.
	server.Handler = handler

	go handleSignals(router, cxt, server)
	err := server.ListenAndServe()
	//err := http.ListenAndServe(addr, nil)
	if err != nil {
		cxt.Logf("error", "Caught error while serving: %s", err)
		if router.HasRoute("@crash") {
			router.HandleRequest("@crash", cxt, false)
		}
	}
}
开发者ID:ngpestelos,项目名称:deis,代码行数:81,代码来源:server.go


示例2: setupHandler

func setupHandler(c *cli.Context, route string, cxt cookoo.Context, router *cookoo.Router) {
	cxt.Put("q", c.GlobalBool("quiet"))
	cxt.Put("yaml", c.GlobalString("yaml"))
	cxt.Put("cliArgs", c.Args())
	if err := router.HandleRequest(route, cxt, false); err != nil {
		fmt.Printf("Oops! %s\n", err)
		os.Exit(1)
	}
}
开发者ID:frozzare,项目名称:glide,代码行数:9,代码来源:glide.go


示例3: NewCookooHandler

// Create a new Cookoo HTTP handler.
//
// This will create an HTTP hanlder, but will not automatically attach it to a server. Implementors
// can take the handler and attach it to an existing HTTP server wiht http.HandleFunc() or
// http.ListenAndServe().
//
// For simple web servers, using this package's Serve() function may be the easier route.
//
// Important details:
//
// - A URIPathResolver is used for resolving request names.
// - The following datasources are added to the Context:
//   * url: A URLDatasource (Provides access to parts of the URL)
//   * path: A PathDatasource (Provides access to parts of a path. E.g. "/foo/bar")
//   * query: A QueryParameterDatasource (Provides access to URL query parameters.)
//   * post: A FormValuesDatasource (Provides access to form data or the body of a request.)
// - The following context variables are set:
//   * http.Request: A pointer to the http.Request object
//   * http.ResponseWriter: The response writer.
//   * server.Address: The server's address and port (NOT ALWAYS PRESENT)
func NewCookooHandler(reg *cookoo.Registry, router *cookoo.Router, cxt cookoo.Context) *CookooHandler {
	handler := new(CookooHandler)
	handler.Registry = reg
	handler.Router = router
	handler.BaseContext = cxt

	// Use the URI oriented request resolver in this package.
	resolver := new(URIPathResolver)
	resolver.Init(reg)
	router.SetRequestResolver(resolver)

	return handler
}
开发者ID:ngpestelos,项目名称:deis,代码行数:33,代码来源:server.go


示例4: ServeTLS

// ServeTLS does the same as Serve, but with SSL support.
//
// If `server.Address` is not found in the context, the default address is
// `:4433`.
//
// Neither certFile nor keyFile are stored in the context. These values are
// considered to be security sensitive.
func ServeTLS(reg *cookoo.Registry, router *cookoo.Router, cxt cookoo.Context, certFile, keyFile string) {
	addr := cxt.Get("server.Address", ":4433").(string)

	server := &http.Server{Addr: addr}
	server.Handler = NewCookooHandler(reg, router, cxt)

	go handleSignals(router, cxt, server)
	err := server.ListenAndServeTLS(certFile, keyFile)
	if err != nil {
		cxt.Logf("error", "Caught error while serving: %s", err)
		if router.HasRoute("@crash") {
			router.HandleRequest("@crash", cxt, false)
		}
	}
}
开发者ID:ngpestelos,项目名称:deis,代码行数:22,代码来源:server.go


示例5: shutdown

// shutdown runs an @shutdown route if it's found in the router.
func shutdown(router *cookoo.Router, cxt cookoo.Context) {
	if router.HasRoute("@shutdown") {
		cxt.Logf("info", "Executing route @shutdown")
		router.HandleRequest("@shutdown", cxt, false)
	}
}
开发者ID:ngpestelos,项目名称:deis,代码行数:7,代码来源:server.go



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


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Golang log.Errf函数代码示例发布时间:2022-05-23
下一篇:
Golang cookoo.Registry类代码示例发布时间:2022-05-23
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap