Created
May 3, 2022 18:34
-
-
Save jakecoffman/70e0c67bbdf385531e7c1fec65408410 to your computer and use it in GitHub Desktop.
trickle server
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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