-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpipeline_step_static_file.go
More file actions
90 lines (74 loc) · 2.44 KB
/
pipeline_step_static_file.go
File metadata and controls
90 lines (74 loc) · 2.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package module
import (
"context"
"fmt"
"net/http"
"os"
"github.com/CrisisTextLine/modular"
"github.com/GoCodeAlone/workflow/config"
)
// StaticFileStep serves a pre-loaded file from disk as an HTTP response.
// The file is read at init time (factory creation) for performance.
type StaticFileStep struct {
name string
content []byte
contentType string
cacheControl string
}
// NewStaticFileStepFactory returns a StepFactory that creates StaticFileStep instances.
// The file is read from disk when the factory is invoked (at config load time).
func NewStaticFileStepFactory() StepFactory {
return func(name string, cfg map[string]any, _ modular.Application) (PipelineStep, error) {
filePath, _ := cfg["file"].(string)
if filePath == "" {
return nil, fmt.Errorf("static_file step %q: 'file' is required", name)
}
contentType, _ := cfg["content_type"].(string)
if contentType == "" {
return nil, fmt.Errorf("static_file step %q: 'content_type' is required", name)
}
// Resolve file path relative to the config file directory.
resolved := config.ResolvePathInConfig(cfg, filePath)
content, err := os.ReadFile(resolved)
if err != nil {
return nil, fmt.Errorf("static_file step %q: failed to read file %q: %w", name, resolved, err)
}
cacheControl, _ := cfg["cache_control"].(string)
return &StaticFileStep{
name: name,
content: content,
contentType: contentType,
cacheControl: cacheControl,
}, nil
}
}
func (s *StaticFileStep) Name() string { return s.name }
func (s *StaticFileStep) Execute(_ context.Context, pc *PipelineContext) (*StepResult, error) {
w, ok := pc.Metadata["_http_response_writer"].(http.ResponseWriter)
if !ok {
// No HTTP response writer — return content as output without writing HTTP.
output := map[string]any{
"content_type": s.contentType,
"body": string(s.content),
}
if s.cacheControl != "" {
output["cache_control"] = s.cacheControl
}
return &StepResult{Output: output, Stop: true}, nil
}
w.Header().Set("Content-Type", s.contentType)
if s.cacheControl != "" {
w.Header().Set("Cache-Control", s.cacheControl)
}
w.WriteHeader(http.StatusOK)
if _, err := w.Write(s.content); err != nil {
return nil, fmt.Errorf("static_file step %q: failed to write response: %w", s.name, err)
}
pc.Metadata["_response_handled"] = true
return &StepResult{
Output: map[string]any{
"content_type": s.contentType,
},
Stop: true,
}, nil
}