2013-08-19 20:43:26 -07:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"flag"
|
|
|
|
"log"
|
|
|
|
"math/rand"
|
|
|
|
"net/http"
|
2013-11-06 22:14:22 -08:00
|
|
|
_ "net/http/pprof"
|
2013-09-28 23:00:29 -07:00
|
|
|
"os"
|
|
|
|
"runtime/pprof"
|
2013-08-19 20:43:26 -07:00
|
|
|
"time"
|
2013-11-29 00:10:49 -08:00
|
|
|
|
2014-04-23 14:28:13 -07:00
|
|
|
hbserver "bitbucket.org/hackerbots/server"
|
2013-11-29 00:10:49 -08:00
|
|
|
"code.google.com/p/go.net/websocket"
|
2013-08-19 20:43:26 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
var addr = flag.String("addr", ":8666", "http service address")
|
2013-09-28 23:00:29 -07:00
|
|
|
var profile = flag.String("pprof", "", "if specified will run with pprof")
|
2014-03-08 15:29:08 -08:00
|
|
|
var mprofile = flag.String("mprof", "", "if specified will dump a memory profile")
|
|
|
|
var netprofile = flag.Bool("netprof", false, "if specified will run with net/http/pprof on :8667")
|
2013-11-13 20:38:57 -08:00
|
|
|
var verbose = flag.Bool("verbose", false, "")
|
|
|
|
var config = flag.String("config", "~/.config/hackerbots/config.json", "location of config file")
|
2013-08-19 20:43:26 -07:00
|
|
|
|
|
|
|
func main() {
|
2014-01-13 20:54:01 -08:00
|
|
|
log.SetFlags(log.Ldate | log.Ltime | log.Lshortfile)
|
2013-10-20 21:15:23 -07:00
|
|
|
log.Printf("Starting Server...")
|
2013-11-13 20:38:57 -08:00
|
|
|
var err error
|
2013-10-20 21:15:23 -07:00
|
|
|
|
2013-08-19 20:43:26 -07:00
|
|
|
rand.Seed(time.Now().UnixNano())
|
|
|
|
flag.Parse()
|
2013-09-28 23:00:29 -07:00
|
|
|
if *profile != "" {
|
|
|
|
f, err := os.Create(*profile)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
pprof.StartCPUProfile(f)
|
|
|
|
}
|
2013-11-06 22:14:22 -08:00
|
|
|
if *netprofile {
|
|
|
|
go func() {
|
|
|
|
log.Println(http.ListenAndServe("localhost:8667", nil))
|
|
|
|
}()
|
2014-03-08 18:28:08 -08:00
|
|
|
log.Println("serving profile info at http://localhost:8667/debug/pprof/")
|
2013-11-06 22:14:22 -08:00
|
|
|
}
|
2013-08-19 20:43:26 -07:00
|
|
|
|
2014-04-23 14:28:13 -07:00
|
|
|
conf, err := hbserver.LoadConfig(*config)
|
2013-11-13 20:38:57 -08:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2013-11-06 22:14:22 -08:00
|
|
|
sm := http.NewServeMux()
|
|
|
|
|
2014-04-23 14:28:13 -07:00
|
|
|
c := hbserver.NewController(conf, *mprofile, *profile)
|
2014-03-31 22:54:30 -07:00
|
|
|
go c.Run()
|
2014-03-18 23:38:06 -07:00
|
|
|
|
2014-04-23 14:28:13 -07:00
|
|
|
sm.Handle("/", hbserver.JsonHandler(c.Index))
|
2014-03-18 23:38:06 -07:00
|
|
|
sm.Handle("/ws/", websocket.Handler(c.AddPlayer))
|
2014-04-23 14:28:13 -07:00
|
|
|
sm.Handle("/game/start/", hbserver.JsonHandler(c.StartGame))
|
|
|
|
sm.Handle("/game/list/", hbserver.JsonHandler(c.ListGames))
|
|
|
|
sm.Handle("/game/stats/", hbserver.JsonHandler(c.GameStats))
|
|
|
|
sm.Handle("/game/bw/", hbserver.JsonHandler(c.BW))
|
|
|
|
sm.Handle("/game/stop/", hbserver.JsonHandler(c.StopGame))
|
2014-03-18 23:38:06 -07:00
|
|
|
sm.HandleFunc("/fuck/shit/up/", c.KillServer)
|
2013-08-19 20:43:26 -07:00
|
|
|
|
2013-11-13 20:38:57 -08:00
|
|
|
err = http.ListenAndServe(*addr, sm)
|
2013-08-19 20:43:26 -07:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal("unable to start server")
|
|
|
|
}
|
|
|
|
}
|