Created
July 7, 2020 03:52
-
-
Save steren/032dc5575c530acf82e50a226ff40799 to your computer and use it in GitHub Desktop.
Transfer-Encoding: chunked
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 ( | |
//"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