golang MP4 文件服务器

https://studygolang.com/articles/11204

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
package main

import (
    "github.com/gorilla/handlers"
    "log"
    "net/http"
    "os"
    "time"
)

func ServeHTTP(w http.ResponseWriter, r *http.Request) {
    video, err := os.Open("/tmp/foo.mp4")
    if err != nil {
        log.Fatal(err)
    }
    defer video.Close()

    http.ServeContent(w, r, "foo.mp4", time.Now(), video)
}

func main() {
    http.HandleFunc("/", ServeHTTP)
    _ = http.ListenAndServe(":8089", handlers.LoggingHandler(os.Stdout, http.DefaultServeMux))
}