Skip to content

Commit

Permalink
parameterize AllowMissingBatchHeader
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonbornsteinMOOV committed Oct 2, 2023
1 parent 39f3e62 commit 4a028d0
Show file tree
Hide file tree
Showing 8 changed files with 19 additions and 17 deletions.
1 change: 1 addition & 0 deletions configs/config.default.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ ACHWebViewer:
HelpfulLinks:
Corrections: "https://moov-io.github.io/ach/changes/"
Returns: "https://moov-io.github.io/ach/returns/"
AllowMissingBatchHeader: true
7 changes: 3 additions & 4 deletions pkg/filelist/achgateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"path/filepath"
"time"

"github.com/moov-io/ach"
"github.com/moov-io/ach-web-viewer/pkg/service"
)

Expand Down Expand Up @@ -96,7 +95,7 @@ func (a *achgatewayLister) getFiles(shard string) ([]File, error) {
return out, nil
}

func (a *achgatewayLister) GetFile(path string) (*File, error) {
func (a *achgatewayLister) GetFile(path string, cfg service.DisplayConfig) (*File, error) {
req, err := http.NewRequest("GET", a.endpoint+"/"+path, nil)
if err != nil {
return nil, err
Expand Down Expand Up @@ -124,15 +123,15 @@ func (a *achgatewayLister) GetFile(path string) (*File, error) {
return nil, err
}

file, err := ach.NewReader(bytes.NewReader(contents)).Read()
file, err := readFile(bytes.NewReader(contents), cfg)
if err != nil {
return nil, err
}

return &File{
Name: wrapper.Filename,
StoragePath: dir,
Contents: &file,
Contents: file,
CreatedAt: wrapper.ModTime,
Size: int64(len(contents)),
}, nil
Expand Down
4 changes: 2 additions & 2 deletions pkg/filelist/bucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ func (ls *bucketLister) GetFiles(opts ListOpts) (Files, error) {
return out, nil
}

func (ls *bucketLister) GetFile(path string) (*File, error) {
func (ls *bucketLister) GetFile(path string, cfg service.DisplayConfig) (*File, error) {
rdr, err := ls.buck.NewReader(context.Background(), path, nil)
if err != nil {
return nil, err
Expand All @@ -97,7 +97,7 @@ func (ls *bucketLister) GetFile(path string) (*File, error) {

_, name := filepath.Split(path)

file, err := readFile(bytes.NewReader(bs))
file, err := readFile(bytes.NewReader(bs), cfg)

return &File{
Name: name,
Expand Down
5 changes: 3 additions & 2 deletions pkg/filelist/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@ import (
"strings"

"github.com/moov-io/ach"
"github.com/moov-io/ach-web-viewer/pkg/service"
)

func readFile(r io.Reader) (*ach.File, error) {
func readFile(r io.Reader, cfg service.DisplayConfig) (*ach.File, error) {
reader := ach.NewReader(r)
reader.SetValidation(&ach.ValidateOpts{
AllowMissingBatchHeader: true,
AllowMissingBatchHeader: cfg.AllowMissingBatchHeader,
})
file, err := reader.Read()
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions pkg/filelist/filesystem.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func (ls *filesystemLister) GetFiles(opts ListOpts) (Files, error) {
return out, nil
}

func (ls *filesystemLister) GetFile(path string) (*File, error) {
func (ls *filesystemLister) GetFile(path string, cfg service.DisplayConfig) (*File, error) {
path = filepath.Clean(path)

if strings.Contains(path, "..") || strings.HasPrefix(path, "/") {
Expand All @@ -75,7 +75,7 @@ func (ls *filesystemLister) GetFile(path string) (*File, error) {

_, name := filepath.Split(fd.Name())

file, err := readFile(fd)
file, err := readFile(fd, cfg)

var stat fs.FileInfo
if fd != nil {
Expand Down
6 changes: 3 additions & 3 deletions pkg/filelist/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ type File struct {
type Lister interface {
SourceID() string

GetFile(path string) (*File, error)
GetFile(path string, cfg service.DisplayConfig) (*File, error)
GetFiles(opts ListOpts) (Files, error)
}

Expand Down Expand Up @@ -71,10 +71,10 @@ func createLister(src service.Source) (Lister, error) {
return nil, fmt.Errorf("unknown source: %#v", src)
}

func (ls Listers) GetFile(sourceID, path string) (*File, error) {
func (ls Listers) GetFile(sourceID, path string, cfg service.DisplayConfig) (*File, error) {
for i := range ls {
if ls[i].SourceID() == sourceID {
return ls[i].GetFile(path)
return ls[i].GetFile(path, cfg)
}
}
return nil, fmt.Errorf("%s not found for sourceID=%s", path, sourceID)
Expand Down
7 changes: 4 additions & 3 deletions pkg/service/model_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,10 @@ type BindAddress struct {
}

type DisplayConfig struct {
Format string // e.g. "human-readable"
Masking MaskingConfig
HelpfulLinks HelpfulLinks
Format string // e.g. "human-readable"
Masking MaskingConfig
HelpfulLinks HelpfulLinks
AllowMissingBatchHeader bool
}

type MaskingConfig struct {
Expand Down
2 changes: 1 addition & 1 deletion pkg/web/web.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ func getFile(logger log.Logger, cfg service.DisplayConfig, listers filelist.List
sourceID := mux.Vars(r)["sourceID"]
fullPath := strings.TrimPrefix(r.URL.Path, fmt.Sprintf("%s/sources/%s/", basePath, sourceID))

file, err := listers.GetFile(sourceID, fullPath)
file, err := listers.GetFile(sourceID, fullPath, cfg)
if err != nil {
logger.Warn().Logf("ERROR: %v\n", err)
w.WriteHeader(http.StatusInternalServerError)
Expand Down

0 comments on commit 4a028d0

Please sign in to comment.