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

Golang beego.NewControllerRegister函数代码示例

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

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



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

示例1: startBeego

func startBeego() {
	beego.BConfig.RunMode = beego.PROD
	beego.BeeLogger.Close()
	mux := beego.NewControllerRegister()
	mux.Get("/hello", beegoHandler)
	http.ListenAndServe(":"+strconv.Itoa(port), mux)
}
开发者ID:cokeboL,项目名称:go-web-framework-benchmark,代码行数:7,代码来源:server.go


示例2: Test_AuthRequestWithAuthorizationHeader

func Test_AuthRequestWithAuthorizationHeader(t *testing.T) {

	url := "/foo"

	mux := beego.NewControllerRegister()

	mux.InsertFilter("*", beego.BeforeRouter, AuthRequest(&Options{
		PrivateKeyPath: "test/jwt.rsa",
		PublicKeyPath:  "test/jwt.rsa.pub",
		WhiteList:      []string{"/v1/jwt/issue-token", "/docs"},
	}))

	mux.Add("/foo", &JwtController{}, "get:Foo")
	newToken := CreateToken()

	rw, r := testRequest("GET", url)
	r.Header.Add("Authorization", "Bearer "+newToken["token"])
	mux.ServeHTTP(rw, r)

	if rw.Code != http.StatusOK {
		t.Errorf("Shoud return 200")
	}
	if rw.Body.String() != "ok" {
		t.Errorf("Should output ok")
	}
}
开发者ID:ngaut,项目名称:beego,代码行数:26,代码来源:jwt_test.go


示例3: TestErrorMessage

// Tests the error message rendered as JSON
func (suite *TestNqmSuite) TestErrorMessage(c *C) {
	testedService := beego.NewControllerRegister()
	setupUrlMappingAndHandler(testedService)

	/**
	 * Sets-up HTTP request and response
	 */
	sampleRequest, err := http.NewRequest(http.MethodGet, "/nqm/icmp/list/by-provinces?dsl=v1%3D10", nil)
	c.Assert(err, IsNil)
	respRecorder := httptest.NewRecorder()
	// :~)

	testedService.ServeHTTP(respRecorder, sampleRequest)
	c.Logf("Response: %v", respRecorder)

	/**
	 * Asserts the status code of HTTP
	 */
	c.Assert(respRecorder.Code, Equals, 400)
	// :~)

	testedJsonBody := jsonDslError{}
	json.Unmarshal(respRecorder.Body.Bytes(), &testedJsonBody)

	/**
	 * Asserts the JSON body for error message
	 */
	c.Assert(testedJsonBody.Code, Equals, 1)
	c.Assert(testedJsonBody.Message, Matches, ".+Unknown parameter.+")
	// :~)
}
开发者ID:donh,项目名称:query,代码行数:32,代码来源:nqm_test.go


示例4: Benchmark_WithoutCORS

func Benchmark_WithoutCORS(b *testing.B) {
	recorder := httptest.NewRecorder()
	handler := beego.NewControllerRegister()
	beego.RunMode = "prod"
	handler.Any("/foo", func(ctx *context.Context) {
		ctx.Output.SetStatus(500)
	})
	b.ResetTimer()
	for i := 0; i < 100; i++ {
		r, _ := http.NewRequest("PUT", "/foo", nil)
		handler.ServeHTTP(recorder, r)
	}
}
开发者ID:6pig9,项目名称:beego,代码行数:13,代码来源:cors_test.go


示例5: configNqmRoutes

func configNqmRoutes() {
	nqmService = nqm.GetDefaultServiceController()
	nqmService.Init()

	/**
	 * Registers the handler of RESTful service on beego
	 */
	serviceController := beego.NewControllerRegister()
	setupUrlMappingAndHandler(serviceController)
	// :~)

	http.Handle("/nqm/", serviceController)
}
开发者ID:donh,项目名称:query,代码行数:13,代码来源:nqm.go


示例6: Test_AllowAll

func Test_AllowAll(t *testing.T) {
	recorder := httptest.NewRecorder()
	handler := beego.NewControllerRegister()
	handler.InsertFilter("*", beego.BeforeRouter, Allow(&Options{
		AllowAllOrigins: true,
	}))
	handler.Any("/foo", func(ctx *context.Context) {
		ctx.Output.SetStatus(500)
	})
	r, _ := http.NewRequest("PUT", "/foo", nil)
	handler.ServeHTTP(recorder, r)

	if recorder.HeaderMap.Get(headerAllowOrigin) != "*" {
		t.Errorf("Allow-Origin header should be *")
	}
}
开发者ID:6pig9,项目名称:beego,代码行数:16,代码来源:cors_test.go


示例7: loadBeegoSingle

