Skip to content

Instantly share code, notes, and snippets.

@steren
Created July 7, 2020 03:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save steren/032dc5575c530acf82e50a226ff40799 to your computer and use it in GitHub Desktop.
Save steren/032dc5575c530acf82e50a226ff40799 to your computer and use it in GitHub Desktop.
Transfer-Encoding: chunked
package main
import (
//"bytes"
//"io/ioutil"
"fmt"
"io"
"log"
"net/http"
"os"
)
func main() {
http.HandleFunc("/videos/big-buck-bunny.mp4", func(w http.ResponseWriter, r *http.Request) {
// Direct file serving
//http.ServeFile(w, r, "videos/big-buck-bunny.mp4")
w.Header().Set("Content-Type", "video/mp4")
w.Header().Set("Transfer-Encoding", "chunked")
f, _ := os.Open("videos/big-buck-bunny.mp4")
// Using a Reader?
//r := bufio.NewReader(f)
// Using Buffer
//streamVideoBytes, _ := ioutil.ReadFile("videos/big-buck-bunny.mp4")
//b := bytes.NewBuffer(streamVideoBytes)
//b.WriteTo(w)
// Using a Copy
io.Copy(w, f)
})
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
io.WriteString(w, `<!DOCTYPE html>
<html>
<body>
<video controls autoplay width="1024">
<source src="/videos/big-buck-bunny.mp4" type="video/mp4">
</video>
</body></html>`)
})
port := os.Getenv("PORT")
if port == "" {
port = "8080"
}
log.Print("Hello from Cloud Run! The container started successfully and is listening for HTTP requests on $PORT.")
log.Fatal(http.ListenAndServe(fmt.Sprintf(":%s", port), nil))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment