-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
122 lines (98 loc) · 3.21 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package main
import (
"fmt"
"github.com/gorilla/mux"
log "github.com/sirupsen/logrus"
"html/template"
"net/http"
)
func main() {
// define gorilla router
router := mux.NewRouter().StrictSlash(true)
// the index router
router.HandleFunc("/", Index).Methods("GET")
// the file system structure
/* /storage/{cdn-disk}/posts/{post-hash}/{video-hash}/{ts-steam-index} */
// the base streaming router
router.HandleFunc("/storage/{cdn:disk[0-9]+}/posts/{post}/{video}/", Streaming).Methods("GET")
// the base streaming router
router.HandleFunc("/storage/{cdn:disk[0-9]+}/posts/{post}/{video}/index.m3u8", Streaming).Methods("GET")
// the other streaming router
router.HandleFunc("/storage/{cdn:disk[0-9]+}/posts/{post}/{video}/{segment:index[0-9]+.ts}", Streaming).Methods("GET")
// the subtitle file
router.HandleFunc("/storage/{cdn:disk[0-9]+}/posts/{post}/{video}/subtitle.srt", Subtitle).Methods("GET")
// the image file
router.HandleFunc("/img/{image}", Image).Methods("GET")
// the js file
router.HandleFunc("/assets/js/{js}", Js).Methods("GET")
log.Info("server is now running on http://localhost:8000")
// if anything goes wrong with the server PANIC!
panic(http.ListenAndServe(":8000", router))
}
// Index Handler to view the index.html page
func Index(w http.ResponseWriter, r *http.Request) {
// parsing templates before execute them
index, err := template.ParseFiles("index.html")
if err != nil {
log.Fatalf("could not parsing the template %v \n", err)
}
// execute template to render it
data := struct {
Post string
Video string
Cdn string
}{
Post: "fc7e987f23de5bd6562b",
Video: "7c0063cad659",
Cdn: "disk1",
}
if err := index.Execute(w, data); err != nil {
log.Fatalf("could not parsing the template %v\n", err)
return
}
return
}
// Streaming Handler to handle the video streaming
func Streaming(w http.ResponseWriter, r *http.Request) {
// parse all querystring & parameters to vars
vars := mux.Vars(r)
post := vars["post"]
video := vars["video"]
cdn := vars["cdn"]
path := fmt.Sprintf("storage/%s/posts/%s/%s", cdn, post, video)
var file, contentType string
segment, ok := vars["segment"]
if !ok {
contentType = "application/x-mpegURL"
file = fmt.Sprintf("%s/%s", path, "index.m3u8")
} else {
contentType = "video/MP2T"
file = fmt.Sprintf("%s/%s", path, segment)
}
w.Header().Set("Content-Type", contentType)
http.ServeFile(w, r, file)
}
// Subtitle Handler to handle the subtitle serving
func Subtitle(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/javascript")
vars := mux.Vars(r)
post := vars["post"]
video := vars["video"]
cdn := vars["cdn"]
path := fmt.Sprintf("storage/%s/posts/%s/%s/subtitle.srt", cdn, post, video)
http.ServeFile(w, r, path)
}
// Image Handler to handle the image serving
func Image(w http.ResponseWriter, r *http.Request) {
//w.Header().Set("Content-Type", "text/javascript")
vars := mux.Vars(r)
path := fmt.Sprintf("img/%s", vars["image"])
http.ServeFile(w, r, path)
}
// Js Handler to handle the js serving
func Js(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/javascript")
vars := mux.Vars(r)
path := fmt.Sprintf("js/%s", vars["js"])
http.ServeFile(w, r, path)
}