You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

80 lines
2.0KB

  1. package main
  2. import (
  3. "flag"
  4. "log"
  5. "math/rand"
  6. "net/http"
  7. _ "net/http/pprof"
  8. "os"
  9. "runtime/pprof"
  10. "time"
  11. "bitbucket.org/hackerbots/botserv"
  12. "code.google.com/p/go.net/websocket"
  13. )
  14. var addr = flag.String("addr", ":8666", "http service address")
  15. var profile = flag.String("pprof", "", "if specified will run with pprof")
  16. var mprofile = flag.String("mprof", "", "if specified will dump a memory profile")
  17. var netprofile = flag.Bool("netprof", false, "if specified will run with net/http/pprof on :8667")
  18. var verbose = flag.Bool("verbose", false, "")
  19. var config = flag.String("config", "~/.config/hackerbots/config.json", "location of config file")
  20. var delta float32
  21. func main() {
  22. log.SetFlags(log.Ldate | log.Ltime | log.Lshortfile)
  23. log.Printf("Starting Server...")
  24. var err error
  25. rand.Seed(time.Now().UnixNano())
  26. flag.Parse()
  27. if *profile != "" {
  28. f, err := os.Create(*profile)
  29. if err != nil {
  30. log.Fatal(err)
  31. }
  32. pprof.StartCPUProfile(f)
  33. }
  34. if *netprofile {
  35. go func() {
  36. log.Println(http.ListenAndServe("localhost:8667", nil))
  37. }()
  38. log.Println("serving profile info at http://localhost:8667/debug/pprof/")
  39. }
  40. conf, err := botserv.LoadConfig(*config)
  41. if err != nil {
  42. log.Fatal(err)
  43. }
  44. delta = (float32(conf.Tick) / 1000.0) * float32(conf.Timescale)
  45. sm := http.NewServeMux()
  46. c := botserv.Controller{
  47. Idg: botserv.NewIdGenerator(),
  48. Conf: conf,
  49. Games: botserv.MapLock{
  50. M: make(map[string]*botserv.Game),
  51. },
  52. Memprofile: *mprofile,
  53. Profile: *profile,
  54. }
  55. sm.Handle("/", botserv.JsonHandler(c.Index))
  56. sm.Handle("/ws/", websocket.Handler(c.AddPlayer))
  57. sm.Handle("/game/start/", botserv.JsonHandler(c.StartGame))
  58. sm.Handle("/game/list/", botserv.JsonHandler(c.ListGames))
  59. sm.Handle("/game/stats/", botserv.JsonHandler(c.GameStats))
  60. sm.Handle("/game/bw/", botserv.JsonHandler(c.BW))
  61. sm.Handle("/game/stop/", botserv.JsonHandler(c.StopGame))
  62. sm.HandleFunc("/fuck/shit/up/", c.KillServer)
  63. err = http.ListenAndServe(*addr, sm)
  64. if err != nil {
  65. log.Fatal("unable to start server")
  66. }
  67. }