代码拉取完成,页面将自动刷新
package thinkgo
import (
"container/list"
"html/template"
"net/http"
"github.com/forgoer/thinkgo/context"
"github.com/forgoer/thinkgo/router"
"github.com/forgoer/thinkgo/think"
)
type Pipeline struct {
handlers []think.Handler
pipeline *list.List
passable *context.Request
}
// Pipeline returns a new Pipeline
func NewPipeline() *Pipeline {
p := &Pipeline{
pipeline: list.New(),
}
return p
}
// Pipe Push a Middleware Handler to the pipeline
func (p *Pipeline) Pipe(m think.Handler) *Pipeline {
p.pipeline.PushBack(m)
return p
}
// Pipe Batch push Middleware Handlers to the pipeline
func (p *Pipeline) Through(hls []think.Handler) *Pipeline {
for _, hl := range hls {
p.Pipe(hl)
}
return p
}
// Passable set the request being sent through the pipeline.
func (p *Pipeline) Passable(passable *context.Request) *Pipeline {
p.passable = passable
return p
}
// Run run the pipeline
func (p *Pipeline) Run() interface{} {
var result interface{}
e := p.pipeline.Front()
if e != nil {
result = p.handler(p.passable, e)
}
return result
}
// ServeHTTP
func (p *Pipeline) ServeHTTP(w http.ResponseWriter, r *http.Request) {
request := context.NewRequest(r)
request.CookieHandler = context.ParseCookieHandler()
p.Passable(request)
result := p.Run()
switch result.(type) {
case router.Response:
result.(router.Response).Send(w)
break
case template.HTML:
think.Html(string(result.(template.HTML))).Send(w)
break
case http.Handler:
result.(http.Handler).ServeHTTP(w, r)
break
default:
think.Response(result).Send(w)
break
}
}
func (p *Pipeline) handler(passable *context.Request, e *list.Element) interface{} {
if e == nil {
return nil
}
hl := e.Value.(think.Handler)
result := hl.Process(passable, p.closure(e))
return result
}
func (p *Pipeline) closure(e *list.Element) think.Closure {
return func(req *context.Request) interface{} {
e = e.Next()
return p.handler(req, e)
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。