-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebserver.go
67 lines (59 loc) · 1.52 KB
/
webserver.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
package main
import (
"encoding/json"
"log"
"net/http"
"os"
"path/filepath"
"strings"
"time"
)
var avaiableStreams []string
func main() {
log.Println("Scanning video/ directory for mpd files.")
if err := filepath.Walk("./video/", discover); err != nil {
log.Fatalln("Could not read video/ directory.")
}
log.Println("Server is up and running at http://localhost:8080/")
http.Handle("/", request())
if err := http.ListenAndServe(":8080", nil); err != nil {
log.Fatalln(err)
}
}
func discover(path string, f os.FileInfo, err error) error {
filename := f.Name()
if strings.HasSuffix(filename, ".mpd") && !strings.Contains(filename, "onDemand") && err == nil {
avaiableStreams = append(avaiableStreams, "/"+path)
}
return nil
}
//Router and logger
func request() http.HandlerFunc {
return (func(w http.ResponseWriter, r *http.Request) {
start := time.Now()
switch r.URL.Path {
case "/":
serveIndex(w, r)
case "/streams":
listStreams(w, r)
default:
serveVideo(w, r)
}
elapsed := time.Since(start) / 1000
log.Printf("HTTP Request: %s %s, Total time: %dms\n", r.Method, r.URL.Path, elapsed)
})
}
//Serve index file
func serveIndex(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, "./videoplayer.html")
}
//List available streams
func listStreams(w http.ResponseWriter, r *http.Request) {
encoder := json.NewEncoder(w)
encoder.Encode(avaiableStreams)
}
//Serve video files
func serveVideo(w http.ResponseWriter, r *http.Request) {
path := "." + r.URL.Path
http.ServeFile(w, r, path)
}