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

Golang vodka.New函数代码示例

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

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



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

示例1: TestGzip

func TestGzip(t *testing.T) {
	req, _ := http.NewRequest(vodka.GET, "/", nil)
	rec := httptest.NewRecorder()
	c := vodka.NewContext(req, vodka.NewResponse(rec), vodka.New())
	h := func(c *vodka.Context) error {
		c.Response().Write([]byte("test")) // For Content-Type sniffing
		return nil
	}

	// Skip if no Accept-Encoding header
	Gzip()(h)(c)
	assert.Equal(t, http.StatusOK, rec.Code)
	assert.Equal(t, "test", rec.Body.String())

	req, _ = http.NewRequest(vodka.GET, "/", nil)
	req.Header.Set(vodka.AcceptEncoding, "gzip")
	rec = httptest.NewRecorder()
	c = vodka.NewContext(req, vodka.NewResponse(rec), vodka.New())

	// Gzip
	Gzip()(h)(c)
	assert.Equal(t, http.StatusOK, rec.Code)
	assert.Equal(t, "gzip", rec.Header().Get(vodka.ContentEncoding))
	assert.Contains(t, rec.Header().Get(vodka.ContentType), vodka.TextPlain)
	r, err := gzip.NewReader(rec.Body)
	defer r.Close()
	if assert.NoError(t, err) {
		buf := new(bytes.Buffer)
		buf.ReadFrom(r)
		assert.Equal(t, "test", buf.String())
	}
}
开发者ID:hiproz,项目名称:vodka,代码行数:32,代码来源:compress_test.go


示例2: main

func main() {
	// Host map
	hosts := make(Hosts)

	//-----
	// API
	//-----

	api := vodka.New()
	api.Use(mw.Logger())
	api.Use(mw.Recover())

	hosts["api.localhost:1323"] = api

	api.Get("/", func(c *vodka.Context) error {
		return c.String(http.StatusOK, "API")
	})

	//------
	// Blog
	//------

	blog := vodka.New()
	blog.Use(mw.Logger())
	blog.Use(mw.Recover())

	hosts["blog.localhost:1323"] = blog

	blog.Get("/", func(c *vodka.Context) error {
		return c.String(http.StatusOK, "Blog")
	})

	//---------
	// Website
	//---------

	site := vodka.New()
	site.Use(mw.Logger())
	site.Use(mw.Recover())

	hosts["localhost:1323"] = site

	site.Get("/", func(c *vodka.Context) error {
		return c.String(http.StatusOK, "Welcome!")
	})

	http.ListenAndServe(":1323", hosts)
}
开发者ID:hiproz,项目名称:vodka,代码行数:48,代码来源:server.go


示例3: main

func main() {
	// Echo instance
	e := vodka.New()

	// Debug mode
	e.Debug()

	//------------
	// Middleware
	//------------

	// Logger
	e.Use(mw.Logger())

	// Recover
	e.Use(mw.Recover())

	// Basic auth
	e.Use(mw.BasicAuth(func(usr, pwd string) bool {
		if usr == "joe" && pwd == "secret" {
			return true
		}
		return false
	}))

	// Gzip
	e.Use(mw.Gzip())

	// Routes
	e.Get("/", hello)

	// Start server
	e.Run(":1323")
}
开发者ID:hiproz,项目名称:vodka,代码行数:34,代码来源:server.go


示例4: main

func main() {
	e := vodka.New()

	// Middleware
	e.Use(middleware.Logger())
	e.Use(middleware.Recover())

	// Login route
	e.POST("/login", login)

	// Unauthenticated route
	e.GET("/", accessible)

	// Restricted group
	r := e.Group("/restricted")

	// Configure middleware with the custom claims type
	config := middleware.JWTConfig{
		Claims:     &jwtCustomClaims{},
		SigningKey: []byte("secret"),
	}
	r.Use(middleware.JWTWithConfig(config))
	r.GET("", restricted)

	e.Run(standard.New(":1323"))
}
开发者ID:insionng,项目名称:vodka,代码行数:26,代码来源:server.go


示例5: TestCORS

