Skip to content

Instantly share code, notes, and snippets.

@steren
Created Jul 7, 2020
Embed
What would you like to do?
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