Skip to content

Commit ff28f39

Browse files
committed
filediscovery: support discovering applications from file
1 parent 3a2638c commit ff28f39

3 files changed

Lines changed: 67 additions & 0 deletions

File tree

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// Discover applications from a JSON "config" file
2+
package filediscovery
3+
4+
import (
5+
"context"
6+
7+
"github.com/function61/edgerouter/pkg/erconfig"
8+
"github.com/function61/edgerouter/pkg/erdiscovery"
9+
"github.com/function61/gokit/jsonfile"
10+
)
11+
12+
const (
13+
DefaultFilename = "applications.json"
14+
)
15+
16+
// file format for discovery file.
17+
// NOTE: not having applications at top level for extensibility + prepare for `$schema`
18+
type file struct {
19+
Apps []erconfig.Application `json:"apps"`
20+
}
21+
22+
type fileDiscovery struct {
23+
file string
24+
}
25+
26+
var _ erdiscovery.Reader = (*fileDiscovery)(nil)
27+
28+
func New(file string) erdiscovery.Reader {
29+
return &fileDiscovery{file}
30+
}
31+
32+
func (f *fileDiscovery) ReadApplications(_ context.Context) ([]erconfig.Application, error) {
33+
appsFromFile := file{}
34+
if err := jsonfile.Read(f.file, &appsFromFile, true); err != nil {
35+
return nil, err
36+
}
37+
return appsFromFile.Apps, nil
38+
}

pkg/erserver/serve.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"github.com/function61/edgerouter/pkg/erdiscovery"
2222
"github.com/function61/edgerouter/pkg/erdiscovery/dockerdiscovery"
2323
"github.com/function61/edgerouter/pkg/erdiscovery/ehdiscovery"
24+
"github.com/function61/edgerouter/pkg/erdiscovery/filediscovery"
2425
"github.com/function61/edgerouter/pkg/erdiscovery/s3discovery"
2526
"github.com/function61/edgerouter/pkg/todoupgradegokit"
2627
"github.com/function61/edgerouter/pkg/turbocharger"
@@ -335,6 +336,15 @@ func configureDiscovery(logger *log.Logger) (erdiscovery.Reader, error) {
335336
readers = append(readers, ehDiscovery)
336337
}
337338

339+
maybeFromFile, err := newFileDiscoveryIfFileExists(filediscovery.DefaultFilename)
340+
if err != nil {
341+
return nil, err
342+
}
343+
344+
if maybeFromFile != nil {
345+
readers = append(readers, maybeFromFile)
346+
}
347+
338348
return erdiscovery.MultiDiscovery(readers), nil
339349
}
340350

pkg/erserver/utils.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ import (
55
"net"
66
"net/http"
77
"strings"
8+
9+
"github.com/function61/edgerouter/pkg/erdiscovery"
10+
"github.com/function61/edgerouter/pkg/erdiscovery/filediscovery"
11+
"github.com/function61/gokit/fileexists"
812
)
913

1014
// net.SplitHostPort() does not support case where port is not defined...
@@ -48,3 +52,18 @@ func cancelableServer(ctx context.Context, srv *http.Server, listener func() err
4852
return err
4953
}
5054
}
55+
56+
// like its `New()` but don't error if file doesn't exist.
57+
// (still errors if existence check fails)
58+
func newFileDiscoveryIfFileExists(path string) (erdiscovery.Reader, error) {
59+
exists, err := fileexists.Exists(path)
60+
if err != nil {
61+
return nil, err
62+
}
63+
64+
if exists {
65+
return filediscovery.New(path), nil
66+
} else {
67+
return nil, nil
68+
}
69+
}

0 commit comments

Comments
 (0)