-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
237 lines (205 loc) · 4.87 KB
/
main.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
package main
import (
"flag"
"fmt"
"html/template"
"io/ioutil"
"log"
"math/rand"
"net"
"net/http"
"os"
"os/exec"
"os/signal"
"path/filepath"
"strings"
"sync"
"syscall"
"time"
"github.com/gorilla/websocket"
"github.com/radovskyb/watcher"
)
var (
rpTemplate = template.Must(template.New("rpTemplate").Parse(rpTemplateSrc))
upgrader = websocket.Upgrader{}
wt *watcher.Watcher
serverFile, socketAddr, serverAddr, address, dir, interval string
cmd *exec.Cmd
)
func main() {
flag.StringVar(&serverFile, "file", "main.go", "the location of the Go server file")
flag.StringVar(&dir, "dir", "", "the directory to watch for changes (default is the server's directory)")
flag.StringVar(&address, "addr", "http://localhost:9001",
"the address to run reloadproxy")
flag.StringVar(&serverAddr, "server", "http://localhost:9000",
"the address where the server is set to run")
flag.StringVar(&interval, "interval", "100ms", "the interval duration to check for changes")
flag.Parse()
_, err := os.Stat(serverFile)
if err != nil {
fmt.Println("Enter the location of the Go server file. (e.g. main.go)\n")
flag.PrintDefaults()
fmt.Println()
os.Exit(1)
}
if dir == "" {
dir = filepath.Dir(serverFile)
}
wt = watcher.New()
if err := wt.AddRecursive(dir); err != nil {
log.Fatalln(err)
}
wt.SetMaxEvents(1)
// Seed the random generator.
rand.Seed(time.Now().UnixNano())
// Generate a random string for the WebSocket url location to avoid conflicts
// with the proxied server paths.
alphaRunes := []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789")
b := make([]rune, 12)
for i := range b {
b[i] = alphaRunes[rand.Intn(len(alphaRunes))]
}
socketAddr = string(b)
listenAddr := strings.SplitAfter(address, "://")[1]
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
data := struct {
SocketAddr string
Address string
Path string
}{
SocketAddr: socketAddr,
Address: listenAddr,
Path: strings.TrimPrefix(r.URL.Path, "/"),
}
if err := rpTemplate.Execute(w, data); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
})
http.HandleFunc("/"+socketAddr+"/", ReloadProxyHandler)
// Start reloadproxy's server
wg := new(sync.WaitGroup)
wg.Add(1)
go func() {
defer wg.Done()
log.Fatal(http.ListenAndServe(listenAddr, nil))
}()
// Start the regular server.
startServer()
c := make(chan os.Signal)
signal.Notify(c, os.Kill, os.Interrupt)
go func() {
<-c
killServer()
wt.Close()
os.Exit(1)
}()
// Start reloadproxy's watcher.
dur, err := time.ParseDuration(interval)
if err != nil {
log.Fatalln(err)
}
go func() {
err := wt.Start(dur)
if err != nil {
log.Fatalln(err)
}
}()
<-wt.Closed
wg.Wait()
}
func ReloadProxyHandler(w http.ResponseWriter, r *http.Request) {
conn, err := upgrader.Upgrade(w, r, nil)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
var c = make(chan struct{})
go func(conn *websocket.Conn) {
for {
mType, _, err := conn.ReadMessage()
if err != nil || mType == websocket.CloseMessage {
c <- struct{}{}
conn.Close()
return
}
}
}(conn)
go func(conn *websocket.Conn) {
path := strings.TrimPrefix(r.URL.Path, "/"+socketAddr)
// Write the page the first time it's visited.
err := conn.WriteMessage(websocket.TextMessage, getPage(serverAddr+path))
if err != nil {
conn.Close()
return
}
for {
select {
case <-wt.Event:
fmt.Println("reloading")
restartServer()
err := conn.WriteMessage(websocket.TextMessage, getPage(serverAddr+path))
if err != nil {
conn.Close()
return
}
case <-c:
return
}
}
}(conn)
}
func getPage(path string) []byte {
GET:
res, err := http.Get(path)
if err != nil {
if _, ok := err.(*net.AddrError); ok {
return []byte(err.Error())
}
// If it's not an address error, sleep for a bit then try again.
time.Sleep(500 * time.Millisecond)
goto GET
}
slurp, err := ioutil.ReadAll(res.Body)
res.Body.Close()
if err != nil {
return []byte(err.Error())
}
return slurp
}
func startServer() {
cmd = exec.Command("go", "run", serverFile)
cmd.SysProcAttr = &syscall.SysProcAttr{Setsid: true}
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
if err := cmd.Start(); err != nil {
log.Fatalln(err)
}
}
func killServer() {
if err := syscall.Kill(-cmd.Process.Pid, syscall.SIGKILL); err != nil {
log.Fatalln(err)
}
}
func restartServer() {
killServer()
startServer()
}
const rpTemplateSrc = `<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Reload Proxy</title>
<script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
<script>
$(document).ready(function() {
var ws = new WebSocket("ws://{{.Address}}/{{.SocketAddr}}/{{.Path}}");
ws.onmessage = function(e) {
$("body").html(e.data);
};
});
</script>
</head>
<body>
</body>
</html>`