server/main.go

74 lines
1.6 KiB
Go
Raw Normal View History

2013-08-19 20:43:26 -07:00
package main
import (
"code.google.com/p/go.net/websocket"
"flag"
"log"
"math/rand"
"net/http"
_ "net/http/pprof"
2013-09-28 23:00:29 -07:00
"os"
"runtime/pprof"
2013-08-19 20:43:26 -07:00
"time"
)
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")
var netprofile = flag.Bool("netprof", false, "if specified will run with net/http/pprof")
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
var delta float32
2013-08-19 20:43:26 -07:00
2013-08-28 23:46:12 -07:00
var idg *IdGenerator
2013-11-13 20:38:57 -08:00
var conf Config
// This is the main, global collection of games
var games MapLock
2013-08-28 23:46:12 -07:00
2013-08-19 20:43:26 -07:00
func main() {
log.Printf("Starting Server...")
2013-11-13 20:38:57 -08:00
var err error
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)
}
if *netprofile {
go func() {
log.Println(http.ListenAndServe("localhost:8667", nil))
}()
}
2013-08-19 20:43:26 -07:00
games = MapLock{m: make(map[string]*game)}
2013-08-28 23:46:12 -07:00
idg = NewIdGenerator()
2013-11-13 20:38:57 -08:00
conf, err = loadConfig(*config)
if err != nil {
log.Fatal(err)
}
log.Printf("config: %+v", conf)
delta = (float32(conf.Tick) / 1000.0) * float32(conf.Timescale)
2013-08-19 20:43:26 -07:00
sm := http.NewServeMux()
2013-11-07 22:05:20 -08:00
sm.Handle("/", JsonHandler(index))
sm.Handle("/ws/", websocket.Handler(addPlayer))
sm.Handle("/game/start/", JsonHandler(startGame))
sm.Handle("/game/list/", JsonHandler(listGames))
2013-11-13 23:45:02 -08:00
sm.Handle("/game/stats/", JsonHandler(gameStats))
sm.Handle("/game/stop/", JsonHandler(stopGame))
sm.HandleFunc("/fuck/shit/up/", 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")
}
}