Old and unmaintained
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

100 lines
2.8 KiB

package main
import (
"flag"
"fmt"
"log"
"net/http"
"os"
"strconv"
"sync"
"time"
"git.remi.im/internet-of-st00fs/speakd/speakc"
"github.com/jteeuwen/go-pkg-mpd"
)
func main() {
cfgFileName := flag.String("config-filename", "config.json", "")
flag.Parse()
cfg, isNew, err := loadConfig(*cfgFileName)
if err != nil {
fmt.Printf("Could not read JSON from configuration file with name %s. "+
"Could not create it either.\nMaybe the following error message will be of use; %s.\n",
*cfgFileName, err.Error())
os.Exit(1)
return
}
if isNew {
fmt.Printf("Configuration file created. Saved as %s. Please look at it, maybe change some values and rerun speakd.\n", *cfgFileName)
os.Exit(2)
return
}
if cfg.HTTPPort == 0 {
fmt.Println("HTTPPort not specified in configuration file.")
os.Exit(4)
return
}
mpdC, err := mpd.Dial(cfg.MPDURL, "")
if err != nil {
fmt.Printf("ERROR connecting to MPD. Is mpdUrl (%q) in the configuration file correct?\n", cfg.MPDURL)
fmt.Println(err)
os.Exit(1)
}
mpdCMutex := &sync.Mutex{}
timeout := newStopTimeout(mpdC,
mpdCMutex,
log.New(os.Stdout, "timeout ", log.Ldate|log.Ltime),
log.New(os.Stderr, "timeouterror", log.Ldate|log.Ltime))
// Set up the env shared between handlers and handled requests.
e := env{
mpdC: mpdC,
mpdCMutex: mpdCMutex,
mpdURL: cfg.MPDURL,
log: log.New(os.Stdout, "http ", log.Ldate|log.Ltime),
logErr: log.New(os.Stderr, "httperror ", log.Ldate|log.Ltime),
playlists: cfg.Playlists,
stopTimeout: timeout,
speakc: &speakc.Client{
BaseURL: cfg.SpeakdBaseURL,
CallbackURL: cfg.SpeakdCallbackBaseURL + "/speakc-callback",
},
currentPlMutex: &sync.RWMutex{},
currentPl: &playlist{},
lockedModePhrase: new(string),
lockedModeMutex: &sync.RWMutex{},
}
// Set up the HTTP server.
mux := http.NewServeMux()
mux.Handle("/load", handle(e, checkLocked, handleLoad))
mux.Handle("/stop", handle(e, checkLocked, handleStop))
mux.Handle("/timeout", handle(e, checkLocked, handleTimeout))
mux.Handle("/seek", handle(e, checkLocked, handleSeek))
mux.Handle("/seek/relative", handle(e, checkLocked, handleSeekRelative))
mux.Handle("/play", handle(e, checkLocked, handlePlay))
mux.Handle("/play/relative", handle(e, checkLocked, handlePlayRelative))
mux.Handle("/volume", handle(e, checkLocked, handleVolume))
mux.Handle("/volume/relative", handle(e, checkLocked, handleVolumeRelative))
mux.Handle("/speak", handle(e, checkLocked, handleSpeak))
mux.Handle("/speakc-callback", e.speakc.CallbackHandler())
srv := &http.Server{
Addr: ":" + strconv.Itoa(cfg.HTTPPort),
Handler: mux,
ReadTimeout: 5 * time.Second,
WriteTimeout: 10 * time.Second,
}
e.log.Printf("Started HTTP server. Listening at port %d.\n", cfg.HTTPPort)
log.Fatal(srv.ListenAndServe())
}