Created
June 24, 2017 21:49
-
-
Save nicolasazrak/b1130c3647c42be5bd4de5831f61cf67 to your computer and use it in GitHub Desktop.
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 ( | |
"fmt" | |
"io" | |
"io/ioutil" | |
"net/http" | |
"os" | |
"strings" | |
"time" | |
) | |
func producer(f *os.File, ch chan struct{}) { | |
content := []string{"a", "b", "c", "d", "e"} | |
for _, letter := range content { | |
time.Sleep(time.Duration(2) * time.Second) | |
f.Write([]byte(strings.Repeat(letter, 2000) + "\n\n")) | |
f.Sync() | |
ch <- struct{}{} | |
} | |
close(ch) | |
} | |
func main() { | |
f, err := ioutil.TempFile("", "") | |
if err != nil { | |
panic(err) | |
} | |
ch := make(chan struct{}) | |
fmt.Println("File created %s", f.Name()) | |
go producer(f, ch) | |
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { | |
otherF, err := os.Open(f.Name()) | |
if err != nil { | |
panic(err) | |
} | |
fl := w.(http.Flusher) | |
for range ch { | |
io.Copy(w, otherF) | |
fl.Flush() | |
} | |
}) | |
fmt.Println("Server started") | |
http.ListenAndServe(":8080", nil) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment