diff options
author | Julio Capote <jcapote@gmail.com> | 2022-12-17 15:31:03 +0000 |
---|---|---|
committer | Julio Capote <jcapote@gmail.com> | 2022-12-17 15:31:03 +0000 |
commit | d3fb280a146759ca1468c1773f41599b4fabb5fa (patch) | |
tree | 931e475e12baf1aecd76d8fab80c10b4b3d3c83d /cgi/servers.go | |
download | communique-d3fb280a146759ca1468c1773f41599b4fabb5fa.tar.gz |
initial
Diffstat (limited to 'cgi/servers.go')
-rw-r--r-- | cgi/servers.go | 90 |
1 files changed, 90 insertions, 0 deletions
diff --git a/cgi/servers.go b/cgi/servers.go new file mode 100644 index 0000000..b18c6a3 --- /dev/null +++ b/cgi/servers.go @@ -0,0 +1,90 @@ +package cgi + +import ( + "context" + "fmt" + "io" + "net" + "net/http" + "net/http/cgi" + "os" + "sync" + "time" + + "git.capotej.com/capotej/communique/config" +) + +type Servers struct{} + +func NewServers() *Servers { + return &Servers{} +} + +// Start iterates over all Handlers and starts an internal CGI server for each one +// along with ticker for the configured handler interval and blocks forever +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) + }(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) { + // TODO add config for this + ticker := time.NewTicker(5 * time.Second) + done := make(chan bool) + func() { + for { + select { + case <-done: + return + case _ = <-ticker.C: + tick(h) + } + } + }() +} + +func tick(h config.Handler) { + 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) + } + io.Copy(os.Stdout, response.Body) +} |