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


Golang web.New函数代码示例

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


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

示例1: InitRouter

// InitRouter sets up the router (and subrouters).
// It also includes the closure middleware where we load the global Settings reference into each request.
func InitRouter(settings *helpers.Settings) *web.Router {
	router := web.New(Context{})

	// A closure that effectively loads the Settings into every request.
	router.Middleware(func(c *Context, resp web.ResponseWriter, req *web.Request, next web.NextMiddlewareFunc) {
		c.Settings = settings
		next(resp, req)
	})

	// Backend Route Initialization
	// Initialize the Gocraft Router with the basic context and routes
	router.Get("/ping", (*Context).Ping)
	router.Get("/handshake", (*Context).LoginHandshake)
	router.Get("/oauth2callback", (*Context).OAuthCallback)

	// Setup the /api subrouter.
	apiRouter := router.Subrouter(APIContext{}, "/v2")
	apiRouter.Middleware((*APIContext).OAuth)
	// All routes accepted
	apiRouter.Get("/authstatus", (*APIContext).AuthStatus)
	apiRouter.Get("/logout", (*APIContext).Logout)
	apiRouter.Get("/profile", (*APIContext).UserProfile)
	apiRouter.Get("/:*", (*APIContext).Proxy)

	// Frontend Route Initialization
	// Set up static file serving to load from the static folder.
	router.Middleware(web.StaticMiddleware("static", web.StaticOption{IndexFile: "index.html"}))

	return router
}
开发者ID:goodnightjj,项目名称:cf-deck,代码行数:32,代码来源:routes.go

示例2: main

func main() {
	router := web.New(Context{}).
		Middleware(web.LoggerMiddleware).
		Middleware(runnerMiddleware).
		Get("/", (*Context).SayHello)
	http.ListenAndServe("localhost:3000", router)
}
开发者ID:stoplightio,项目名称:fresh,代码行数:7,代码来源:main.go

示例3: StartServer

func StartServer(database *gorm.DB) {

	db = database

	router := web.New(Context{}).
		Middleware(web.LoggerMiddleware).
		Middleware((*Context).QueryVarsMiddleware).
		Middleware(CorsMiddleware).
		Get("/counter", (*Context).CounterHandler).
		Get("/counter/:code", (*Context).CounterByCodeHandler).
		Post("/counter/:code/tick", (*Context).CounterByCodeTickHandler).
		Put("/counter/:code/corr", (*Context).CounterByCodeCorrectHandler).
		Get("/counter/:code/events", (*Context).CounterByCodeEventsHandler).
		Get("/thermometer", (*Context).ThermometerHandler).
		Get("/thermometer/:code", (*Context).ThermometerByCodeHandler).
		Post("/thermometer/:code/reading", (*Context).ThermometerByCodeAddReadingHandler).
		Get("/thermometer/:code/readings", (*Context).ThermometerByCodeGetReadingsHandler).
		Get("/flag", (*Context).FlagHandler).
		Get("/flag/:code", (*Context).FlagByCodeHandler).
		Post("/flag/:code/state", (*Context).FlagByCodeChangeStateHandler).
		Get("/flag/:code/states", (*Context).FlagByCodeGetStatesHandler)

	e := http.ListenAndServe(":8080", router)
	tools.ErrorCheck(e)
}
开发者ID:b00lduck,项目名称:raspberry-energy-monitor,代码行数:25,代码来源:server.go

示例4: TestMultipleErrorHandlers2

func (s *ErrorTestSuite) TestMultipleErrorHandlers2(c *C) {
	router := web.New(Context{})
	router.Get("/action", (*Context).ErrorAction)

	admin := router.Subrouter(AdminContext{}, "/admin")
	admin.Error((*AdminContext).ErrorHandler)
	admin.Get("/action", (*AdminContext).ErrorAction)

	api := router.Subrouter(ApiContext{}, "/api")
	api.Error((*ApiContext).ErrorHandler)
	api.Get("/action", (*ApiContext).ErrorAction)

	rw, req := newTestRequest("GET", "/action")
	router.ServeHTTP(rw, req)
	c.Assert(strings.TrimSpace(string(rw.Body.Bytes())), Equals, "Application Error")
	c.Assert(rw.Code, Equals, http.StatusInternalServerError)

	rw, req = newTestRequest("GET", "/admin/action")
	router.ServeHTTP(rw, req)
	c.Assert(strings.TrimSpace(string(rw.Body.Bytes())), Equals, "Admin Error")
	c.Assert(rw.Code, Equals, http.StatusInternalServerError)

	rw, req = newTestRequest("GET", "/api/action")
	router.ServeHTTP(rw, req)
	c.Assert(strings.TrimSpace(string(rw.Body.Bytes())), Equals, "Api Error")
	c.Assert(rw.Code, Equals, http.StatusInternalServerError)
}
开发者ID:kristian-puccio,项目名称:web,代码行数:27,代码来源:error_test.go

示例5: BenchmarkGocraftWeb_Composite

