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.

77 lines
1.9 KiB

  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. "code.google.com/p/go.net/websocket"
  12. )
  13. var addr = flag.String("addr", ":8666", "http service address")
  14. var profile = flag.String("pprof", "", "if specified will run with pprof")
  15. var mprofile = flag.String("mprof", "", "if specified will dump a memory profile")
  16. var netprofile = flag.Bool("netprof", false, "if specified will run with net/http/pprof on :8667")
  17. var verbose = flag.Bool("verbose", false, "")
  18. var config = flag.String("config", "~/.config/hackerbots/config.json", "location of config file")
  19. var delta float32
  20. var idg *IdGenerator
  21. var conf Config
  22. // This is the main, global collection of games
  23. var games MapLock
  24. func main() {
  25. log.SetFlags(log.Ldate | log.Ltime | log.Lshortfile)
  26. log.Printf("Starting Server...")
  27. var err error
  28. rand.Seed(time.Now().UnixNano())
  29. flag.Parse()
  30. if *profile != "" {
  31. f, err := os.Create(*profile)
  32. if err != nil {
  33. log.Fatal(err)
  34. }
  35. pprof.StartCPUProfile(f)
  36. }
  37. if *netprofile {
  38. go func() {
  39. log.Println(http.ListenAndServe("localhost:8667", nil))
  40. }()
  41. log.Println("serving profile info at http://localhost:8667/debug/pprof/")
  42. }
  43. games = MapLock{m: make(map[string]*game)}
  44. idg = NewIdGenerator()
  45. conf, err = loadConfig(*config)
  46. if err != nil {
  47. log.Fatal(err)
  48. }
  49. delta = (float32(conf.Tick) / 1000.0) * float32(conf.Timescale)
  50. sm := http.NewServeMux()
  51. sm.Handle("/", JsonHandler(index))
  52. sm.Handle("/ws/", websocket.Handler(addPlayer))
  53. sm.Handle("/game/start/", JsonHandler(startGame))
  54. sm.Handle("/game/list/", JsonHandler(listGames))
  55. sm.Handle("/game/stats/", JsonHandler(gameStats))
  56. sm.Handle("/game/bw/", JsonHandler(bw))
  57. sm.Handle("/game/stop/", JsonHandler(stopGame))
  58. sm.HandleFunc("/fuck/shit/up/", killServer)
  59. err = http.ListenAndServe(*addr, sm)
  60. if err != nil {
  61. log.Fatal("unable to start server")
  62. }
  63. }