-
Notifications
You must be signed in to change notification settings - Fork 0
/
apps.go
296 lines (238 loc) · 4.56 KB
/
apps.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
package main
import (
"errors"
"fmt"
"net"
"net/http"
"os"
"path"
"time"
"github.com/jweslley/localtunnel"
"github.com/jweslley/procker"
)
type App interface {
Name() string
Port() int
Start() error
Stop() error
Running() bool
}
var (
errAlreadyStarted = errors.New("Already started")
errNotStarted = errors.New("Not started")
errAlreadyShared = errors.New("Already shared")
)
type app struct {
name string
port int
}
func (a *app) Name() string {
return a.name
}
func (a *app) Port() int {
return a.port
}
func (a *app) String() string {
return fmt.Sprintf("%s:%d", a.name, a.port)
}
type processApp struct {
app
dir string
env []string
processes map[string]string
process procker.Process
}
func (a *processApp) Start() error {
if a.Running() {
return errAlreadyStarted
}
p, err := a.buildProcess()
if err != nil {
return err
}
a.process = p
return a.process.Start()
}
func (a *processApp) Stop() error {
if !a.Running() {
return errNotStarted
}
err := a.process.Stop(3 * time.Second) // FIXME magic number
a.process = nil
return err
}
func (a *processApp) Running() bool {
return a.process != nil && a.process.Running()
}
func (a *processApp) buildProcess() (procker.Process, error) {
port, err := FreePort()
if err != nil {
return nil, err
}
a.port = port
p := []procker.Process{}
for name, command := range a.processes {
prefix := fmt.Sprintf("[%s:%s] ", a.Name(), name)
process := &procker.SysProcess{
Command: command,
Dir: a.dir,
Env: append(a.env, fmt.Sprintf("PORT=%d", port)),
Stdout: procker.NewPrefixedWriter(os.Stdout, prefix),
Stderr: procker.NewPrefixedWriter(os.Stderr, prefix),
}
p = append(p, process)
}
return procker.NewProcessGroup(p...), nil
}
func NewProcessApp(procfile string) (App, error) {
processes, err := parseProfile(procfile)
if err != nil {
return nil, err
}
dir := path.Dir(procfile)
name := path.Base(dir)
envFile := path.Join(dir, ".env")
env, err := parseEnv(envFile)
if err != nil {
env = []string{}
}
a := &processApp{dir: dir, env: env, processes: processes}
a.name = name
return a, nil
}
func parseProfile(filepath string) (map[string]string, error) {
file, err := os.Open(filepath)
if err != nil {
return nil, err
}
defer file.Close()
processes, err := procker.ParseProcfile(file)
if err != nil {
return nil, err
}
return processes, nil
}
func parseEnv(filepath string) ([]string, error) {
file, err := os.Open(filepath)
if err != nil {
return nil, err
}
defer file.Close()
env, err := procker.ParseEnv(file)
if err != nil {
return nil, err
}
return env, nil
}
type aliasApp struct {
app
running bool
}
func (a *aliasApp) Start() error {
a.running = true
return nil
}
func (a *aliasApp) Stop() error {
a.running = false
return nil
}
func (a *aliasApp) Running() bool {
return a.running
}
func NewAliasApp(name string, port int) App {
a := &aliasApp{}
a.name = name
a.port = port
a.running = true
return a
}
type webApp struct {
app
handler http.Handler
listener net.Listener
}
func (a *webApp) Start() error {
if a.Running() {
return errAlreadyStarted
}
l, err := NewLocalListener()
if err != nil {
return err
}
port, err := AddrPort(l.Addr().String())
if err != nil {
return err
}
a.listener = l
a.port = port
s := &http.Server{Handler: a.handler}
go func() {
s.Serve(a.listener)
a.listener = nil
}()
return nil
}
func (a *webApp) Stop() error {
if !a.Running() {
return errNotStarted
}
err := a.listener.Close()
a.listener = nil
return err
}
func (a *webApp) Running() bool {
return a.listener != nil
}
func NewWebServerApp(dir string) App {
a := &webApp{}
a.name = path.Base(dir)
a.handler = http.StripPrefix("/", http.FileServer(http.Dir(dir)))
return a
}
type ShareableApp struct {
App
tunnel *localtunnel.Tunnel
}
func (a *ShareableApp) Stop() error {
if a.Shared() {
go func() { a.Unshare() }()
}
return a.App.Stop()
}
func (a *ShareableApp) Share() error {
if !a.Running() {
return errNotStarted
}
if a.Shared() {
return errAlreadyShared
}
tunnel := localtunnel.DefaultClient.NewLocalTunnel(a.Port())
err := tunnel.Open()
if err != nil {
return err
}
a.tunnel = tunnel
go func() {
<-tunnel.Closing()
a.tunnel = nil
}()
return nil
}
func (a *ShareableApp) Unshare() error {
if !a.Running() {
return errNotStarted
}
if a.Shared() {
a.tunnel.Close()
}
return nil
}
func (a *ShareableApp) Shared() bool {
return a.tunnel != nil
}
func (a *ShareableApp) URL() string {
if a.tunnel == nil {
return ""
}
return a.tunnel.URL()
}