Skip to content

Commit 57745b2

Browse files
committed
feat: read files from INITIAL_DATA_DIRECTORY
1 parent 3c88a3e commit 57745b2

File tree

3 files changed

+99
-16
lines changed

3 files changed

+99
-16
lines changed

README.md

+15-12
Original file line numberDiff line numberDiff line change
@@ -188,18 +188,21 @@ PONG
188188

189189
### Configuration settings
190190

191-
| Environmental Variable | Description | Default |
192-
|-----|-------------------------------------------------------------------------------------------------------------------------------------|-----|
193-
| `FEDACH_DATA_PATH` | Filepath to FedACH data file | `./data/FedACHdir.txt` |
194-
| `FEDWIRE_DATA_PATH` | Filepath to Fedwire data file | `./data/fpddir.txt` |
195-
| `FRB_ROUTING_NUMBER` | Federal Reserve Board eServices (ABA) routing number used to download FedACH and FedWire files | Empty |
196-
| `FRB_DOWNLOAD_CODE` | Federal Reserve Board eServices (ABA) download code used to download FedACH and FedWire files | Empty |
197-
| `FRB_DOWNLOAD_URL_TEMPLATE` | URL Template for downloading files from alternate source | `https://frbservices.org/EPaymentsDirectory/directories/%s?format=json`|
198-
| `LOG_FORMAT` | Format for logging lines to be written as. | Options: `json`, `plain` - Default: `plain` |
199-
| `HTTP_BIND_ADDRESS` | Address for Fed to bind its HTTP server on. This overrides the command-line flag `-http.addr`. | Default: `:8086` |
200-
| `HTTP_ADMIN_BIND_ADDRESS` | Address for Fed to bind its admin HTTP server on. This overrides the command-line flag `-admin.addr`. | Default: `:9096` |
201-
| `HTTPS_CERT_FILE` | Filepath containing a certificate (or intermediate chain) to be served by the HTTP server. Requires all traffic be over secure HTTP. | Empty |
202-
| `HTTPS_KEY_FILE` | Filepath of a private key matching the leaf certificate from `HTTPS_CERT_FILE`. | Empty |
191+
| Environmental Variable | Description | Default |
192+
|-----------------------------|--------------------------------------------------------------------------------------------------------------------------------------|-------------------------------------------------------------------------|
193+
| `FEDACH_DATA_PATH` | Filepath to FedACH data file | `./data/FedACHdir.txt` |
194+
| `FEDWIRE_DATA_PATH` | Filepath to Fedwire data file | `./data/fpddir.txt` |
195+
| `INITIAL_DATA_DIRECTORY` | Directory of files to be used instead of downloading or `*_DATA_PATH` variables. | Empty |
196+
|   | ACH: FedACHdir.txt, fedachdir.json, fedach.txt, fedach.json |   |
197+
|   | Wire: fpddir.json, fpddir.txt, fedwire.txt, fedwire.json |   |
198+
| `FRB_ROUTING_NUMBER` | Federal Reserve Board eServices (ABA) routing number used to download FedACH and FedWire files | Empty |
199+
| `FRB_DOWNLOAD_CODE` | Federal Reserve Board eServices (ABA) download code used to download FedACH and FedWire files | Empty |
200+
| `FRB_DOWNLOAD_URL_TEMPLATE` | URL Template for downloading files from alternate source | `https://frbservices.org/EPaymentsDirectory/directories/%s?format=json` |
201+
| `LOG_FORMAT` | Format for logging lines to be written as. | Options: `json`, `plain` - Default: `plain` |
202+
| `HTTP_BIND_ADDRESS` | Address for Fed to bind its HTTP server on. This overrides the command-line flag `-http.addr`. | Default: `:8086` |
203+
| `HTTP_ADMIN_BIND_ADDRESS` | Address for Fed to bind its admin HTTP server on. This overrides the command-line flag `-admin.addr`. | Default: `:9096` |
204+
| `HTTPS_CERT_FILE` | Filepath containing a certificate (or intermediate chain) to be served by the HTTP server. Requires all traffic be over secure HTTP. | Empty |
205+
| `HTTPS_KEY_FILE` | Filepath of a private key matching the leaf certificate from `HTTPS_CERT_FILE`. | Empty |
203206

204207
### Data persistence
205208
By design, Fed **does not persist** (save) any data about the search queries created. The only storage occurs in memory of the process and upon restart Fed will have no files or data saved. Also, no in-memory encryption of the data is performed.

cmd/server/reader.go

+57-4
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,37 @@ import (
99
"fmt"
1010
"io"
1111
"os"
12+
"path/filepath"
13+
"strings"
1214

1315
"github.com/moov-io/base/log"
1416
"github.com/moov-io/fed"
1517
"github.com/moov-io/fed/pkg/download"
1618
)
1719

