From e17c2fc8b10e7bcc6ca299e27de8843afecfb653 Mon Sep 17 00:00:00 2001 From: yanzuoguang Date: Tue, 4 Nov 2025 21:06:57 +0800 Subject: [PATCH] =?UTF-8?q?refactor(pipeline):=E9=87=8D=E6=9E=84=E7=AE=A1?= =?UTF-8?q?=E9=81=93=E7=BB=84=E4=BB=B6=E6=8E=A5=E5=8F=A3=E4=BB=A5=E6=94=AF?= =?UTF-8?q?=E6=8C=81=E6=A8=A1=E6=9D=BF=E6=89=A7=E8=A1=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 修改了 SimpleMessageComponent以使用模板执行器渲染消息 - 移除了对 io.WriteString 的直接调用 - 更新了 CreatePipeline 函数签名以接受 interface{} 类型- 调整了 createServiceDependentFunction 参数类型为 interface{} - 简化了中间件组件处理逻辑,移除冗余类型断言 --- 32-platform/platform/pipeline/pipeline.go | 6 ++-- .../placeholder/message_middleware.go | 28 ++++++++----------- 2 files changed, 14 insertions(+), 20 deletions(-) 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) }