func loadBeegoSingle(method, path string, handler beego.FilterFunc) http.Handler {
	app := beego.NewControllerRegister()
	switch method {
	case "GET":
		app.Get(path, handler)
	case "POST":
		app.Post(path, handler)
	case "PUT":
		app.Put(path, handler)
	case "PATCH":
		app.Patch(path, handler)
	case "DELETE":
		app.Delete(path, handler)
	default:
		panic("Unknow HTTP method: " + method)
	}
	return app
}
开发者ID:kosuda,项目名称:go-http-routing-benchmark,代码行数:18,代码来源:routers.go


示例8: Test_DefaultAllowHeaders

func Test_DefaultAllowHeaders(t *testing.T) {
	recorder := httptest.NewRecorder()
	handler := beego.NewControllerRegister()
	handler.InsertFilter("*", beego.BeforeRouter, Allow(&Options{
		AllowAllOrigins: true,
	}))
	handler.Any("/foo", func(ctx *context.Context) {
		ctx.Output.SetStatus(500)
	})

	r, _ := http.NewRequest("PUT", "/foo", nil)
	handler.ServeHTTP(recorder, r)

	headersVal := recorder.HeaderMap.Get(headerAllowHeaders)
	if headersVal != "Origin,Accept,Content-Type,Authorization" {
		t.Errorf("Allow-Headers is expected to be Origin,Accept,Content-Type,Authorization; found %v", headersVal)
	}
}
开发者ID:6pig9,项目名称:beego,代码行数:18,代码来源:cors_test.go


示例9: Test_AllowRegexNoMatch

func Test_AllowRegexNoMatch(t *testing.T) {
	recorder := httptest.NewRecorder()
	handler := beego.NewControllerRegister()
	handler.InsertFilter("*", beego.BeforeRouter, Allow(&Options{
		AllowOrigins: []string{"https://*.foo.com"},
	}))
	handler.Any("/foo", func(ctx *context.Context) {
		ctx.Output.SetStatus(500)
	})
	origin := "https://ww.foo.com.evil.com"
	r, _ := http.NewRequest("PUT", "/foo", nil)
	r.Header.Add("Origin", origin)
	handler.ServeHTTP(recorder, r)

	headerValue := recorder.HeaderMap.Get(headerAllowOrigin)
	if headerValue != "" {
		t.Errorf("Allow-Origin header should not exist, found %v", headerValue)
	}
}
开发者ID:6pig9,项目名称:beego,代码行数:19,代码来源:cors_test.go


示例10: Test_Preflight

func Test_Preflight(t *testing.T) {
	recorder := NewRecorder()
	handler := beego.NewControllerRegister()
	handler.InsertFilter("*", beego.BeforeRouter, Allow(&Options{
		AllowAllOrigins: true,
		AllowMethods:    []string{"PUT", "PATCH"},
		AllowHeaders:    []string{"Origin", "X-whatever", "X-CaseSensitive"},
	}))

	handler.Any("/foo", func(ctx *context.Context) {
		ctx.Output.SetStatus(200)
	})

	r, _ := http.NewRequest("OPTIONS", "/foo", nil)
	r.Header.Add(headerRequestMethod, "PUT")
	r.Header.Add(headerRequestHeaders, "X-whatever, x-casesensitive")
	handler.ServeHTTP(recorder, r)

	headers := recorder.Header()
	methodsVal := headers.Get(headerAllowMethods)
	headersVal := headers.Get(headerAllowHeaders)
	originVal := headers.Get(headerAllowOrigin)

	if methodsVal != "PUT,PATCH" {
		t.Errorf("Allow-Methods is expected to be PUT,PATCH, found %v", methodsVal)
	}

	if !strings.Contains(headersVal, "X-whatever") {
		t.Errorf("Allow-Headers is expected to contain X-whatever, found %v", headersVal)
	}

	if !strings.Contains(headersVal, "x-casesensitive") {
		t.Errorf("Allow-Headers is expected to contain x-casesensitive, found %v", headersVal)
	}

	if originVal != "*" {
		t.Errorf("Allow-Origin is expected to be *, found %v", originVal)
	}

	if recorder.Code != http.StatusOK {
		t.Errorf("Status code is expected to be 200, found %d", recorder.Code)
	}
}
开发者ID:6pig9,项目名称:beego,代码行数:43,代码来源:cors_test.go


示例11: Test_OtherHeaders

