diff options
author | Julio Capote <jcapote@gmail.com> | 2023-01-04 02:36:22 +0000 |
---|---|---|
committer | Julio Capote <jcapote@gmail.com> | 2023-01-04 02:36:22 +0000 |
commit | b7b4d0affe3e76c2a4eaac1aff9759d83625c6c6 (patch) | |
tree | 78858a9054ab83978275cf9a1a8550d4fb0b6adb | |
parent | b74190b22474986c20149b3a4a527a684f4ee3ce (diff) | |
download | communique-b7b4d0affe3e76c2a4eaac1aff9759d83625c6c6.tar.gz |
content or description, add logging to cgi handlers
-rw-r--r-- | cgi/servers.go | 33 |
1 files changed, 22 insertions, 11 deletions
diff --git a/cgi/servers.go b/cgi/servers.go index a3c7e47..06cd72f 100644 --- a/cgi/servers.go +++ b/cgi/servers.go @@ -30,25 +30,27 @@ func NewServers(log *zap.SugaredLogger, persister *models.Persister) *Servers { // along with ticker for the configured handler interval then blocks indefinitely func (s *Servers) Start(cfg config.Config) { var wg sync.WaitGroup + logger := s.log.With("type", "cgi") for _, handler := range cfg.Handlers { + handlerLogger := logger.With("handler", handler.Name) wg.Add(3) // Internal CGI server go func(aHandler config.Handler) { defer wg.Done() - startCGIServer(aHandler) + startCGIServer(aHandler, handlerLogger) }(handler) // Ticker go func(aHandler config.Handler) { defer wg.Done() - startTicker(aHandler, s.persister, s.log) + startTicker(aHandler, s.persister, handlerLogger) }(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) - err := processTick(aHandler, output, s.persister, s.log) + output := tick(aHandler, handlerLogger) + err := processTick(aHandler, output, s.persister, handlerLogger) if err != nil { s.log.Error(err) } @@ -58,7 +60,7 @@ func (s *Servers) Start(cfg config.Config) { wg.Wait() } -func startCGIServer(h config.Handler) { +func startCGIServer(h config.Handler, log *zap.SugaredLogger) { cgiHandler := cgi.Handler{Path: h.Exec} server := http.Server{ @@ -66,6 +68,7 @@ func startCGIServer(h config.Handler) { } sock := fmt.Sprintf("%s.sock", h.Name) + log.Debugf("starting cgi server at %s", sock) os.Remove(sock) unixListener, err := net.Listen("unix", sock) if err != nil { @@ -83,7 +86,7 @@ func startTicker(h config.Handler, persister *models.Persister, log *zap.Sugared case <-done: return case _ = <-ticker.C: - output := tick(h) + output := tick(h, log) err := processTick(h, output, persister, log) if err != nil { log.Error(err) @@ -102,9 +105,16 @@ func processTick(h config.Handler, output []byte, persister *models.Persister, l } for _, v := range feed.Items { - if len(v.Description) != 0 { - log.Debugf("found description '%s'", v.Description) - outboxItem := models.CreateOutboxItem(h, []byte(v.Description)) + var extractedContent string + // if there is no content, use descrption + if len(v.Content) != 0 { + extractedContent = v.Content + } else if len(v.Description) != 0 { + extractedContent = v.Description + } + if len(extractedContent) != 0 { + log.Debugf("extracted content '%s'", extractedContent) + outboxItem := models.CreateOutboxItem(h, []byte(extractedContent)) err = persister.Store(outboxItem) if err != nil { return err @@ -114,7 +124,7 @@ func processTick(h config.Handler, output []byte, persister *models.Persister, l return nil } -func tick(h config.Handler) []byte { +func tick(h config.Handler, log *zap.SugaredLogger) []byte { sock := fmt.Sprintf("%s.sock", h.Name) httpc := http.Client{ Transport: &http.Transport{ @@ -125,9 +135,10 @@ func tick(h config.Handler) []byte { } var response *http.Response var err error + log.Debugf("executing cgi handler at %s", sock) response, err = httpc.Get("http://unix/" + sock) if err != nil { - panic(err) + log.Errorf("received error from cgi handler %s", err) } body, err := ioutil.ReadAll(response.Body) |