diff --git a/32-platform/platform/pipeline/pipeline.go b/32-platform/platform/pipeline/pipeline.go index 470bd36..6683fdc 100644 --- a/32-platform/platform/pipeline/pipeline.go +++ b/32-platform/platform/pipeline/pipeline.go @@ -13,7 +13,7 @@ var emptyPipeline RequestPipeline = func(*ComponentContext) { } // CreatePipeline 创建管道 -func CreatePipeline(components ...MiddlewareComponent) RequestPipeline { +func CreatePipeline(components ...interface{}) RequestPipeline { f := emptyPipeline for i := len(components) - 1; i >= 0; i-- { nextFunc := f @@ -31,7 +31,7 @@ func CreatePipeline(components ...MiddlewareComponent) RequestPipeline { } else if stdComp, ok := currentComponent.(MiddlewareComponent); ok { f = func(context *ComponentContext) { if context.error == nil { - currentComponent.ProcessRequest(context, nextFunc) + stdComp.ProcessRequest(context, nextFunc) } } stdComp.Init() @@ -42,7 +42,7 @@ func CreatePipeline(components ...MiddlewareComponent) RequestPipeline { return f } -func createServiceDependentFunction(component MiddlewareComponent, nextFunc RequestPipeline) RequestPipeline { +func createServiceDependentFunction(component interface{}, nextFunc RequestPipeline) RequestPipeline { method := reflect.ValueOf(component).MethodByName("ProcessRequestWithServices") if method.IsValid() { return func(context *ComponentContext) { diff --git a/32-platform/platform/placeholder/message_middleware.go b/32-platform/platform/placeholder/message_middleware.go index f6b6058..10e6645 100644 --- a/32-platform/platform/placeholder/message_middleware.go +++ b/32-platform/platform/placeholder/message_middleware.go @@ -1,37 +1,31 @@ package placeholder import ( - "errors" - "io" "platform/config" "platform/pipeline" - "platform/services" + "platform/templates" ) type SimpleMessageComponent struct { + Message string + config.Configuration +} + +func (c *SimpleMessageComponent) ImplementsProcessRequestWithServices() { } func (c *SimpleMessageComponent) Init() { + c.Message = c.Configuration.GetStringDefault("main:message", "Default Message") } func (c *SimpleMessageComponent) ProcessRequest( ctx *pipeline.ComponentContext, - next func(*pipeline.ComponentContext)) { - var cfg config.Configuration - err := services.GetService(&cfg) + next func(*pipeline.ComponentContext), + executor templates.TemplateExecutor) { + err := executor.Execute(ctx.ResponseWriter, "simple_message.html", c.Message) if err != nil { ctx.Error(err) - return - } - msg, ok := cfg.GetString("main:message") - if ok { - _, err := io.WriteString(ctx.ResponseWriter, msg) - if err != nil { - ctx.Error(err) - return - } } else { - ctx.Error(errors.New("cannot find config setting")) + next(ctx) } - next(ctx) }