func Test_OtherHeaders(t *testing.T) {
	recorder := httptest.NewRecorder()
	handler := beego.NewControllerRegister()
	handler.InsertFilter("*", beego.BeforeRouter, Allow(&Options{
		AllowAllOrigins:  true,
		AllowCredentials: true,
		AllowMethods:     []string{"PATCH", "GET"},
		AllowHeaders:     []string{"Origin", "X-whatever"},
		ExposeHeaders:    []string{"Content-Length", "Hello"},
		MaxAge:           5 * time.Minute,
	}))
	handler.Any("/foo", func(ctx *context.Context) {
		ctx.Output.SetStatus(500)
	})
	r, _ := http.NewRequest("PUT", "/foo", nil)
	handler.ServeHTTP(recorder, r)

	credentialsVal := recorder.HeaderMap.Get(headerAllowCredentials)
	methodsVal := recorder.HeaderMap.Get(headerAllowMethods)
	headersVal := recorder.HeaderMap.Get(headerAllowHeaders)
	exposedHeadersVal := recorder.HeaderMap.Get(headerExposeHeaders)
	maxAgeVal := recorder.HeaderMap.Get(headerMaxAge)

	if credentialsVal != "true" {
		t.Errorf("Allow-Credentials is expected to be true, found %v", credentialsVal)
	}

	if methodsVal != "PATCH,GET" {
		t.Errorf("Allow-Methods is expected to be PATCH,GET; found %v", methodsVal)
	}

	if headersVal != "Origin,X-whatever" {
		t.Errorf("Allow-Headers is expected to be Origin,X-whatever; found %v", headersVal)
	}

	if exposedHeadersVal != "Content-Length,Hello" {
		t.Errorf("Expose-Headers are expected to be Content-Length,Hello. Found %v", exposedHeadersVal)
	}

	if maxAgeVal != "300" {
		t.Errorf("Max-Age is expected to be 300, found %v", maxAgeVal)
	}
}
开发者ID:6pig9,项目名称:beego,代码行数:43,代码来源:cors_test.go


示例12: Benchmark_WithCORS

func Benchmark_WithCORS(b *testing.B) {
	recorder := httptest.NewRecorder()
	handler := beego.NewControllerRegister()
	beego.RunMode = "prod"
	handler.InsertFilter("*", beego.BeforeRouter, Allow(&Options{
		AllowAllOrigins:  true,
		AllowCredentials: true,
		AllowMethods:     []string{"PATCH", "GET"},
		AllowHeaders:     []string{"Origin", "X-whatever"},
		MaxAge:           5 * time.Minute,
	}))
	handler.Any("/foo", func(ctx *context.Context) {
		ctx.Output.SetStatus(500)
	})
	b.ResetTimer()
	for i := 0; i < 100; i++ {
		r, _ := http.NewRequest("PUT", "/foo", nil)
		handler.ServeHTTP(recorder, r)
	}
}
开发者ID:6pig9,项目名称:beego,代码行数:20,代码来源:cors_test.go


示例13: Test_AuthRequestWithoutAuthorizationHeader

func Test_AuthRequestWithoutAuthorizationHeader(t *testing.T) {
	url := "/foo"

	mux := beego.NewControllerRegister()

	mux.InsertFilter("*", beego.BeforeRouter, AuthRequest(&Options{
		PrivateKeyPath: "test/jwt.rsa",
		PublicKeyPath:  "test/jwt.rsa.pub",
		WhiteList:      []string{"/v1/jwt/issue-token", "/docs"},
	}))

	mux.Add("/foo", &JwtController{}, "get:Foo")

	rw, r := testRequest("GET", url)
	mux.ServeHTTP(rw, r)

	if rw.Code != http.StatusUnauthorized {
		t.Errorf("Shoud return 401")
	}
}
开发者ID:ngaut,项目名称:beego,代码行数:20,代码来源:jwt_test.go


示例14: loadBeego

func loadBeego(routes []route) http.Handler {
	re := regexp.MustCompile(":([^/]*)")
	app := beego.NewControllerRegister()
	for _, route := range routes {
		route.path = re.ReplaceAllString(route.path, ":$1")
		switch route.method {
		case "GET":
			app.Get(route.path, beegoHandler)
		case "POST":
			app.Post(route.path, beegoHandler)
		case "PUT":
			app.Put(route.path, beegoHandler)
		case "PATCH":
			app.Patch(route.path, beegoHandler)
		case "DELETE":
			app.Delete(route.path, beegoHandler)
		default:
			panic("Unknow HTTP method: " + route.method)
		}
	}
	return app
}
开发者ID:kosuda,项目名称:go-http-routing-benchmark,代码行数:22,代码来源:routers.go



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


鲜花

握手

雷人

路过

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

请发表评论

全部评论

专题导读
上一篇:
Golang beego.NewFlash函数代码示例发布时间:2022-05-24
下一篇:
Golang beego.NSRouter函数代码示例发布时间:2022-05-24
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

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

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

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