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" "time"
"bitbucket.org/hackerbots/server" "bitbucket.org/hackerbots/server"
"golang.org/x/net/websocket"
) )
var addr = flag.String("addr", ":8666", "http service address") var addr = flag.String("addr", ":8666", "http service address")
@ -47,21 +46,9 @@ func main() {
log.Fatal(err) log.Fatal(err)
} }
sm := http.NewServeMux()
c := server.NewController(conf, *mprofile, *profile) c := server.NewController(conf, *mprofile, *profile)
go c.Run()
sm.Handle("/", server.JsonHandler(c.Index)) err = http.ListenAndServe(*addr, c)
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)
if err != nil { if err != nil {
log.Fatal("unable to start server") log.Fatal("unable to start server")
} }

View File

@ -11,6 +11,8 @@ import (
"runtime/pprof" "runtime/pprof"
"strings" "strings"
"sync" "sync"
"golang.org/x/net/websocket"
) )
// JsonHandler is a function type that allows setting the Content-Type // 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 // NewController takes a populated Config, and some parameters to determine
// what sorts of profiling to deal with and returns a freshly populated // what sorts of profiling to deal with and returns a freshly populated
// Controller. // Controller.
func NewController(conf Config, mprof, pprof string) *Controller { func NewController(conf Config, mprof, pprof string) *http.ServeMux {
idg := NewIdGenerator() c := &Controller{
return &Controller{ Idg: NewIdGenerator(),
Idg: idg,
Conf: conf, Conf: conf,
Games: MapLock{ Games: MapLock{
M: make(map[string]*Game), M: make(map[string]*Game),
@ -47,6 +48,20 @@ func NewController(conf Config, mprof, pprof string) *Controller {
Memprofile: mprof, Memprofile: mprof,
Profile: pprof, 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 // TODO Eventually this thing will have a select loop for dealing with game