-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
66 lines (52 loc) · 1.59 KB
/
main.go
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package main
import (
"fmt"
"net/http"
"strconv"
"github.com/gorilla/mux"
)
func main() {
router := mux.NewRouter()
router.HandleFunc("/", indexPage).Methods("GET")
router.HandleFunc("/favicon.ico", http.NotFound).Methods("GET")
router.HandleFunc("/media/{mid:[0-9]+}/stream/", streamHandler).Methods("GET")
router.HandleFunc("/media/{mid:[0-9]+}/stream/{segName:index[0-9]+.ts}", streamHandler).Methods("GET")
http.Handle("/", router)
http.ListenAndServe(":8080", nil)
}
func indexPage(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, "index.html")
}
func streamHandler(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
mid, err := strconv.Atoi(vars["mid"])
if err != nil {
w.WriteHeader(http.StatusNotFound)
return
}
segName, ok := vars["segName"]
if !ok {
mediaBase := getMediaBase(mid)
m3u8Name := "index.m3u8"
serveHLSm3u8(w, r, mediaBase, m3u8Name)
return
}
mediaBase := getMediaBase(mid)
serveHLSts(w, r, mediaBase, segName)
}
func getMediaBase(mid int) string {
mediaRoot := "assets/media"
return fmt.Sprintf("%s/%d", mediaRoot, mid)
}
func serveHLSm3u8(w http.ResponseWriter, r *http.Request, mediaBase, m3u8Name string) {
mediaFile := fmt.Sprintf("%s/hls/%s", mediaBase, m3u8Name)
fmt.Println(mediaFile)
http.ServeFile(w, r, mediaFile)
w.Header().Set("Content-Type", "application/x-mpegURL")
}
func serveHLSts(w http.ResponseWriter, r *http.Request, mediaBase, segName string) {
mediaFile := fmt.Sprintf("%s/hls/%s", mediaBase, segName)
fmt.Println(mediaFile)
http.ServeFile(w, r, mediaFile)
w.Header().Set("Content-Type", "video/MP2T")
}