Skip to content

Instantly share code, notes, and snippets.

@jakecoffman
Created May 3, 2022 18:34
Show Gist options
  • Save jakecoffman/70e0c67bbdf385531e7c1fec65408410 to your computer and use it in GitHub Desktop.
Save jakecoffman/70e0c67bbdf385531e7c1fec65408410 to your computer and use it in GitHub Desktop.
trickle server
package main
import (
"log"
"net/http"
"time"
)
func main() {
log.SetFlags(log.Lshortfile)
mux := http.NewServeMux()
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
log.Println("Request came in")
timeout := time.After(10 * time.Second)
for {
select {
case <-timeout:
log.Println("Time up")
return
case <-r.Context().Done():
log.Println("Client disconnected")
return
default:
}
log.Println("Stream some")
w.Write([]byte("stream some"))
if f, ok := w.(http.Flusher); ok {
f.Flush()
}
time.Sleep(1 * time.Second)
}
})
log.Println("Serving http://127.0.0.1:8080")
if err := http.ListenAndServe("127.0.0.1:8080", mux); err != nil {
log.Fatal(err)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment