This repository has been archived by the owner on Jul 16, 2019. It is now read-only.
/
http.go
79 lines (66 loc) · 1.7 KB
/
http.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
package main
import (
"encoding/json"
"net/http"
"path"
"strings"
)
func playStream(res http.ResponseWriter, r *http.Request) {
if len(r.FormValue("stream")) > 0 {
streamChangeChan <- r.FormValue("stream")
http.Error(res, "OK", http.StatusOK)
return
}
http.Error(res, "Please provide a stream", http.StatusInternalServerError)
}
func getFilteredDirectoryList(res http.ResponseWriter, r *http.Request) {
res.Header().Add("Content-Type", "application/json")
search := r.URL.Query().Get("search")
if len(search) < 3 {
res.Write([]byte("[]"))
return
}
entries := directory.Search(search)
if len(entries) > 100 {
res.Write([]byte("[]"))
return
}
out, err := json.Marshal(entries)
if err != nil {
http.Error(res, "An error ocurred", http.StatusInternalServerError)
}
res.Write(out)
}
func getFavorites(res http.ResponseWriter, r *http.Request) {
body, err := json.Marshal(favorites)
if err != nil {
http.Error(res, "An error ocurred", http.StatusInternalServerError)
}
res.Header().Add("Content-Type", "application/json")
res.Write(body)
}
func serveStatic(res http.ResponseWriter, r *http.Request) {
p := strings.TrimLeft(r.URL.Path, "/")
if len(p) == 0 {
p = "index.html"
}
switch path.Ext(p) {
case ".html":
res.Header().Add("Content-Type", "text/html")
case ".js":
res.Header().Add("Content-Type", "application/javascript")
default:
http.Error(res, "Nope.", http.StatusNotFound)
return
}
file, err := Asset(path.Join("frontend", p))
if err != nil {
http.Error(res, "Nope.", http.StatusNotFound)
return
}
res.Write(file)
}
func getVersion(res http.ResponseWriter, r *http.Request) {
res.Header().Set("Content-Type", "text/plain")
res.Write([]byte(version))
}