-
-
Notifications
You must be signed in to change notification settings - Fork 180
/
run.go
119 lines (109 loc) · 2.83 KB
/
run.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
package run
import (
"context"
"net"
"path/filepath"
"github.com/livebud/bud/package/exe"
"github.com/livebud/bud/package/log/console"
"github.com/livebud/bud/package/watcher"
"github.com/livebud/bud/package/hot"
"golang.org/x/sync/errgroup"
"github.com/livebud/bud/package/socket"
"github.com/livebud/bud/runtime/bud"
)
type Command struct {
Flag *bud.Flag
Project *bud.Project
Port string
}
func (c *Command) Run(ctx context.Context) error {
eg, ctx := errgroup.WithContext(ctx)
// Initialize the hot server
var hotServer *hot.Server
if c.Flag.Hot {
hotServer = hot.New()
// Start the hot reload server
eg.Go(func() error { return c.startHot(ctx, hotServer) })
}
// Start the web server
eg.Go(func() error { return c.startApp(ctx, hotServer) })
return eg.Wait()
}
func (c *Command) compileAndStart(ctx context.Context, ln net.Listener) (*exe.Cmd, error) {
app, err := c.Project.Compile(ctx, c.Flag)
if err != nil {
return nil, err
}
process, err := app.Start(ctx, ln)
if err != nil {
return nil, err
}
return process, nil
}
func (c *Command) startApp(ctx context.Context, hotServer *hot.Server) error {
listener, err := socket.Load(c.Port)
if err != nil {
return err
}
// Compile and start the project
process, err := c.compileAndStart(ctx, listener)
if err != nil {
// TODO: de-duplicate with the watcher above
console.Error(err.Error())
if err := watcher.Watch(ctx, ".", func(path string) error {
process, err = c.compileAndStart(ctx, listener)
if err != nil {
console.Error(err.Error())
return nil
}
// TODO: host should be dynamic
console.Info("Ready on http://127.0.0.1" + c.Port)
return watcher.Stop
}); err != nil {
return err
}
}
defer process.Close()
// Start watching
if err := watcher.Watch(ctx, ".", func(path string) error {
switch filepath.Ext(path) {
// Re-compile the app and restart the Go server
case ".go":
// Trigger a reload if there's a hot reload server configured
if hotServer != nil {
// Exclamation point just means full page reload
hotServer.Reload("!")
}
if err := process.Close(); err != nil {
console.Error(err.Error())
return nil
}
app, err := c.Project.Compile(ctx, c.Flag)
if err != nil {
console.Error(err.Error())
return nil
}
process, err = app.Start(ctx, listener)
if err != nil {
console.Error(err.Error())
return nil
}
// TODO: host should be dynamic
console.Info("Ready on http://127.0.0.1" + c.Port)
return nil
// Hot reload the page
default:
// Trigger a reload if there's a hot reload server configured
if hotServer != nil {
hotServer.Reload("*")
}
return nil
}
}); err != nil {
return err
}
return process.Wait()
}
func (c *Command) startHot(ctx context.Context, hotServer *hot.Server) error {
return hotServer.ListenAndServe(ctx, ":35729")
}