func BenchmarkGocraftWeb_Composite(b *testing.B) {
	namespaces, resources, requests := resourceSetup(10)

	nextMw := func(rw web.ResponseWriter, r *web.Request, next web.NextMiddlewareFunc) {
		next(rw, r)
	}

	router := web.New(BenchContext{})
	router.Middleware(func(c *BenchContext, rw web.ResponseWriter, r *web.Request, next web.NextMiddlewareFunc) {
		c.MyField = r.URL.Path
		next(rw, r)
	})
	router.Middleware(nextMw)
	router.Middleware(nextMw)

	for _, ns := range namespaces {
		subrouter := router.Subrouter(BenchContextB{}, "/"+ns)
		subrouter.Middleware(nextMw)
		subrouter.Middleware(nextMw)
		subrouter.Middleware(nextMw)
		for _, res := range resources {
			subrouter.Get("/"+res, (*BenchContextB).Action)
			subrouter.Post("/"+res, (*BenchContextB).Action)
			subrouter.Get("/"+res+"/:id", (*BenchContextB).Action)
			subrouter.Put("/"+res+"/:id", (*BenchContextB).Action)
			subrouter.Delete("/"+res+"/:id", (*BenchContextB).Action)
		}
	}
	benchmarkRoutes(b, router, requests)
}
开发者ID:rexk,项目名称:golang-mux-benchmark,代码行数:30,代码来源:mux_bench_test.go

示例6: BenchmarkMiddleware

// In this bench we want to test middleware.
// Context: middleware stack with 3 levels of context
// Each middleware has 2 functions which just call next()
func BenchmarkMiddleware(b *testing.B) {

	nextMw := func(w web.ResponseWriter, r *web.Request, next web.NextMiddlewareFunc) {
		next(w, r)
	}

	router := web.New(BenchContext{})
	router.Middleware(nextMw)
	router.Middleware(nextMw)
	routerB := router.Subrouter(BenchContextB{}, "/b")
	routerB.Middleware(nextMw)
	routerB.Middleware(nextMw)
	routerC := routerB.Subrouter(BenchContextC{}, "/c")
	routerC.Middleware(nextMw)
	routerC.Middleware(nextMw)
	routerC.Get("/action", func(w web.ResponseWriter, r *web.Request) {
		fmt.Fprintf(w, "hello")
	})

	rw, req := testRequest("GET", "/b/c/action")

	b.ResetTimer()
	for i := 0; i < b.N; i++ {
		router.ServeHTTP(rw, req)
		// if rw.Code != 200 { panic("no good") }
	}
}
开发者ID:nikai3d,项目名称:web,代码行数:30,代码来源:speed_test.go

示例7: main

func main() {
	runtime.MemProfileRate = 1

	router := web.New(Context{}).
		Middleware((*Context).Middleware).
		Middleware((*Context).Middleware).
		Middleware((*Context).Middleware).
		Middleware((*Context).Middleware).
		Middleware((*Context).Middleware).
		Middleware((*Context).Middleware).
		Get("/action", (*Context).Action)

	rw := &NullWriter{}
	req, _ := http.NewRequest("GET", "/action", nil)

	// pprof.StartCPUProfile(f)
	// defer pprof.StopCPUProfile()

	for i := 0; i < 1; i += 1 {
		router.ServeHTTP(rw, req)
	}

	f, err := os.Create("myprof.out")
	if err != nil {
		log.Fatal(err)
	}

	pprof.WriteHeapProfile(f)
	f.Close()
}
开发者ID:hannobraun,项目名称:web,代码行数:30,代码来源:main.go

示例8: main

func main() {
	router := web.New(Context{}). // Create your router
					Middleware(web.LoggerMiddleware).     // Use some included middleware
					Middleware(web.ShowErrorsMiddleware). // ...
					Middleware((*Context).SetHelloCount). // Your own middleware!
					Get("/", (*Context).SayHello)         // Add a route
	http.ListenAndServe("localhost:3000", router) // Start the server!
}
开发者ID:kristian-puccio,项目名称:web,代码行数:8,代码来源:main.go

示例9: TestBadMethod

func (s *NotFoundTestSuite) TestBadMethod(c *C) {
	router := web.New(Context{})

	rw, req := newTestRequest("POOP", "/this_path_doesnt_exist")
	router.ServeHTTP(rw, req)
	c.Assert(strings.TrimSpace(string(rw.Body.Bytes())), Equals, "Not Found")
	c.Assert(rw.Code, Equals, http.StatusNotFound)
}
开发者ID:kristian-puccio,项目名称:web,代码行数:8,代码来源:not_found_test.go

示例10: TestSameNamespace

func (s *MiddlewareTestSuite) TestSameNamespace(c *C) {
	router := web.New(Context{})
	admin := router.Subrouter(AdminContext{}, "/")
	admin.Get("/action", (*AdminContext).B)

	rw, req := newTestRequest("GET", "/action")
	router.ServeHTTP(rw, req)
	assertResponse(c, rw, "admin-B", 200)
}
开发者ID:nikai3d,项目名称:web,代码行数:9,代码来源:middleware_test.go

