2022-05-14 20:33:45 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
|
|
|
|
"github.com/faiface/beep"
|
|
|
|
)
|
|
|
|
|
|
|
|
type playback struct {
|
|
|
|
File string
|
|
|
|
IsLoaded bool
|
|
|
|
Streamer beep.Streamer
|
|
|
|
Control *beep.Ctrl
|
|
|
|
}
|
|
|
|
|
|
|
|
type playbackWebReturn struct {
|
|
|
|
File string
|
|
|
|
IsLoaded bool
|
|
|
|
Id int
|
|
|
|
}
|
|
|
|
|
|
|
|
type streamBuf struct {
|
|
|
|
Streamer beep.Streamer
|
|
|
|
Format beep.Format
|
|
|
|
Buffer *beep.Buffer
|
|
|
|
}
|
|
|
|
|
2022-05-16 20:43:33 +02:00
|
|
|
type Configuration struct {
|
|
|
|
Port int
|
2022-05-14 20:33:45 +02:00
|
|
|
}
|
|
|
|
|
2022-05-16 20:43:33 +02:00
|
|
|
var playbacks map[int]playback
|
2022-05-14 20:33:45 +02:00
|
|
|
|
2022-05-16 20:43:33 +02:00
|
|
|
var streamMap map[string]streamBuf
|
2022-05-14 20:33:45 +02:00
|
|
|
|
|
|
|
func main() {
|
|
|
|
|
2022-05-16 20:43:33 +02:00
|
|
|
fmt.Println("Welcome to Soundr!")
|
|
|
|
|
2022-05-14 20:33:45 +02:00
|
|
|
playbacks = make(map[int]playback)
|
|
|
|
streamMap = make(map[string]streamBuf)
|
|
|
|
|
2022-05-16 20:43:33 +02:00
|
|
|
// Create /sounds if not exists
|
|
|
|
if _, err := os.Stat("./sounds"); os.IsNotExist(err) {
|
|
|
|
fmt.Println("Created /sounds folder")
|
|
|
|
os.Mkdir("./sounds", 0777)
|
|
|
|
}
|
2022-05-14 20:33:45 +02:00
|
|
|
|
2022-05-16 20:43:33 +02:00
|
|
|
// Handle config
|
|
|
|
fmt.Println("Opening conf.json")
|
|
|
|
file, fOpenError := os.Open("conf.json") // Try to open the file
|
2022-05-14 20:33:45 +02:00
|
|
|
|
2022-05-16 20:43:33 +02:00
|
|
|
if errors.Is(fOpenError, os.ErrNotExist) { // If it does not exist, create it
|
|
|
|
fmt.Println("Creating conf.json")
|
|
|
|
file, fOpenError = os.Create("conf.json")
|
|
|
|
if fOpenError != nil {
|
|
|
|
log.Fatal(fOpenError)
|
2022-05-14 20:33:45 +02:00
|
|
|
}
|
2022-05-16 20:43:33 +02:00
|
|
|
defer file.Close()
|
|
|
|
fmt.Println("Writing to conf.json")
|
|
|
|
// Write the default config to the file
|
|
|
|
json.NewEncoder(file).Encode(Configuration{
|
|
|
|
Port: 8080,
|
|
|
|
})
|
|
|
|
fmt.Println("Wrote to conf.json")
|
|
|
|
}
|
|
|
|
defer file.Close()
|
|
|
|
// Decode the config
|
|
|
|
decoder := json.NewDecoder(file)
|
|
|
|
configuration := Configuration{}
|
|
|
|
err := decoder.Decode(&configuration)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println("error:", err)
|
|
|
|
}
|
2022-05-14 20:33:45 +02:00
|
|
|
|
2022-05-16 20:43:33 +02:00
|
|
|
// Web server stuff
|
|
|
|
// Play route, takes file as parameter, file is base64 encoded
|
|
|
|
http.HandleFunc("/v1/play", handlePlay)
|
|
|
|
// Buffer route, buffers file
|
|
|
|
http.HandleFunc("/v1/buffer", handleBuffer)
|
|
|
|
http.HandleFunc("/v1/bufferAll", handleBufferAll)
|
|
|
|
http.HandleFunc("/v1/stop", handleStop)
|
|
|
|
http.HandleFunc("/v1/stopAll", handleStopAll)
|
|
|
|
http.HandleFunc("/v1/current", handleCurrent)
|
|
|
|
http.HandleFunc("/v1/list", handleListing)
|
|
|
|
|
|
|
|
fmt.Println("Listening on port " + fmt.Sprint(configuration.Port))
|
|
|
|
log.Fatal(http.ListenAndServe(":"+fmt.Sprint(configuration.Port), nil))
|
2022-05-14 20:33:45 +02:00
|
|
|
}
|