-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
executable file
·53 lines (42 loc) · 1.07 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
package main
import (
"encoding/json"
"flag"
"fmt"
"net/http"
)
var root string
func init() {
flag.StringVar(&root, "root", "/", "directory listed in the player when the application starts")
}
func directoryHandler(rw http.ResponseWriter, r *http.Request) {
var path string
if path = r.URL.Query().Get("path"); path == "" {
path = root
}
files, err := getFiles(path)
if err != nil {
fmt.Println(err)
}
enc := json.NewEncoder(rw)
if err := enc.Encode(files); err != nil {
fmt.Println(err)
}
}
func homeHandler(rw http.ResponseWriter, r *http.Request) {
http.ServeFile(rw, r, "static/mediaplayer.html")
}
func mediaFileHandler(rw http.ResponseWriter, r *http.Request) {
http.ServeFile(rw, r, r.URL.Query().Get("file"))
}
func staticFilesHandler(rw http.ResponseWriter, r *http.Request) {
http.ServeFile(rw, r, r.URL.Path[1:])
}
func main() {
flag.Parse()
http.HandleFunc("/", homeHandler)
http.HandleFunc("/dir", directoryHandler)
http.HandleFunc("/static/", staticFilesHandler)
http.HandleFunc("/media/", mediaFileHandler)
http.ListenAndServe(":9090", nil)
}