aboutsummaryrefslogtreecommitdiff
path: root/cgi/servers.go
blob: 229bca2782b84faaf5d7424f8b92a6ef4cd2d350 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
package cgi

import (
	"context"
	"encoding/json"
	"fmt"
	"io/ioutil"
	"net"
	"net/http"
	"net/http/cgi"
	"os"
	"sync"
	"time"

	"git.capotej.com/capotej/communique/config"
	"git.capotej.com/capotej/communique/models"
	"github.com/go-fed/activity/streams"
	"github.com/go-fed/activity/streams/vocab"
	"go.uber.org/zap"
)

type Servers struct {
	log       *zap.SugaredLogger
	persister *models.Persister
}

func NewServers(log *zap.SugaredLogger, persister *models.Persister) *Servers {
	return &Servers{log: log, persister: persister}
}

// 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) {
	var wg sync.WaitGroup

	for _, handler := range cfg.Handlers {
		wg.Add(2)
		go func(aHandler config.Handler) {
			defer wg.Done()
			startCGIServer(aHandler)
		}(handler)
		go func(aHandler config.Handler) {
			defer wg.Done()
			startTicker(aHandler, s.persister, s.log)
		}(handler)
	}
	wg.Wait()
}

func startCGIServer(h config.Handler) {
	cgiHandler := cgi.Handler{Path: h.Exec}

	server := http.Server{
		Handler: &cgiHandler,
	}

	sock := fmt.Sprintf("%s.sock", h.Name)
	os.Remove(sock)
	unixListener, err := net.Listen("unix", sock)
	if err != nil {
		panic(err)
	}
	server.Serve(unixListener)
}

func startTicker(h config.Handler, persister *models.Persister, log *zap.SugaredLogger) {
	ticker := time.NewTicker(h.Interval)
	done := make(chan bool)
	func() {
		for {
			select {
			case <-done:
				return
			case _ = <-ticker.C:
				output := tick(h)
				err := processTick(h, output, persister, log)
				if err != nil {
					log.Error(err)
				}
			}
		}
	}()
}

func processTick(h config.Handler, output []byte, persister *models.Persister, log *zap.SugaredLogger) error {
	var m map[string]interface{}
	err := json.Unmarshal(output, &m)
	if err != nil {
		return fmt.Errorf("could not unmarshal JSON: %w", err)
	}
	log.Debugf("processTick map is %+v", m)
	var coll vocab.ActivityStreamsOrderedCollection
	resolver, err := streams.NewJSONResolver(func(c context.Context, a vocab.ActivityStreamsOrderedCollection) error {
		// Example: store the article in the enclosing scope, for later.
		coll = a
		// We could pass an error back up, if desired.
		return nil
	})
	ctx := context.Background()
	err = resolver.Resolve(ctx, m)
	if err != nil {
		return fmt.Errorf("could not resolve JSON: %w", err)
	}
	orderedProp := coll.GetActivityStreamsOrderedItems()
	for iter := orderedProp.Begin(); iter != orderedProp.End(); iter = iter.Next() {
		contentType := iter.GetType().GetTypeName()
		log.Debugf("found object type %s in activity stream", contentType)

		contentNote := iter.GetActivityStreamsNote() // TODO make this configurable since it cant be dynamic

		localNote := models.NewActivityStreamsObject(contentNote, h)
		err = persister.Store(localNote)
		if err != nil {
			return err
		}
	}
	return nil
}

func tick(h config.Handler) []byte {
	sock := fmt.Sprintf("%s.sock", h.Name)
	httpc := http.Client{
		Transport: &http.Transport{
			DialContext: func(_ context.Context, _, _ string) (net.Conn, error) {
				return net.Dial("unix", sock)
			},
		},
	}
	var response *http.Response
	var err error
	response, err = httpc.Get("http://unix/" + sock)
	if err != nil {
		panic(err)
	}

	body, err := ioutil.ReadAll(response.Body)
	response.Body.Close()
	return body
}