func TestCORS(t *testing.T) {
	e := vodka.New()
	req := test.NewRequest(vodka.GET, "/", nil)
	rec := test.NewResponseRecorder()
	c := e.NewContext(req, rec)
	cors := CORSWithConfig(CORSConfig{
		AllowCredentials: true,
	})
	h := cors(func(c vodka.Context) error {
		return c.String(http.StatusOK, "test")
	})

	// No origin header
	h(c)
	assert.Equal(t, "", rec.Header().Get(vodka.HeaderAccessControlAllowOrigin))

	// Empty origin header
	req = test.NewRequest(vodka.GET, "/", nil)
	rec = test.NewResponseRecorder()
	c = e.NewContext(req, rec)
	req.Header().Set(vodka.HeaderOrigin, "")
	h(c)
	assert.Equal(t, "*", rec.Header().Get(vodka.HeaderAccessControlAllowOrigin))

	// Wildcard origin
	req = test.NewRequest(vodka.GET, "/", nil)
	rec = test.NewResponseRecorder()
	c = e.NewContext(req, rec)
	req.Header().Set(vodka.HeaderOrigin, "localhost")
	h(c)
	assert.Equal(t, "*", rec.Header().Get(vodka.HeaderAccessControlAllowOrigin))

	// Simple request
	req = test.NewRequest(vodka.GET, "/", nil)
	rec = test.NewResponseRecorder()
	c = e.NewContext(req, rec)
	req.Header().Set(vodka.HeaderOrigin, "localhost")
	cors = CORSWithConfig(CORSConfig{
		AllowOrigins:     []string{"localhost"},
		AllowCredentials: true,
		MaxAge:           3600,
	})
	h = cors(func(c vodka.Context) error {
		return c.String(http.StatusOK, "test")
	})
	h(c)
	assert.Equal(t, "localhost", rec.Header().Get(vodka.HeaderAccessControlAllowOrigin))

	// Preflight request
	req = test.NewRequest(vodka.OPTIONS, "/", nil)
	rec = test.NewResponseRecorder()
	c = e.NewContext(req, rec)
	req.Header().Set(vodka.HeaderOrigin, "localhost")
	req.Header().Set(vodka.HeaderContentType, vodka.MIMEApplicationJSON)
	h(c)
	assert.Equal(t, "localhost", rec.Header().Get(vodka.HeaderAccessControlAllowOrigin))
	assert.NotEmpty(t, rec.Header().Get(vodka.HeaderAccessControlAllowMethods))
	assert.Equal(t, "true", rec.Header().Get(vodka.HeaderAccessControlAllowCredentials))
	assert.Equal(t, "3600", rec.Header().Get(vodka.HeaderAccessControlMaxAge))
}
开发者ID:insionng,项目名称:vodka,代码行数:60,代码来源:cors_test.go


示例6: main

func main() {
	e := vodka.New()

	// Debug mode
	e.SetDebug(true)

	//-------------------
	// Custom middleware
	//-------------------
	// Stats
	s := NewStats()
	e.Use(s.Process)
	e.GET("/stats", s.Handle) // Endpoint to get stats

	// Server header
	e.Use(ServerHeader)

	// Handler
	e.GET("/", func(c vodka.Context) error {
		return c.String(http.StatusOK, "Hello, World!")
	})

	// Start server
	e.Run(standard.New(":1323"))
}
开发者ID:insionng,项目名称:vodka,代码行数:25,代码来源:server.go


示例7: TestLoggerIPAddress

func TestLoggerIPAddress(t *testing.T) {
	e := vodka.New()
	req := test.NewRequest(vodka.GET, "/", nil)
	rec := test.NewResponseRecorder()
	c := e.NewContext(req, rec)
	buf := new(bytes.Buffer)
	e.Logger().SetOutput(buf)
	ip := "127.0.0.1"
	h := Logger()(func(c vodka.Context) error {
		return c.String(http.StatusOK, "test")
	})

	// With X-Real-IP
	req.Header().Add(vodka.HeaderXRealIP, ip)
	h(c)
	assert.Contains(t, ip, buf.String())

	// With X-Forwarded-For
	buf.Reset()
	req.Header().Del(vodka.HeaderXRealIP)
	req.Header().Add(vodka.HeaderXForwardedFor, ip)
	h(c)
	assert.Contains(t, ip, buf.String())

	buf.Reset()
	h(c)
	assert.Contains(t, ip, buf.String())
}
开发者ID:insionng,项目名称:vodka,代码行数:28,代码来源:logger_test.go


