本文整理匯總了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)
}
示例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,
})
}
}
示例3: Request
func (rc *requestCheck) Request(ctx filters.FilterContext) {
if !rc.check(ctx.Request()) {
ctx.Serve(&http.Response{StatusCode: http.StatusBadRequest})
}
}
示例4: Request
func (b *breaker) Request(c filters.FilterContext) { c.Serve(b.resp) }
示例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}}})
}