61 lines
1014 B
Go
61 lines
1014 B
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
|
||
|
|
||
|
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)
|
||
|
|
||
|
return s
|
||
|
}
|
||
|
|
||
|
// ListenAndServe starts the HTTP app.
|
||
|
func (s *Sonar) ListenAndServe(address string) error {
|
||
|
label := address
|
||
|
if strings.HasPrefix(label, ":") {
|
||
|
label = "0.0.0.0" + label
|
||
|
}
|
||
|
log.Info("Listening at http://%s", label)
|
||
|
|
||
|
return http.ListenAndServe(address, s.n)
|
||
|
}
|