-
Notifications
You must be signed in to change notification settings - Fork 1
/
afr.go
206 lines (165 loc) · 4.61 KB
/
afr.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
/*
Tiny library for integrating "live reload" into your HTTP server.
Dependency-free. See `readme.md` for documentation.
Installation:
go install github.com/mitranim/afr/afr@latest
*/
package afr
import (
_ "embed"
"encoding/json"
"fmt"
"io"
"net/http"
"strings"
"sync"
)
//go:embed client.mjs
var client string
/*
Source code of `client.mjs`, the client part of Afr. Exported "just in case".
The script is served by `Broad.ServeHTTP` on `/afr/client.mjs`. To use the
client, include the following into your HTML, changing the port as needed:
<script type="module" src="http://localhost:23456/afr/client.mjs"></script>
*/
var ClientScript = scriptExec(client)
/*
Short for "broadcaster". Serves various Afr endpoints. See `readme.md` for
documentation. Functionality is equivalent between Go and JS.
*/
type Broad struct {
lock sync.Mutex
chans map[int]chan chunk
count int
}
/*
Implements `http.Handler`. Serves various Afr endpoints. See `readme.md` for
documentation. Functionality is equivalent between Go and JS.
*/
func (self *Broad) ServeHTTP(rew http.ResponseWriter, req *http.Request) {
method, path := req.Method, req.URL.Path
if method == http.MethodGet && path == `/afr/client.mjs` {
self.getClient(rew, req)
return
}
if method == http.MethodGet && path == `/afr/event` {
self.getEvent(rew, req)
return
}
if method == http.MethodGet && path == `/afr/events` {
self.getEvents(rew, req)
return
}
if method == http.MethodPost && path == `/afr/send` {
self.postSend(rew, req)
return
}
rew.WriteHeader(http.StatusNotFound)
fmt.Fprintf(rew, `no endpoint for %v %q`, method, path)
}
/*
Broadcasts the data to all connected clients. For clients waiting on
"/afr/event", this is sent as the complete response body, closing the
connection. For clients waiting on "/afr/events", this is sent as one message
in the data stream, keeping the connection open.
*/
func (self *Broad) Send(val []byte) {
self.lock.Lock()
defer self.lock.Unlock()
for _, ch := range self.initChans() {
ch <- val
}
}
// Shortcut for broadcasting a JSON-encoded msg via `Broad.Send`.
func (self *Broad) SendMsg(msg Msg) {
self.Send(tryByteSlice(json.Marshal(msg)))
}
func (self *Broad) getClient(rew http.ResponseWriter, _ *http.Request) {
rew.Header().Set(`Content-Type`, `application/javascript`)
tryInt(io.WriteString(rew, ClientScript))
}
func (self *Broad) getEvent(rew http.ResponseWriter, req *http.Request) {
ctx := req.Context()
key, events := self.tee()
defer self.del(key)
select {
case <-ctx.Done():
return
case event := <-events:
tryInt(rew.Write(event))
}
}
func (self *Broad) getEvents(rew http.ResponseWriter, req *http.Request) {
ctx := req.Context()
key, events := self.tee()
defer self.del(key)
eventStreamInit(rew, req)
for {
select {
case <-ctx.Done():
return
case event := <-events:
eventStreamWrite(rew, event)
}
}
}
func (self *Broad) postSend(rew http.ResponseWriter, req *http.Request) {
self.Send(tryByteSlice(io.ReadAll(req.Body)))
}
func (self *Broad) tee() (int, chan chunk) {
val := make(chan chunk, 1)
self.lock.Lock()
defer self.lock.Unlock()
self.count++
key := self.count
self.initChans()[key] = val
return key, val
}
func (self *Broad) del(key int) {
self.lock.Lock()
defer self.lock.Unlock()
delete(self.initChans(), key)
}
// Must be called under mutex.
func (self *Broad) initChans() map[int]chan chunk {
if self.chans == nil {
self.chans = make(map[int]chan chunk)
}
return self.chans
}
/*
Message type understood by `client.mjs`. You should use a file-watching library
to watch files, convert file events into Afr msgs, and send them via `Broad`.
To simply reload the page, use the following:
bro.SendMsg(afr.Msg{Type: `change`})
To reinject CSS, use something like:
bro.SendMsg(afr.Msg{Type: `change`, Path: `/styles/main.css`})
*/
type Msg struct {
Type string `json:"type,omitempty"`
Path string `json:"path,omitempty"`
}
type chunk []byte
func eventStreamInit(rew http.ResponseWriter, req *http.Request) {
rew.Header().Set(`Content-Type`, `text/event-stream`)
flush(rew)
}
func eventStreamWrite(rew http.ResponseWriter, val []byte) {
tryInt(io.WriteString(rew, `data: `))
tryInt(rew.Write(val))
tryInt(io.WriteString(rew, "\n\n"))
flush(rew)
}
func scriptExec(val string) string {
val = strings.TrimSpace(val)
val = strings.TrimPrefix(val, `export `)
return `void ` + val + `()`
}
func flush(rew http.ResponseWriter) { rew.(http.Flusher).Flush() }
func tryByteSlice(val []byte, err error) []byte { try(err); return val }
func tryInt(val int, err error) int { try(err); return val }
func try(err error) {
if err != nil {
panic(err)
}
}