20+
var (
21+
fedachFilenames = []string{"FedACHdir.txt", "fedachdir.json", "fedach.txt", "fedach.json"}
22+
fedwireFilenames = []string{"fpddir.json", "fpddir.txt", "fedwire.txt", "fedwire.json"}
23+
)
24+
1825
func fedACHDataFile(logger log.Logger) (io.Reader, error) {
19-
file, err := attemptFileDownload(logger, "fedach")
26+
initialDir := os.Getenv("INITIAL_DATA_DIRECTORY")
27+
file, err := inspectInitialDataDirectory(logger, initialDir, fedachFilenames)
28+
if err != nil {
29+
return nil, fmt.Errorf("inspecting %s for FedACH file failed: %w", initialDir, err)
30+
}
31+
if file != nil {
32+
logger.Info().Logf("found FedACH file in %s", initialDir)
33+
return file, nil
34+
}
35+
36+
file, err = attemptFileDownload(logger, "fedach")
2037
if err != nil && !errors.Is(err, download.ErrMissingConfigValue) {
2138
return nil, fmt.Errorf("problem downloading fedach: %v", err)
2239
}
2340

2441
if file != nil {
2542
logger.Info().Log("search: downloaded ACH file")
26-
2743
return file, nil
2844
}
2945

@@ -38,14 +54,23 @@ func fedACHDataFile(logger log.Logger) (io.Reader, error) {
3854
}
3955

4056
func fedWireDataFile(logger log.Logger) (io.Reader, error) {
41-
file, err := attemptFileDownload(logger, "fedwire")
57+
initialDir := os.Getenv("INITIAL_DATA_DIRECTORY")
58+
file, err := inspectInitialDataDirectory(logger, initialDir, fedwireFilenames)
59+
if err != nil {
60+
return nil, fmt.Errorf("inspecting %s for FedWire file failed: %w", initialDir, err)
61+
}
62+
if file != nil {
63+
logger.Info().Logf("found FedWire file in %s", initialDir)
64+
return file, nil
65+
}
66+
67+
file, err = attemptFileDownload(logger, "fedwire")
4268
if err != nil && !errors.Is(err, download.ErrMissingConfigValue) {
4369
return nil, fmt.Errorf("problem downloading fedwire: %v", err)
4470
}
4571

4672
if file != nil {
4773
logger.Info().Log("search: downloaded Wire file")
48-
4974
return file, nil
5075
}
5176

@@ -59,6 +84,34 @@ func fedWireDataFile(logger log.Logger) (io.Reader, error) {
5984
return file, nil
6085
}
6186

87+
func inspectInitialDataDirectory(logger log.Logger, dir string, needles []string) (io.Reader, error) {
88+
entries, err := os.ReadDir(dir)
89+
if err != nil {
90+
if os.IsNotExist(err) {
91+
return nil, nil
92+
}
93+
return nil, fmt.Errorf("readdir on %s failed: %w", dir, err)
94+
}
95+
96+
for _, entry := range entries {
97+
_, filename := filepath.Split(entry.Name())
98+
99+
for idx := range needles {
100+
if strings.EqualFold(filename, needles[idx]) {
101+
where := filepath.Join(dir, entry.Name())
102+
103+
fd, err := os.Open(where)
104+
if err != nil {
105+
return nil, fmt.Errorf("opening %s failed: %w", where, err)
106+
}
107+
return fd, nil
108+
}
109+
}
110+
}
111+
112+
return nil, nil
113+
}
114+
62115
func attemptFileDownload(logger log.Logger, listName string) (io.Reader, error) {
63116
logger.Logf("download: attempting %s", listName)
64117
client, err := download.NewClient(nil)

cmd/server/reader_test.go

+27
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,33 @@ func TestReader__fedWireDataFile(t *testing.T) {
3232
require.ErrorContains(t, err, "no such file or directory")
3333
}
3434

35+
func TestReader_inspectInitialDataDirectory(t *testing.T) {
36+
logger := log.NewNopLogger()
37+
38+
dir := t.TempDir()
39+
40+
err := os.WriteFile(filepath.Join(dir, "fedach.txt"), nil, 0600)
41+
require.NoError(t, err)
42+
err = os.WriteFile(filepath.Join(dir, "fedwire.txt"), nil, 0600)
43+
require.NoError(t, err)
44+
45+
// FedACH files
46+
fd, err := inspectInitialDataDirectory(logger, dir, fedachFilenames)
47+
require.NoError(t, err)
48+
49+
file, ok := fd.(*os.File)
50+
require.True(t, ok)
51+
require.Equal(t, filepath.Join(dir, "fedach.txt"), file.Name())
52+
53+
// FedWire files
54+
fd, err = inspectInitialDataDirectory(logger, dir, fedwireFilenames)
55+
require.NoError(t, err)
56+
57+
file, ok = fd.(*os.File)
58+
require.True(t, ok)
59+
require.Equal(t, filepath.Join(dir, "fedwire.txt"), file.Name())
60+
}
61+
3562
func TestReader__readFEDACHData(t *testing.T) {
3663
s := &searcher{logger: log.NewNopLogger()}
3764

0 commit comments

Comments
 (0)