sonar/sonar.go

64 lines
1.1 KiB
Go

package sonar
import (
"net/http"
"strings"
"github.com/gorilla/mux"
"github.com/gorilla/sessions"
"github.com/urfave/negroni"
)
// Program constants.
const (
Version = "0.1.0"
)
// Sonar is the master app object.
type Sonar struct {
Debug bool
Player *Player
Config *Config
url string
n *negroni.Negroni
router *mux.Router
sessions sessions.Store
}
// New creates the Sonar app.
func New() *Sonar {
s := &Sonar{
Player: &Player{},
}
s.Config = LoadConfig()
router := s.Register()
s.router = router
s.sessions = sessions.NewCookieStore([]byte("XXX DEBUG KEY")) // TODO
s.n = negroni.New(
negroni.NewRecovery(),
negroni.NewLogger(),
negroni.NewStatic(http.Dir("./www")),
)
s.n.UseHandler(router)
// Watch the player to advance to the next track.
go s.Watch()
return s
}
// ListenAndServe starts the HTTP app.
func (s *Sonar) ListenAndServe(address string) error {
s.url = address
if strings.HasPrefix(s.url, ":") {
s.url = "0.0.0.0" + s.url
}
log.Info("Listening at http://%s", s.url)
return http.ListenAndServe(address, s.n)
}