-
Notifications
You must be signed in to change notification settings - Fork 0
Add new step type: step.static_file for serving files from disk #250
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
0b1dcf1
Initial plan
Copilot 97951e5
feat: add step.static_file pipeline step type for serving files from …
Copilot 080c9f6
Merge branch 'main' into copilot/add-static-file-step-type
intel352 c159f2b
fix: address PR review comments on step.static_file tests and schema
Copilot c9bc3cf
Merge branch 'main' into copilot/add-static-file-step-type
intel352 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,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 | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,171 @@ | ||
| package module | ||
|
|
||
| import ( | ||
| "context" | ||
| "io" | ||
| "net/http/httptest" | ||
| "os" | ||
| "path/filepath" | ||
| "testing" | ||
| ) | ||
|
|
||
| func TestStaticFileStep_ServesFile(t *testing.T) { | ||
| // Write a temporary file to serve. | ||
| dir := t.TempDir() | ||
| filePath := filepath.Join(dir, "spec.yaml") | ||
| content := "openapi: 3.0.0\ninfo:\n title: Test\n" | ||
| if err := os.WriteFile(filePath, []byte(content), 0o600); err != nil { | ||
| t.Fatalf("write temp file: %v", err) | ||
| } | ||
|
|
||
| factory := NewStaticFileStepFactory() | ||
| step, err := factory("serve_spec", map[string]any{ | ||
| "file": filePath, | ||
| "content_type": "application/yaml", | ||
| "cache_control": "public, max-age=3600", | ||
| }, nil) | ||
| if err != nil { | ||
| t.Fatalf("factory error: %v", err) | ||
| } | ||
|
|
||
| recorder := httptest.NewRecorder() | ||
| pc := NewPipelineContext(nil, map[string]any{ | ||
| "_http_response_writer": recorder, | ||
| }) | ||
|
|
||
| result, err := step.Execute(context.Background(), pc) | ||
| if err != nil { | ||
| t.Fatalf("execute error: %v", err) | ||
| } | ||
|
|
||
| if !result.Stop { | ||
| t.Error("expected Stop=true") | ||
| } | ||
|
|
||
| resp := recorder.Result() | ||
| if resp.StatusCode != 200 { | ||
| t.Errorf("expected status 200, got %d", resp.StatusCode) | ||
| } | ||
| if ct := resp.Header.Get("Content-Type"); ct != "application/yaml" { | ||
| t.Errorf("expected Content-Type application/yaml, got %q", ct) | ||
| } | ||
| if cc := resp.Header.Get("Cache-Control"); cc != "public, max-age=3600" { | ||
| t.Errorf("expected Cache-Control header, got %q", cc) | ||
| } | ||
|
|
||
| body, err := io.ReadAll(resp.Body) | ||
| resp.Body.Close() | ||
| if err != nil { | ||
| t.Fatalf("read response body: %v", err) | ||
| } | ||
| if string(body) != content { | ||
| t.Errorf("expected body %q, got %q", content, string(body)) | ||
| } | ||
|
|
||
| if pc.Metadata["_response_handled"] != true { | ||
| t.Error("expected _response_handled=true") | ||
| } | ||
| } | ||
|
|
||
| func TestStaticFileStep_NoHTTPWriter(t *testing.T) { | ||
| dir := t.TempDir() | ||
| filePath := filepath.Join(dir, "data.json") | ||
| content := `{"key":"value"}` | ||
| if err := os.WriteFile(filePath, []byte(content), 0o600); err != nil { | ||
| t.Fatalf("write temp file: %v", err) | ||
| } | ||
|
|
||
| factory := NewStaticFileStepFactory() | ||
| step, err := factory("serve_json", map[string]any{ | ||
| "file": filePath, | ||
| "content_type": "application/json", | ||
| }, nil) | ||
| if err != nil { | ||
| t.Fatalf("factory error: %v", err) | ||
| } | ||
|
|
||
| pc := NewPipelineContext(nil, map[string]any{}) | ||
| result, err := step.Execute(context.Background(), pc) | ||
| if err != nil { | ||
| t.Fatalf("execute error: %v", err) | ||
| } | ||
|
|
||
| if !result.Stop { | ||
| t.Error("expected Stop=true") | ||
| } | ||
| if result.Output["body"] != content { | ||
| t.Errorf("expected body %q, got %q", content, result.Output["body"]) | ||
| } | ||
| if result.Output["content_type"] != "application/json" { | ||
| t.Errorf("unexpected content_type: %v", result.Output["content_type"]) | ||
| } | ||
| } | ||
|
|
||
| func TestStaticFileStep_ConfigRelativePath(t *testing.T) { | ||
| // Write a temporary file to serve via a relative path resolved from _config_dir. | ||
| dir := t.TempDir() | ||
| filePath := filepath.Join(dir, "spec.yaml") | ||
| content := "openapi: 3.0.0\n" | ||
| if err := os.WriteFile(filePath, []byte(content), 0o600); err != nil { | ||
| t.Fatalf("write temp file: %v", err) | ||
| } | ||
|
|
||
| factory := NewStaticFileStepFactory() | ||
| // Pass relative file name + _config_dir so ResolvePathInConfig joins them. | ||
| step, err := factory("serve_spec", map[string]any{ | ||
| "file": "spec.yaml", | ||
| "content_type": "application/yaml", | ||
| "_config_dir": dir, | ||
| }, nil) | ||
| if err != nil { | ||
| t.Fatalf("factory error: %v", err) | ||
| } | ||
|
|
||
| pc := NewPipelineContext(nil, map[string]any{}) | ||
| result, err := step.Execute(context.Background(), pc) | ||
| if err != nil { | ||
| t.Fatalf("execute error: %v", err) | ||
| } | ||
|
|
||
| if result.Output["body"] != content { | ||
| t.Errorf("expected body %q, got %q", content, result.Output["body"]) | ||
| } | ||
| } | ||
|
|
||
| func TestStaticFileStep_MissingFile(t *testing.T) { | ||
| factory := NewStaticFileStepFactory() | ||
| _, err := factory("bad_step", map[string]any{ | ||
| "file": "", | ||
| "content_type": "text/plain", | ||
| }, nil) | ||
| if err == nil { | ||
| t.Error("expected error for missing 'file' config") | ||
| } | ||
| } | ||
|
|
||
| func TestStaticFileStep_MissingContentType(t *testing.T) { | ||
| dir := t.TempDir() | ||
| filePath := filepath.Join(dir, "data.txt") | ||
| if err := os.WriteFile(filePath, []byte("hello"), 0o600); err != nil { | ||
| t.Fatalf("write temp file: %v", err) | ||
| } | ||
|
|
||
| factory := NewStaticFileStepFactory() | ||
| _, err := factory("bad_step", map[string]any{ | ||
| "file": filePath, | ||
| }, nil) | ||
| if err == nil { | ||
| t.Error("expected error for missing 'content_type' config") | ||
| } | ||
| } | ||
|
|
||
| func TestStaticFileStep_FileNotFound(t *testing.T) { | ||
| factory := NewStaticFileStepFactory() | ||
| _, err := factory("bad_step", map[string]any{ | ||
| "file": "/nonexistent/path/file.yaml", | ||
| "content_type": "application/yaml", | ||
| }, nil) | ||
| if err == nil { | ||
| t.Error("expected error for non-existent file") | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The tests don’t cover the advertised config-relative path resolution via
_config_dir/config.ResolvePathInConfig(i.e., passing a relativefilepath and setting_config_dirin the step config). Adding a test for this would guard the primary feature this step is introducing.