mirror of
https://github.com/facundoolano/jorge.git
synced 2024-12-25 21:58:28 +01:00
more sample code snippets in serve post
This commit is contained in:
parent
567db560f5
commit
17c75c0228
2 changed files with 55 additions and 4 deletions
|
@ -172,9 +172,9 @@ func (htmlFS HTMLFileSystem) Open(name string) (http.File, error) {
|
||||||
return f, err
|
return f, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// The event broker allows the file watcher to publish site rebuild events
|
// The event broker mediates between the file watcher
|
||||||
// and register http clients to listen for them, in order to trigger browser refresh
|
// that publishes site rebuild events
|
||||||
// events after the the site has been rebuilt.
|
// and the clients listening for them to refresh the browser
|
||||||
type EventBroker struct {
|
type EventBroker struct {
|
||||||
inEvents chan string
|
inEvents chan string
|
||||||
inSubscriptions chan Subscription
|
inSubscriptions chan Subscription
|
||||||
|
|
|
@ -130,6 +130,7 @@ func runWatcher(config *config.Config) {
|
||||||
#+end_src
|
#+end_src
|
||||||
|
|
||||||
*** Build optimizations
|
*** Build optimizations
|
||||||
|
- optimization: ln static files
|
||||||
- optimization: worker pool
|
- optimization: worker pool
|
||||||
|
|
||||||
#+begin_src go
|
#+begin_src go
|
||||||
|
@ -202,7 +203,6 @@ func (site *site) build() error {
|
||||||
#+end_src
|
#+end_src
|
||||||
|
|
||||||
- in other languages, a similar change would have required adding async/await statements on half of my codebase.
|
- in other languages, a similar change would have required adding async/await statements on half of my codebase.
|
||||||
- optimization: ln static files
|
|
||||||
|
|
||||||
*** Live reload
|
*** Live reload
|
||||||
|
|
||||||
|
@ -272,7 +272,58 @@ newSSE();
|
||||||
- explain need
|
- explain need
|
||||||
- is this name right?
|
- is this name right?
|
||||||
- show api + link implementation
|
- show api + link implementation
|
||||||
|
see the full implementation [[https://github.com/facundoolano/jorge/blob/567db560f511b11492b85cf4f72b51599e8e3a3d/commands/serve.go#L175-L238][here]]
|
||||||
|
|
||||||
|
#+begin_src go
|
||||||
|
// The event broker mediates between the file watcher
|
||||||
|
// that publishes site rebuild events
|
||||||
|
// and the clients listening for them to refresh the browser
|
||||||
|
type EventBroker struct
|
||||||
|
|
||||||
|
func newEventBroker() *EventBroker
|
||||||
|
|
||||||
|
// Adds a subscription to this broker events
|
||||||
|
// returning a subscriber id (useful for unsubscribing)
|
||||||
|
// and a channel where events will be delivered.
|
||||||
|
func (broker *EventBroker) subscribe() (uint64, <-chan string)
|
||||||
|
|
||||||
|
// Remove the subscriber with the given id from the broker,
|
||||||
|
// closing its associated channel.
|
||||||
|
func (broker *EventBroker) unsubscribe(id uint64)
|
||||||
|
|
||||||
|
// Publish an event to all the broker subscribers.
|
||||||
|
func (broker *EventBroker) publish(event string)
|
||||||
|
|
||||||
|
|
||||||
|
#+end_src
|
||||||
- show updated handler
|
- show updated handler
|
||||||
|
#+begin_src diff
|
||||||
|
-func ServerEventsHandler (res http.ResponseWriter, req *http.Request) {
|
||||||
|
+func makeServerEventsHandler(broker *EventBroker) http.HandlerFunc {
|
||||||
|
+ return func(res http.ResponseWriter, req *http.Request) {
|
||||||
|
res.Header().Set("Content-Type", "text/event-stream")
|
||||||
|
res.Header().Set("Connection", "keep-alive")
|
||||||
|
res.Header().Set("Cache-Control", "no-cache")
|
||||||
|
res.Header().Set("Access-Control-Allow-Origin", "*")
|
||||||
|
|
||||||
|
+ id, events := broker.subscribe()
|
||||||
|
for {
|
||||||
|
select {
|
||||||
|
- case <-time.After(5 * time.Second):
|
||||||
|
+ case <-events:
|
||||||
|
// send an event to the connected client.
|
||||||
|
// data\n\n just means send an empty, unnamed event
|
||||||
|
fmt.Fprint(res, "data\n\n")
|
||||||
|
res.(http.Flusher).Flush()
|
||||||
|
case <-req.Context().Done():
|
||||||
|
// client connection closed
|
||||||
|
+ broker.unsubscribe(id)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#+end_src
|
||||||
- show updated watcher
|
- show updated watcher
|
||||||
|
|
||||||
#+begin_src diff
|
#+begin_src diff
|
||||||
|
|
Loading…
Reference in a new issue