-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwatch.go
More file actions
271 lines (235 loc) · 6.01 KB
/
Copy pathwatch.go
File metadata and controls
271 lines (235 loc) · 6.01 KB
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
// watch.go owns the LOCAL preview path used by `gander <file> --watch`:
// a localhost HTTP server pushes hot-swaps to the user's own browser.
//
// The REMOTE live-share used by the `gander watch <file>` top-level
// subcommand lives in share.go as runWatchCmd and is a thin wrapper
// around `share --watch` — different scope, different code path. Keep
// these two deliberately separate.
package main
import (
"context"
"crypto/sha256"
"encoding/hex"
"encoding/json"
"fmt"
"log"
"net"
"net/http"
"os"
"os/signal"
"sync"
"syscall"
"time"
"github.com/fsnotify/fsnotify"
)
type watchState struct {
absPath string
htmlBytes []byte
contentHTML string
headings []Heading
mu sync.RWMutex
subs map[chan string]struct{}
subMu sync.Mutex
lastHash string
}
func newWatchState(absPath string, initialHTML []byte, initialContent string, initialHeadings []Heading, lastHash string) *watchState {
return &watchState{
absPath: absPath,
htmlBytes: initialHTML,
contentHTML: initialContent,
headings: initialHeadings,
subs: make(map[chan string]struct{}),
lastHash: lastHash,
}
}
func (s *watchState) snapshot() (html []byte, content string) {
s.mu.RLock()
defer s.mu.RUnlock()
out := make([]byte, len(s.htmlBytes))
copy(out, s.htmlBytes)
return out, s.contentHTML
}
func (s *watchState) update(contentHTML string, html []byte, headings []Heading, hash string) {
s.mu.Lock()
s.htmlBytes = html
s.contentHTML = contentHTML
s.headings = headings
s.lastHash = hash
s.mu.Unlock()
s.subMu.Lock()
for ch := range s.subs {
select {
case ch <- contentHTML:
default:
}
}
s.subMu.Unlock()
}
func (s *watchState) subscribe() chan string {
ch := make(chan string, 1)
s.subMu.Lock()
s.subs[ch] = struct{}{}
s.subMu.Unlock()
return ch
}
func (s *watchState) unsubscribe(ch chan string) {
s.subMu.Lock()
if _, ok := s.subs[ch]; ok {
delete(s.subs, ch)
close(ch)
}
s.subMu.Unlock()
}
func (s *watchState) handleIndex(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/" {
http.NotFound(w, r)
return
}
html, _ := s.snapshot()
w.Header().Set("Content-Type", "text/html; charset=utf-8")
w.Header().Set("Cache-Control", "no-cache")
_, _ = w.Write(html)
}
func (s *watchState) handleEvents(w http.ResponseWriter, r *http.Request) {
flusher, ok := w.(http.Flusher)
if !ok {
http.Error(w, "streaming unsupported", http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "text/event-stream")
w.Header().Set("Cache-Control", "no-cache")
w.Header().Set("Connection", "keep-alive")
w.Header().Set("X-Accel-Buffering", "no")
_, content := s.snapshot()
writeSSE(w, flusher, "content", content)
ch := s.subscribe()
defer s.unsubscribe(ch)
ctx := r.Context()
for {
select {
case <-ctx.Done():
return
case data, open := <-ch:
if !open {
return
}
writeSSE(w, flusher, "content", data)
}
}
}
func writeSSE(w http.ResponseWriter, flusher http.Flusher, event, data string) {
b, err := json.Marshal(data)
if err != nil {
b = []byte(`""`)
}
fmt.Fprintf(w, "event: %s\ndata: %s\n\n", event, b)
flusher.Flush()
}
func runWatch(absPath string, cfg Config) error {
content, err := os.ReadFile(absPath)
if err != nil {
return fmt.Errorf("read %s: %w", absPath, err)
}
contentHTML, headings := renderMarkdownWithIDs(string(content))
html := []byte(buildHTML(contentHTML, headings, true))
hash := hashBytes(content)
state := newWatchState(absPath, html, contentHTML, headings, hash)
addr := fmt.Sprintf("127.0.0.1:%d", cfg.Port)
ln, err := net.Listen("tcp", addr)
if err != nil {
return fmt.Errorf("listen %s: %w", addr, err)
}
mux := http.NewServeMux()
mux.HandleFunc("/", state.handleIndex)
mux.HandleFunc("/events", state.handleEvents)
srv := &http.Server{
Handler: mux,
ReadHeaderTimeout: 5 * time.Second,
}
url := fmt.Sprintf("http://%s", ln.Addr().String())
fmt.Printf("Preview at: %s\n", url)
fmt.Println("Watching for changes. Press Ctrl+C to stop.")
openBrowser(url)
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
defer stop()
go func() {
if err := srv.Serve(ln); err != nil && err != http.ErrServerClosed {
log.Printf("server error: %v", err)
stop()
}
}()
go watchLoop(ctx, state, absPath, cfg.DebounceMs)
<-ctx.Done()
fmt.Println("\nStopping...")
shutdownCtx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
if err := srv.Shutdown(shutdownCtx); err != nil {
return fmt.Errorf("shutdown: %w", err)
}
return nil
}
func watchLoop(ctx context.Context, s *watchState, absPath string, debounceMs int) {
debounce := time.Duration(debounceMs) * time.Millisecond
if debounce < 50*time.Millisecond {
debounce = 50 * time.Millisecond
}
watcher, err := fsnotify.NewWatcher()
if err != nil {
log.Printf("watcher init: %v", err)
return
}
defer watcher.Close()
if err := watcher.Add(absPath); err != nil {
log.Printf("watch %s: %v", absPath, err)
return
}
var pending *time.Timer
for {
select {
case <-ctx.Done():
return
case ev, ok := <-watcher.Events:
if !ok {
return
}
if ev.Op&(fsnotify.Write|fsnotify.Create) == 0 {
continue
}
if pending != nil {
pending.Stop()
}
pending = time.AfterFunc(debounce, func() {
if err := reloadFile(s, absPath); err != nil {
log.Printf("reload: %v", err)
}
})
case err, ok := <-watcher.Errors:
if !ok {
return
}
log.Printf("watcher error: %v", err)
}
}
}
func reloadFile(s *watchState, absPath string) error {
data, err := os.ReadFile(absPath)
if err != nil {
return fmt.Errorf("read: %w", err)
}
h := hashBytes(data)
s.mu.RLock()
if h == s.lastHash {
s.mu.RUnlock()
return nil
}
s.mu.RUnlock()
contentHTML, headings := renderMarkdownWithIDs(string(data))
html := []byte(buildHTML(contentHTML, headings, true))
s.update(contentHTML, html, headings, h)
fmt.Println("Reloaded.")
return nil
}
func hashBytes(b []byte) string {
sum := sha256.Sum256(b)
return hex.EncodeToString(sum[:])
}