forked from dokku/plugn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgateway.go
135 lines (128 loc) · 2.79 KB
/
gateway.go
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
package main
import (
"io"
"io/ioutil"
"log"
"os"
"os/signal"
"strings"
"sync"
"github.com/dokku/duplex/poc2/duplex"
"github.com/progrium/plugin-demo/demo/coproc"
)
func runGateway() {
log.Println("starting gateway...")
wg := new(sync.WaitGroup)
host := coproc.StartHost(findPlugins())
wg.Add(1)
go func() {
host.Wait()
wg.Done()
}()
gateway := startGateway(wg, host)
go func() {
handler := make(chan os.Signal, 1)
signal.Notify(handler, os.Interrupt)
first := true
for sig := range handler {
switch sig {
case os.Interrupt:
log.Println("ctrl-c detected")
gateway.Shutdown()
host.Shutdown(!first)
first = false
}
}
}()
wg.Wait()
}
func findPlugins() []string {
var plugins []string
enabled, err := ioutil.ReadDir(PluginPath + "/enabled")
if err != nil {
log.Println(err)
return []string{}
}
for _, file := range enabled {
filepath := PluginPath + "/available/" + file.Name() + "/" + file.Name()
if _, err := os.Stat(filepath); err == nil {
plugins = append(plugins, filepath)
}
}
return plugins
}
func startGateway(wg *sync.WaitGroup, plugins *coproc.Host) *duplex.Peer {
gateway := duplex.NewPeer()
gateway.SetOption(duplex.OptName, "plugn:gateway")
err := gateway.Bind("unix://" + PluginPath + "/gateway.sock")
if err != nil {
panic(err)
}
wg.Add(1)
go func() {
for {
meta, ch := gateway.Accept()
if meta == nil {
break
}
if meta.Service() == "reload" {
log.Println("reload received...")
plugins.RestartWith(findPlugins())
ch.Close()
continue
}
go func() {
for _, peer := range gateway.Peers() {
if !strings.HasPrefix(peer, "plugn") {
hook, err := gateway.Open(peer, meta.Service(), meta.Headers())
if err != nil {
log.Println("unable to trigger", peer)
continue
}
io.Copy(ch, hook)
hook.Close()
}
}
ch.Close()
}()
}
wg.Done()
}()
return gateway
}
func TriggerGateway(args []string) {
gatewaySock := PluginPath + "/gateway.sock"
if _, err := os.Stat(gatewaySock); os.IsNotExist(err) {
return
}
trigger := duplex.NewPeer()
defer trigger.Shutdown()
trigger.SetOption(duplex.OptName, "plugn:trigger")
err := trigger.Connect("unix://" + gatewaySock)
if err != nil {
panic(err)
}
ch, err := trigger.Open("plugn:gateway", args[0], args[1:])
if err != nil {
panic(err)
}
io.Copy(os.Stdout, ch)
ch.Close()
}
func ReloadGateway(args []string) {
gatewaySock := PluginPath + "/gateway.sock"
if _, err := os.Stat(gatewaySock); os.IsNotExist(err) {
return
}
trigger := duplex.NewPeer()
defer trigger.Shutdown()
trigger.SetOption(duplex.OptName, "plugn:reload")
err := trigger.Connect("unix://" + gatewaySock)
if err != nil {
panic(err)
}
_, err = trigger.Open("plugn:gateway", "reload", nil)
if err != nil {
panic(err)
}
}