refactor(pipeline):重构管道组件接口以支持模板执行

- 修改了 SimpleMessageComponent以使用模板执行器渲染消息
- 移除了对 io.WriteString 的直接调用
- 更新了 CreatePipeline 函数签名以接受 interface{} 类型- 调整了 createServiceDependentFunction 参数类型为 interface{}
- 简化了中间件组件处理逻辑,移除冗余类型断言
This commit is contained in:
2025-11-04 21:06:57 +08:00
parent d80846ea37
commit e17c2fc8b1
2 changed files with 14 additions and 20 deletions

View File

@@ -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) {

View File

@@ -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)
}