示例11: TestNoNext

func (s *MiddlewareTestSuite) TestNoNext(c *C) {
	router := web.New(Context{})
	router.Middleware((*Context).mwNoNext)
	router.Get("/action", (*Context).A)

	rw, req := newTestRequest("GET", "/action")
	router.ServeHTTP(rw, req)
	assertResponse(c, rw, "context-mw-NoNext", 200)
}
开发者ID:nikai3d,项目名称:web,代码行数:9,代码来源:middleware_test.go

示例12: InitRouter

// InitRouter sets up the router (and subrouters).
// It also includes the closure middleware where we load the global Settings reference into each request.
func InitRouter(settings *helpers.Settings) *web.Router {
	if settings == nil {
		return nil
	}
	router := web.New(Context{})

	// A closure that effectively loads the Settings into every request.
	router.Middleware(func(c *Context, resp web.ResponseWriter, req *web.Request, next web.NextMiddlewareFunc) {
		c.Settings = settings
		next(resp, req)
	})

	// Backend Route Initialization
	// Initialize the Gocraft Router with the basic context and routes
	router.Get("/ping", (*Context).Ping)
	router.Get("/handshake", (*Context).LoginHandshake)
	router.Get("/oauth2callback", (*Context).OAuthCallback)

	// Secure all the other routes
	secureRouter := router.Subrouter(SecureContext{}, "/")

	// Setup the /api subrouter.
	apiRouter := secureRouter.Subrouter(APIContext{}, "/v2")
	apiRouter.Middleware((*APIContext).OAuth)
	// All routes accepted
	apiRouter.Get("/authstatus", (*APIContext).AuthStatus)
	apiRouter.Get("/logout", (*APIContext).Logout)
	apiRouter.Get("/profile", (*APIContext).UserProfile)
	apiRouter.Get("/:*", (*APIContext).APIProxy)
	apiRouter.Put("/:*", (*APIContext).APIProxy)
	apiRouter.Post("/:*", (*APIContext).APIProxy)
	apiRouter.Delete("/:*", (*APIContext).APIProxy)

	// Setup the /uaa subrouter.
	uaaRouter := secureRouter.Subrouter(UAAContext{}, "/uaa")
	uaaRouter.Middleware((*UAAContext).OAuth)
	uaaRouter.Get("/userinfo", (*UAAContext).UserInfo)
	uaaRouter.Post("/Users", (*UAAContext).QueryUser)

	if settings.PProfEnabled {
		// Setup the /pprof subrouter.
		pprofRouter := secureRouter.Subrouter(PProfContext{}, "/debug/pprof")
		pprofRouter.Get("/", (*PProfContext).Index)
		pprofRouter.Get("/heap", (*PProfContext).Heap)
		pprofRouter.Get("/goroutine", (*PProfContext).Goroutine)
		pprofRouter.Get("/threadcreate", (*PProfContext).Threadcreate)
		pprofRouter.Get("/block", (*PProfContext).Threadcreate)
		pprofRouter.Get("/profile", (*PProfContext).Profile)
		pprofRouter.Get("/symbol", (*PProfContext).Symbol)
	}

	// Frontend Route Initialization
	// Set up static file serving to load from the static folder.
	router.Middleware(web.StaticMiddleware("static", web.StaticOption{IndexFile: "index.html"}))

	return router
}
开发者ID:dlapiduz,项目名称:cf-deck,代码行数:59,代码来源:routes.go

示例13: TestWithRootContext

func (s *NotFoundTestSuite) TestWithRootContext(c *C) {
	router := web.New(Context{})
	router.NotFound((*Context).HandlerWithContext)

	rw, req := newTestRequest("GET", "/this_path_doesnt_exist")
	router.ServeHTTP(rw, req)
	c.Assert(strings.TrimSpace(string(rw.Body.Bytes())), Equals, "My Not Found With Context")
	c.Assert(rw.Code, Equals, http.StatusNotFound)
}
开发者ID:kristian-puccio,项目名称:web,代码行数:9,代码来源:not_found_test.go

示例14: main

func main() {
	router := web.New(Context{}). // Create your router
					Middleware(web.LoggerMiddleware).       // Use some included middleware
					Middleware(web.ShowErrorsMiddleware).   // ...
					Middleware((*Context).Recording).       // Your own middleware!
					Get("/", (*Context).Root).              // Add a route
					Get("/stats", (*Context).RetrieveStats) // Add a route
	http.ListenAndServe("localhost:3001", router) // Start the server!
}
开发者ID:krisrang,项目名称:fancypants,代码行数:9,代码来源:server.go

示例15: initRouter

func initRouter() *web.Router {
	router := web.New(Context{})
	router.Middleware(LogMiddleware)
	router.Middleware(web.ShowErrorsMiddleware)
	//  router.Middleware((*Context).OutputJson)

	// router.Get("/v1/clean", (*Context).Clean)

	return router
}
开发者ID:ahjdzx,项目名称:dis,代码行数:10,代码来源:router.go


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