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.

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