-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
138 lines (119 loc) · 3.32 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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
package main
import (
"flag"
"fmt"
"io"
"log"
"net/http"
"os"
"time"
"github.com/Hundemeier/go-sacn/sacn"
assetfs "github.com/elazarl/go-bindata-assetfs"
"github.com/faiface/beep"
"github.com/faiface/beep/speaker"
"github.com/fatih/color"
)
const poolDir = "./pool"
const sampleRate = beep.SampleRate(44100)
//slotMap contains the mapping of a slot to a filename in the poolDir
var slotMap = newSyncSlotMap()
var port = flag.Uint("port", 8000, "the port on which the webinterface is listening. Only use port 80, when no other application is using this port!")
var universe = flag.Uint("universe", 1, "the sACN universe on which the player should listen on incoming DMX data. Currently only via Unicast.")
func main() {
slotMap.onPlayerChange = func(slot uint, play *player) {
item, err := slotMap.getSlotItem(slot)
if err == nil {
go websockets.writeToWebsockets(item)
}
}
flag.Parse()
initSpeaker()
makeSlotMapFromConfig()
initSacn()
initGraphql()
initWebService()
}
func initSpeaker() {
err := speaker.Init(sampleRate, sampleRate.N(time.Second/15))
if err != nil {
log.Fatal(err)
}
}
func initWebService() {
fmt.Println("Starting server...")
server := http.Server{
Addr: fmt.Sprintf(":%v", *port),
}
fmt.Println("Serving at:")
fmt.Printf("\thttp://127.0.0.1:%v\n", *port)
addrs, _ := getMyInterfaceAddr()
for _, addr := range addrs {
fmt.Printf("\thttp://%v%v\n", addr, server.Addr)
}
//fmt.Println("Close with \033[47;30m Ctrl+C \033[0m")
fmt.Print("Close with ")
color.Set(color.FgBlack)
color.Set(color.BgWhite)
fmt.Print(" Ctrl+C ")
color.Unset()
fmt.Print("\n")
//http.Handle("/", http.FileServer(http.Dir("webgui/")))
http.Handle("/", http.FileServer(&assetfs.AssetFS{Asset: Asset, AssetDir: AssetDir, AssetInfo: AssetInfo, Prefix: "webgui/dist"}))
http.HandleFunc("/pool/upload", uploadHandler)
http.HandleFunc("/websocket", handleWebsocket)
log.Fatal(server.ListenAndServe())
}
func initSacn() {
recv, err := sacn.NewReceiverSocket("", nil)
if err != nil {
fmt.Println("Error! sACN could not be initalized! sACN is not available!")
}
recv.SetOnChangeCallback(sACNhandler)
recv.Start()
}
func makeSlotMapFromConfig() {
conf := readConfig()
for _, item := range conf.SlotMap {
slotMap.setFileForSlot(item.Slot, item.Filename)
player := slotMap.getPlayer(item.Slot)
if player != nil {
player.setVolume(item.Player.Volume)
player.loop = item.Player.Loop
}
}
}
func uploadHandler(w http.ResponseWriter, r *http.Request) {
if r.Method == "POST" {
r.ParseMultipartForm(32 << 20)
httpFile, head, err := r.FormFile("uploadfile")
if err != nil {
//fmt.Println("Err:", err)
w.WriteHeader(http.StatusBadRequest)
return
}
defer httpFile.Close()
if head.Size != 0 {
//check if dir "pool" exists, otherwise create directory
if _, err := os.Stat(poolDir); os.IsNotExist(err) {
err = os.MkdirAll(poolDir, 0755)
if err != nil {
fmt.Println("Error:", err)
}
}
path := poolDir + "/" + head.Filename
f, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE, 0755)
if err != nil {
//fmt.Println("Err:", err)
w.WriteHeader(http.StatusInternalServerError)
return
}
defer f.Close()
_, err = io.Copy(f, httpFile)
if err != nil {
//fmt.Println("Err:", err)
w.WriteHeader(http.StatusInternalServerError)
return
}
}
}
}