示例8: TestGzip

func TestGzip(t *testing.T) {
	e := vodka.New()
	req := test.NewRequest(vodka.GET, "/", nil)
	rec := test.NewResponseRecorder()
	c := e.NewContext(req, rec)

	// Skip if no Accept-Encoding header
	h := Gzip()(func(c vodka.Context) error {
		c.Response().Write([]byte("test")) // For Content-Type sniffing
		return nil
	})
	h(c)
	assert.Equal(t, "test", rec.Body.String())

	req = test.NewRequest(vodka.GET, "/", nil)
	req.Header().Set(vodka.HeaderAcceptEncoding, "gzip")
	rec = test.NewResponseRecorder()
	c = e.NewContext(req, rec)

	// Gzip
	h(c)
	assert.Equal(t, "gzip", rec.Header().Get(vodka.HeaderContentEncoding))
	assert.Contains(t, rec.Header().Get(vodka.HeaderContentType), vodka.MIMETextPlain)
	r, err := gzip.NewReader(rec.Body)
	defer r.Close()
	if assert.NoError(t, err) {
		buf := new(bytes.Buffer)
		buf.ReadFrom(r)
		assert.Equal(t, "test", buf.String())
	}
}
开发者ID:insionng,项目名称:vodka,代码行数:31,代码来源:compress_test.go


示例9: TestRemoveTrailingSlash

func TestRemoveTrailingSlash(t *testing.T) {
	e := vodka.New()
	req := test.NewRequest(vodka.GET, "/remove-slash/", nil)
	rec := test.NewResponseRecorder()
	c := e.NewContext(req, rec)
	h := RemoveTrailingSlash()(func(c vodka.Context) error {
		return nil
	})
	h(c)
	assert.Equal(t, "/remove-slash", req.URL().Path())
	assert.Equal(t, "/remove-slash", req.URI())

	// With config
	req = test.NewRequest(vodka.GET, "/remove-slash/?key=value", nil)
	rec = test.NewResponseRecorder()
	c = e.NewContext(req, rec)
	h = RemoveTrailingSlashWithConfig(TrailingSlashConfig{
		RedirectCode: http.StatusMovedPermanently,
	})(func(c vodka.Context) error {
		return nil
	})
	h(c)
	assert.Equal(t, http.StatusMovedPermanently, rec.Status())
	assert.Equal(t, "/remove-slash?key=value", rec.Header().Get(vodka.HeaderLocation))

	// With bare URL
	req = test.NewRequest(vodka.GET, "http://localhost", nil)
	rec = test.NewResponseRecorder()
	c = e.NewContext(req, rec)
	h = RemoveTrailingSlash()(func(c vodka.Context) error {
		return nil
	})
	h(c)
	assert.Equal(t, "", req.URL().Path())
}
开发者ID:insionng,项目名称:vodka,代码行数:35,代码来源:slash_test.go


示例10: TestSecure

