Centralised service management

This commit is contained in:
JP Hastings-Spital 2023-11-12 09:47:58 +00:00
parent 16f1435550
commit 00283bec87
5 changed files with 48 additions and 15 deletions

View file

@ -1,13 +1,15 @@
package backfeeder 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 { type backfeeder struct {
services map[string]shared.Service services *services.List
done map[string]struct{} done map[string]struct{}
} }
func New(services map[string]shared.Service) *backfeeder { func New(services *services.List) *backfeeder {
return &backfeeder{ return &backfeeder{
services: services, services: services,
done: make(map[string]struct{}), done: make(map[string]struct{}),

View file

@ -8,6 +8,7 @@ import (
"strings" "strings"
"github.com/BurntSushi/toml" "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/by-jp/www.byjp.me/tools/syndicate/shared"
"github.com/bmatcuk/doublestar/v4" "github.com/bmatcuk/doublestar/v4"
@ -30,7 +31,7 @@ type fileConfig struct {
type config struct { type config struct {
feeds []string feeds []string
services map[string]shared.Service services *services.List
interactionsDir string interactionsDir string
content []string content []string
tagMatcher *regexp.Regexp tagMatcher *regexp.Regexp
@ -47,7 +48,6 @@ func parseConfig(cfgPath string) (*config, error) {
cfg := &config{ cfg := &config{
feeds: []string{}, feeds: []string{},
services: make(map[string]shared.Service),
syndicationMatchers: make(map[string]*regexp.Regexp), syndicationMatchers: make(map[string]*regexp.Regexp),
interactionsDir: cfgData.InteractionsDir, interactionsDir: cfgData.InteractionsDir,
urlToPath: func(url string) string { urlToPath: func(url string) string {
@ -72,11 +72,10 @@ func parseConfig(cfgPath string) (*config, error) {
var serviceTags []string var serviceTags []string
for name, siteConfig := range cfgData.Sites { for name, siteConfig := range cfgData.Sites {
svc, err := shared.Load(name, siteConfig) svc, err := cfg.services.Load(name, siteConfig)
if err != nil { if err != nil {
return nil, err return nil, err
} }
cfg.services[name] = svc
bf, err := svc.BackfeedMatcher() bf, err := svc.BackfeedMatcher()
if err != nil { if err != nil {

View file

@ -36,7 +36,7 @@ func main() {
if len(services) > 0 { if len(services) > 0 {
fmt.Fprintf(os.Stderr, "Connecting to %s to syndicate & backfeed…\n", strings.Join(services, ", ")) fmt.Fprintf(os.Stderr, "Connecting to %s to syndicate & backfeed…\n", strings.Join(services, ", "))
for _, sname := range 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)) check(fmt.Errorf("couldn't connect to %s: %w", sname, err))
} }
} }

View file

@ -7,31 +7,28 @@ import (
"regexp" "regexp"
"strings" "strings"
"github.com/by-jp/www.byjp.me/tools/syndicate/services"
"github.com/by-jp/www.byjp.me/tools/syndicate/shared" "github.com/by-jp/www.byjp.me/tools/syndicate/shared"
) )
type poster struct { type poster struct {
services map[string]shared.Service services *services.List
done map[shared.SyndicationID]string done map[shared.SyndicationID]string
} }
func New(services map[string]shared.Service) *poster { func New(services *services.List) *poster {
return &poster{ return &poster{
services: services, services: services,
done: make(map[shared.SyndicationID]string), 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 { func (p *poster) Post(sid shared.SyndicationID, post shared.Post) error {
if _, ok := p.done[sid]; ok { if _, ok := p.done[sid]; ok {
return nil return nil
} }
url, err := p.services[sid.Source].Post(post) url, err := p.services.Service(sid.Source).Post(post)
if err != nil { if err != nil {
return err return err
} }

View file

@ -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]
}