2022-05-14 20:33:45 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
|
|
|
|
"github.com/faiface/beep"
|
2023-03-26 15:29:24 +02:00
|
|
|
"github.com/getsentry/sentry-go"
|
2022-05-14 20:33:45 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
type playback struct {
|
|
|
|
File string
|
|
|
|
IsLoaded bool
|
2022-10-13 16:04:51 +02:00
|
|
|
Streamer beep.StreamSeeker
|
2022-05-14 20:33:45 +02:00
|
|
|
Control *beep.Ctrl
|
2022-05-26 17:46:03 +02:00
|
|
|
Loop bool
|
2022-10-13 16:04:51 +02:00
|
|
|
Format beep.Format
|
|
|
|
Done chan bool
|
2022-05-14 20:33:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
2023-03-26 15:29:24 +02:00
|
|
|
Port int
|
|
|
|
AllowSentry bool
|
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{
|
2023-03-26 15:29:24 +02:00
|
|
|
Port: 8080,
|
|
|
|
AllowSentry: true,
|
2022-05-16 20:43:33 +02:00
|
|
|
})
|
|
|
|
fmt.Println("Wrote to conf.json")
|
|
|
|
}
|
|
|
|
defer file.Close()
|
|
|
|
// Decode the config
|
|
|
|
decoder := json.NewDecoder(file)
|
|
|
|
configuration := Configuration{}
|
|
|
|
err := decoder.Decode(&configuration)
|
|
|
|
|
2023-03-26 15:29:24 +02:00
|
|
|
if configuration.AllowSentry {
|
|
|
|
err := sentry.Init(sentry.ClientOptions{
|
|
|
|
Dsn: "https://0eae4896cb23446b99a8a5b9f9da75f1@sentry.thegreydiamond.de/8",
|
|
|
|
// Enable printing of SDK debug messages.
|
|
|
|
// Useful when getting started or trying to figure something out.
|
|
|
|
// Debug: true,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("sentry.Init: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-16 20:43:33 +02:00
|
|
|
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)
|
2022-10-13 16:04:51 +02:00
|
|
|
http.HandleFunc("/v1/remaining", handleRemaining)
|
2022-10-13 12:47:44 +02:00
|
|
|
http.HandleFunc("/", handleRoot)
|
2022-05-16 20:43:33 +02:00
|
|
|
|
|
|
|
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
|
|
|
}
|