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


Golang FilterContext.Serve方法代码示例

本文整理汇总了Golang中github.com/zalando/skipper/filters.FilterContext.Serve方法的典型用法代码示例。如果您正苦于以下问题:Golang FilterContext.Serve方法的具体用法?Golang FilterContext.Serve怎么用?Golang FilterContext.Serve使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在github.com/zalando/skipper/filters.FilterContext的用法示例。


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

示例1: ServeHTTP

// Creates a response from a handler and a request.
//
// It calls the handler's ServeHTTP method with an internal response
// writer that shares the status code, headers and the response body
// with the returned response. It blocks until the handler calls the
// response writer's WriteHeader, or starts writing the body, or
// returns. The written body is not buffered, but piped to the returned
// response's body.
//
// Example, a simple file server:
//
// 	var handler = http.StripPrefix(webRoot, http.FileServer(http.Dir(root)))
//
// 	func (f *myFilter) Request(ctx filters.FilterContext) {
// 		serve.ServeHTTP(ctx, handler)
// 	}
//
func ServeHTTP(ctx filters.FilterContext, h http.Handler) {
	rsp := &http.Response{Header: make(http.Header)}
	r, w := io.Pipe()
	d := &pipedResponse{
		response:   rsp,
		reader:     r,
		writer:     w,
		headerDone: make(chan struct{})}

	req := ctx.Request()
	go func() {
		h.ServeHTTP(d, req)
		select {
		case <-d.headerDone:
		default:
			d.WriteHeader(http.StatusOK)
		}

		w.CloseWithError(io.EOF)
	}()

	<-d.headerDone
	rsp.Body = d
	ctx.Serve(rsp)
}
开发者ID:zalando,项目名称:skipper,代码行数:42,代码来源:serve.go

示例2: Request

// check basic auth
func (a *basic) Request(ctx filters.FilterContext) {
	username := a.authenticator.CheckAuth(ctx.Request())

	if username == "" {
		header := http.Header{}
		header.Set(ForceBasicAuthHeaderName, a.realmDefinition)

		ctx.Serve(&http.Response{
			StatusCode: http.StatusUnauthorized,
			Header:     header,
		})
	}
}
开发者ID:zalando,项目名称:skipper,代码行数:14,代码来源:basic.go

示例3: Request

func (rc *requestCheck) Request(ctx filters.FilterContext) {
	if !rc.check(ctx.Request()) {
		ctx.Serve(&http.Response{StatusCode: http.StatusBadRequest})
	}
}
开发者ID:zalando,项目名称:skipper,代码行数:5,代码来源:diag_test.go

示例4: Request

func (b *breaker) Request(c filters.FilterContext)                       { c.Serve(b.resp) }
开发者ID:zalando,项目名称:skipper,代码行数:1,代码来源:proxy_test.go

示例5: Redirect

// Redirect implements the redirect logic as a standalone function.
func Redirect(ctx filters.FilterContext, code int, location *url.URL) {
	u := getLocation(ctx, location)
	ctx.Serve(&http.Response{
		StatusCode: code,
		Header:     http.Header{"Location": []string{u}}})
}
开发者ID:zalando,项目名称:skipper,代码行数:7,代码来源:redirect.go


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