func TestSecure(t *testing.T) {
	e := vodka.New()
	req := test.NewRequest(vodka.GET, "/", nil)
	rec := test.NewResponseRecorder()
	c := e.NewContext(req, rec)
	h := func(c vodka.Context) error {
		return c.String(http.StatusOK, "test")
	}

	// Default
	Secure()(h)(c)
	assert.Equal(t, "1; mode=block", rec.Header().Get(vodka.HeaderXXSSProtection))
	assert.Equal(t, "nosniff", rec.Header().Get(vodka.HeaderXContentTypeOptions))
	assert.Equal(t, "SAMEORIGIN", rec.Header().Get(vodka.HeaderXFrameOptions))
	assert.Equal(t, "", rec.Header().Get(vodka.HeaderStrictTransportSecurity))
	assert.Equal(t, "", rec.Header().Get(vodka.HeaderContentSecurityPolicy))

	// Custom
	req.Header().Set(vodka.HeaderXForwardedProto, "https")
	rec = test.NewResponseRecorder()
	c = e.NewContext(req, rec)
	SecureWithConfig(SecureConfig{
		XSSProtection:         "",
		ContentTypeNosniff:    "",
		XFrameOptions:         "",
		HSTSMaxAge:            3600,
		ContentSecurityPolicy: "default-src 'self'",
	})(h)(c)
	assert.Equal(t, "", rec.Header().Get(vodka.HeaderXXSSProtection))
	assert.Equal(t, "", rec.Header().Get(vodka.HeaderXContentTypeOptions))
	assert.Equal(t, "", rec.Header().Get(vodka.HeaderXFrameOptions))
	assert.Equal(t, "max-age=3600; includeSubdomains", rec.Header().Get(vodka.HeaderStrictTransportSecurity))
	assert.Equal(t, "default-src 'self'", rec.Header().Get(vodka.HeaderContentSecurityPolicy))
}
开发者ID:insionng,项目名称:vodka,代码行数:34,代码来源:secure_test.go


示例11: createMux

func createMux() *vodka.Vodka {
	e := vodka.New()

	// note: we don't need to provide the middleware or static handlers
	// for the appengine vm version - that's taken care of by the platform

	return e
}
开发者ID:insionng,项目名称:vodka,代码行数:8,代码来源:app-managed.go


示例12: main

func main() {
	e := vodka.New()
	e.Use(middleware.Logger())
	e.Use(middleware.Recover())
	e.Use(middleware.Static("../public"))
	e.GET("/ws", standard.WrapHandler(http.HandlerFunc(hello())))
	e.Run(standard.New(":1323"))
}
开发者ID:insionng,项目名称:vodka,代码行数:8,代码来源:server.go


示例13: main

func main() {
	// Setup
	e := vodka.New()
	e.Get("/", func(c *vodka.Context) error {
		return c.String(http.StatusOK, "Six sick bricks tick")
	})

	gracehttp.Serve(e.Server(":1323"))
}
开发者ID:hiproz,项目名称:vodka,代码行数:9,代码来源:server.go


示例14: main

func main() {
	v := vodka.New()
	v.Use(m.Logger())
	v.Use(m.Recover())
	v.Use(m.Gzip())

	v.GET("/", HelloHandler)
	v.Run(fasthttp.New(":1987"))
}
开发者ID:vodka-contrib,项目名称:helloworld,代码行数:9,代码来源:hi.go


示例15: main

func main() {
	e := vodka.New()
	e.GET("/", func(c vodka.Context) error {
		return c.String(http.StatusOK, "Six sick bricks tick")
	})
	std := standard.New(":1323")
	std.SetHandler(e)
	gracehttp.Serve(std.Server)
}
开发者ID:insionng,项目名称:vodka,代码行数:9,代码来源:server.go


示例16: TestStatic

func TestStatic(t *testing.T) {
	e := vodka.New()
	req := test.NewRequest(vodka.GET, "/", nil)
	rec := test.NewResponseRecorder()
	c := e.NewContext(req, rec)
	h := Static("../_fixture")(func(c vodka.Context) error {
		return vodka.ErrNotFound
	})

	// Directory
	if assert.NoError(t, h(c)) {
		assert.Contains(t, rec.Body.String(), "Vodka")
	}

	// HTML5 mode
	req = test.NewRequest(vodka.GET, "/client", nil)
	rec = test.NewResponseRecorder()
	c = e.NewContext(req, rec)
	static := StaticWithConfig(StaticConfig{
		Root:  "../_fixture",
		HTML5: true,
	})
	h = static(func(c vodka.Context) error {
		return vodka.ErrNotFound
	})
	if assert.NoError(t, h(c)) {
		assert.Equal(t, http.StatusOK, rec.Status())
	}

	// Browse
	req = test.NewRequest(vodka.GET, "/", nil)
	rec = test.NewResponseRecorder()
	c = e.NewContext(req, rec)
	static = StaticWithConfig(StaticConfig{
		Root:   "../_fixture/images",
		Browse: true,
	})
	h = static(func(c vodka.Context) error {
		return vodka.ErrNotFound
	})
	if assert.NoError(t, h(c)) {
		assert.Contains(t, rec.Body.String(), "walle")
	}

	// Not found
	req = test.NewRequest(vodka.GET, "/not-found", nil)
	rec = test.NewResponseRecorder()
	c = e.NewContext(req, rec)
	static = StaticWithConfig(StaticConfig{
		Root: "../_fixture/images",
	})
	h = static(func(c vodka.Context) error {
		return vodka.ErrNotFound
	})
	assert.Error(t, h(c))
}
开发者ID:insionng,项目名称:vodka,代码行数:56,代码来源:static_test.go


