package processor import ( "bytes" "context" "fmt" "text/template" "github.com/jwetzell/showbridge-go/internal/config" ) type HTTPResponseCreate struct { Status int BodyTmpl *template.Template config config.ProcessorConfig } type HTTPResponse struct { Status int Body []byte } func (hrc *HTTPResponseCreate) Process(ctx context.Context, payload any) (any, error) { templateData := GetTemplateData(ctx, payload) var bodyBuffer bytes.Buffer err := hrc.BodyTmpl.Execute(&bodyBuffer, templateData) if err != nil { return nil, err } return HTTPResponse{ Status: hrc.Status, Body: bodyBuffer.Bytes(), }, nil } func (hrc *HTTPResponseCreate) Type() string { return hrc.config.Type } func init() { RegisterProcessor(ProcessorRegistration{ Type: "http.response.create", New: func(config config.ProcessorConfig) (Processor, error) { params := config.Params statusNum, err := params.GetInt("status") if err != nil { return nil, fmt.Errorf("http.response.create status error: %w", err) } bodyTemplateString, err := params.GetString("bodyTemplate") if err != nil { return nil, fmt.Errorf("http.response.create bodyTemplate error: %w", err) } bodyTemplate, err := template.New("body").Parse(bodyTemplateString) if err != nil { return nil, err } // TODO(jwetzell): support other body kind (direct bytes from input, from file?) return &HTTPResponseCreate{config: config, Status: int(statusNum), BodyTmpl: bodyTemplate}, nil }, }) }