From 3eb76009ad539554fe62d329c34a080e99d5f2ba Mon Sep 17 00:00:00 2001 From: Stephen McQuay Date: Mon, 24 Nov 2014 14:32:27 -0800 Subject: [PATCH] moved http routes out of the server main() --- botserv/main.go | 15 +-------------- control.go | 23 +++++++++++++++++++---- 2 files changed, 20 insertions(+), 18 deletions(-) diff --git a/botserv/main.go b/botserv/main.go index e3ab3b9..9ff590d 100644 --- a/botserv/main.go +++ b/botserv/main.go @@ -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") } diff --git a/control.go b/control.go index 322fe61..a1b8985 100644 --- a/control.go +++ b/control.go @@ -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