示例17: TestJwt

func TestJwt(t *testing.T) {
	v := vodka.New()
	v.Get("/nojwt/", NoJwtHandler)
	v.Get("/token/", TokenHandler)

	// Restricted group
	r := v.Group("/jwt/")
	r.Use(JWTAuther(Options{
		KeyFunc: func(ctx *vodka.Context) (string, error) {
			return JWTContextKey, nil
		},
	}))
	r.Any("", JwtHandler)

	recorder := httptest.NewRecorder()
	req, err := http.NewRequest("GET", "http://localhost:8000/token/", nil)
	if err != nil {
		t.Error(err)
	}

	v.ServeHTTP(recorder, req)
	token := recorder.Body.String()

	if len(token) == 0 {
		t.Error("len(token) == 0")
	} else {
		fmt.Println("token:", token)
	}

	//---------------------------------
	recorder = httptest.NewRecorder()
	req, err = http.NewRequest("GET", "http://localhost:8000/jwt/", nil)
	if err != nil {
		t.Error(err)
	}

	req.Header.Add("Authorization", "Bearer "+token)

	v.ServeHTTP(recorder, req)
	expect(t, recorder.Code, http.StatusOK)
	expect(t, recorder.Body.String(), "JwtHandler")
	//---------------------------------

	recorder = httptest.NewRecorder()
	req, err = http.NewRequest("GET", "http://localhost:8000/nojwt/", nil)
	if err != nil {
		t.Error(err)
	}

	v.ServeHTTP(recorder, req)

	expect(t, recorder.Code, http.StatusOK)
	expect(t, recorder.Body.String(), "NoJwtHandler")

}
开发者ID:vodka-contrib,项目名称:jwt,代码行数:55,代码来源:jwt_test.go


示例18: main

func main() {
	handler := http.StripPrefix(
		"/static/", http.FileServer(rice.MustFindBox("app").HTTPBox()),
	)
	e := vodka.New()
	e.Get("/static/*", func(c *vodka.Context) error {
		handler.ServeHTTP(c.Response().Writer(), c.Request())
		return nil
	})
	e.Run("localhost:1234")
}
开发者ID:hiproz,项目名称:vodka,代码行数:11,代码来源:main.go


示例19: createMux

func createMux() *vodka.Vodka {
	e := vodka.New()

	e.Use(middleware.Recover())
	e.Use(middleware.Logger())
	e.Use(middleware.Gzip())

	e.Use(middleware.Static("public"))

	return e
}
开发者ID:insionng,项目名称:vodka,代码行数:11,代码来源:app-standalone.go


示例20: main

func main() {
	e := vodka.New()

	e.Use(middleware.Logger())
	e.Use(middleware.Recover())
	e.Use(middleware.Static("public"))

	e.POST("/upload", upload)

	e.Run(standard.New(":1323"))
}
开发者ID:insionng,项目名称:vodka,代码行数:11,代码来源:server.go



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


鲜花

握手

雷人

路过

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

请发表评论

全部评论

专题导读
上一篇:
Golang vodka.Context类代码示例发布时间:2022-05-23
下一篇:
Golang manifest.Manifest类代码示例发布时间: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