moved http routes out of the server main()

This commit is contained in:
Stephen McQuay 2014-11-24 14:32:27 -08:00
parent b15f010073
commit 3eb76009ad
2 changed files with 20 additions and 18 deletions

View File

@ -11,7 +11,6 @@ import (
"time"
"bitbucket.org/hackerbots/server"
"golang.org/x/net/websocket"
)
var addr = flag.String("addr", ":8666", "http service address")
@ -47,21 +46,9 @@ func main() {
log.Fatal(err)
}
sm := http.NewServeMux()
c := server.NewController(conf, *mprofile, *profile)
go c.Run()
sm.Handle("/", server.JsonHandler(c.Index))
sm.Handle("/ws/", websocket.Handler(c.AddPlayer))
sm.Handle("/game/start/", server.JsonHandler(c.StartGame))
sm.Handle("/game/list/", server.JsonHandler(c.ListGames))
sm.Handle("/game/stats/", server.JsonHandler(c.GameStats))
sm.Handle("/game/bw/", server.JsonHandler(c.BW))
sm.Handle("/game/stop/", server.JsonHandler(c.StopGame))
sm.HandleFunc("/fuck/shit/up/", c.KillServer)
err = http.ListenAndServe(*addr, sm)
err = http.ListenAndServe(*addr, c)
if err != nil {
log.Fatal("unable to start server")
}

View File

@ -11,6 +11,8 @@ import (
"runtime/pprof"
"strings"
"sync"
"golang.org/x/net/websocket"
)
// JsonHandler is a function type that allows setting the Content-Type
@ -36,10 +38,9 @@ type Controller struct {
// NewController takes a populated Config, and some parameters to determine
// what sorts of profiling to deal with and returns a freshly populated
// Controller.
func NewController(conf Config, mprof, pprof string) *Controller {
idg := NewIdGenerator()
return &Controller{
Idg: idg,
func NewController(conf Config, mprof, pprof string) *http.ServeMux {
c := &Controller{
Idg: NewIdGenerator(),
Conf: conf,
Games: MapLock{
M: make(map[string]*Game),
@ -47,6 +48,20 @@ func NewController(conf Config, mprof, pprof string) *Controller {
Memprofile: mprof,
Profile: pprof,
}
go c.Run()
sm := http.NewServeMux()
sm.Handle("/", JsonHandler(c.Index))
sm.Handle("/ws/", websocket.Handler(c.AddPlayer))
sm.Handle("/game/start/", JsonHandler(c.StartGame))
sm.Handle("/game/list/", JsonHandler(c.ListGames))
sm.Handle("/game/stats/", JsonHandler(c.GameStats))
sm.Handle("/game/bw/", JsonHandler(c.BW))
sm.Handle("/game/stop/", JsonHandler(c.StopGame))
sm.HandleFunc("/fuck/shit/up/", c.KillServer)
return sm
}
// TODO Eventually this thing will have a select loop for dealing with game