From 00283bec8767852867e51723d50d33d5fa02d71e Mon Sep 17 00:00:00 2001 From: JP Hastings-Spital Date: Sun, 12 Nov 2023 09:47:58 +0000 Subject: [PATCH] Centralised service management --- tools/syndicate/backfeeder/backfeeder.go | 8 ++++-- tools/syndicate/config.go | 7 ++--- tools/syndicate/main.go | 2 +- tools/syndicate/poster/poster.go | 11 +++----- tools/syndicate/services/tracker.go | 35 ++++++++++++++++++++++++ 5 files changed, 48 insertions(+), 15 deletions(-) create mode 100644 tools/syndicate/services/tracker.go diff --git a/tools/syndicate/backfeeder/backfeeder.go b/tools/syndicate/backfeeder/backfeeder.go index 0ae36c1e..1aefcb45 100644 --- a/tools/syndicate/backfeeder/backfeeder.go +++ b/tools/syndicate/backfeeder/backfeeder.go @@ -1,13 +1,15 @@ package backfeeder -import "github.com/by-jp/www.byjp.me/tools/syndicate/shared" +import ( + "github.com/by-jp/www.byjp.me/tools/syndicate/services" +) type backfeeder struct { - services map[string]shared.Service + services *services.List done map[string]struct{} } -func New(services map[string]shared.Service) *backfeeder { +func New(services *services.List) *backfeeder { return &backfeeder{ services: services, done: make(map[string]struct{}), diff --git a/tools/syndicate/config.go b/tools/syndicate/config.go index ad8eaed5..4f460dd6 100644 --- a/tools/syndicate/config.go +++ b/tools/syndicate/config.go @@ -8,6 +8,7 @@ import ( "strings" "github.com/BurntSushi/toml" + "github.com/by-jp/www.byjp.me/tools/syndicate/services" "github.com/by-jp/www.byjp.me/tools/syndicate/shared" "github.com/bmatcuk/doublestar/v4" @@ -30,7 +31,7 @@ type fileConfig struct { type config struct { feeds []string - services map[string]shared.Service + services *services.List interactionsDir string content []string tagMatcher *regexp.Regexp @@ -47,7 +48,6 @@ func parseConfig(cfgPath string) (*config, error) { cfg := &config{ feeds: []string{}, - services: make(map[string]shared.Service), syndicationMatchers: make(map[string]*regexp.Regexp), interactionsDir: cfgData.InteractionsDir, urlToPath: func(url string) string { @@ -72,11 +72,10 @@ func parseConfig(cfgPath string) (*config, error) { var serviceTags []string for name, siteConfig := range cfgData.Sites { - svc, err := shared.Load(name, siteConfig) + svc, err := cfg.services.Load(name, siteConfig) if err != nil { return nil, err } - cfg.services[name] = svc bf, err := svc.BackfeedMatcher() if err != nil { diff --git a/tools/syndicate/main.go b/tools/syndicate/main.go index 50e7dfed..bd97a5e6 100644 --- a/tools/syndicate/main.go +++ b/tools/syndicate/main.go @@ -36,7 +36,7 @@ func main() { if len(services) > 0 { fmt.Fprintf(os.Stderr, "Connecting to %s to syndicate & backfeed…\n", strings.Join(services, ", ")) for _, sname := range services { - if err := pstr.Connect(sname); err != nil { + if err := cfg.services.Init(sname); err != nil { check(fmt.Errorf("couldn't connect to %s: %w", sname, err)) } } diff --git a/tools/syndicate/poster/poster.go b/tools/syndicate/poster/poster.go index c8c9a2b9..84a58815 100644 --- a/tools/syndicate/poster/poster.go +++ b/tools/syndicate/poster/poster.go @@ -7,31 +7,28 @@ import ( "regexp" "strings" + "github.com/by-jp/www.byjp.me/tools/syndicate/services" "github.com/by-jp/www.byjp.me/tools/syndicate/shared" ) type poster struct { - services map[string]shared.Service + services *services.List done map[shared.SyndicationID]string } -func New(services map[string]shared.Service) *poster { +func New(services *services.List) *poster { return &poster{ services: services, done: make(map[shared.SyndicationID]string), } } -func (p *poster) Connect(sname string) error { - return p.services[sname].Connect(false) -} - func (p *poster) Post(sid shared.SyndicationID, post shared.Post) error { if _, ok := p.done[sid]; ok { return nil } - url, err := p.services[sid.Source].Post(post) + url, err := p.services.Service(sid.Source).Post(post) if err != nil { return err } diff --git a/tools/syndicate/services/tracker.go b/tools/syndicate/services/tracker.go new file mode 100644 index 00000000..26c4cff6 --- /dev/null +++ b/tools/syndicate/services/tracker.go @@ -0,0 +1,35 @@ +package services + +import ( + "fmt" + + "github.com/by-jp/www.byjp.me/tools/syndicate/shared" +) + +type List struct { + available map[string]shared.Service +} + +func New() *List { + return &List{} +} + +func (l *List) Load(name string, siteConfig any) (shared.Service, error) { + svc, err := shared.Load(name, siteConfig) + if err != nil { + return nil, fmt.Errorf("couldn't load service %s: %w", name, err) + } + l.available[name] = svc + return svc, nil +} + +func (l *List) Init(service string) error { + if svc, ok := l.available[service]; ok { + return svc.Connect(false) + } + return fmt.Errorf("service %s not available", service) +} + +func (l *List) Service(name string) shared.Service { + return l.available[name] +}