diff options
-rw-r--r-- | cgi/servers.go | 30 | ||||
-rw-r--r-- | main.go | 4 |
2 files changed, 22 insertions, 12 deletions
diff --git a/cgi/servers.go b/cgi/servers.go index a419417..0a87a62 100644 --- a/cgi/servers.go +++ b/cgi/servers.go @@ -8,11 +8,13 @@ import ( "net/http" "net/http/cgi" "os" + "strings" "sync" "time" "git.capotej.com/capotej/communique/config" "git.capotej.com/capotej/communique/models" + "git.capotej.com/capotej/communique/views" "github.com/mmcdole/gofeed" "go.uber.org/zap" ) @@ -20,19 +22,20 @@ import ( type Servers struct { log *zap.SugaredLogger persister *models.Persister + cfg config.Config } -func NewServers(log *zap.SugaredLogger, persister *models.Persister) *Servers { - return &Servers{log: log, persister: persister} +func NewServers(log *zap.SugaredLogger, persister *models.Persister, cfg config.Config) *Servers { + return &Servers{log: log, persister: persister, cfg: cfg} } // Start iterates over all Handlers and starts an internal CGI server for each one // along with ticker for the configured handler interval then blocks indefinitely -func (s *Servers) Start(cfg config.Config) { +func (s *Servers) Start() { var wg sync.WaitGroup logger := s.log.With("type", "cgi") - for _, handler := range cfg.Handlers { + for _, handler := range s.cfg.Handlers { handlerLogger := logger.With("handler", handler.Name) wg.Add(3) // Internal CGI server @@ -43,14 +46,14 @@ func (s *Servers) Start(cfg config.Config) { // Ticker go func(aHandler config.Handler) { defer wg.Done() - startTicker(aHandler, s.persister, handlerLogger) + startTicker(aHandler, s.persister, handlerLogger, s.cfg) }(handler) // Execute a handler tick on start since Go's ticker waits until $interval to trigger first tick go func(aHandler config.Handler) { defer wg.Done() time.Sleep(1 * time.Second) output := tick(aHandler, handlerLogger) - err := processTick(aHandler, output, s.persister, handlerLogger) + err := processTick(aHandler, output, s.persister, handlerLogger, s.cfg) if err != nil { s.log.Error(err) } @@ -77,7 +80,7 @@ func startCGIServer(h config.Handler, log *zap.SugaredLogger) { server.Serve(unixListener) } -func startTicker(h config.Handler, persister *models.Persister, log *zap.SugaredLogger) { +func startTicker(h config.Handler, persister *models.Persister, log *zap.SugaredLogger, cfg config.Config) { ticker := time.NewTicker(h.Interval) // TODO add some random jitter here so handlers dont run at the same exact intervals done := make(chan bool) func() { @@ -87,7 +90,7 @@ func startTicker(h config.Handler, persister *models.Persister, log *zap.Sugared return case _ = <-ticker.C: output := tick(h, log) - err := processTick(h, output, persister, log) + err := processTick(h, output, persister, log, cfg) if err != nil { log.Error(err) } @@ -96,7 +99,7 @@ func startTicker(h config.Handler, persister *models.Persister, log *zap.Sugared }() } -func processTick(h config.Handler, output []byte, persister *models.Persister, log *zap.SugaredLogger) error { +func processTick(h config.Handler, output []byte, persister *models.Persister, log *zap.SugaredLogger, cfg config.Config) error { fp := gofeed.NewParser() fp.ParseString(string(output)) feed, err := fp.ParseString(string(output)) @@ -125,8 +128,15 @@ func processTick(h config.Handler, output []byte, persister *models.Persister, l logger.Error(err) return } + payload, err := views.RenderActivity(h.Name, cfg.Domain, *outboxItem) + if err != nil { + logger.Error(err) + return + } for _, v := range keys { - logger.Debugf("found subscription %s", string(v)) + parts := strings.Split(string(v), ":") + url := parts[2:] + logger.With("payload", payload).With("inboxUrl", url).Debugf("deliverying activity") } }) if err != nil { @@ -61,9 +61,9 @@ func main() { var mainWg sync.WaitGroup // // Internal CGI Servers - cgiServers := cgi.NewServers(log, persister) + cgiServers := cgi.NewServers(log, persister, cfg) mainWg.Add(1) - go cgiServers.Start(cfg) + go cgiServers.Start() // // External Http Server writer := &zapio.Writer{Log: logger, Level: zap.DebugLevel} |