1
0
Fork 0
server/bserv/main.go

80 linhas
2.0 KiB
Go

package main
import (
"flag"
"log"
"math/rand"
"net/http"
_ "net/http/pprof"
"os"
"runtime/pprof"
"time"
"bitbucket.org/hackerbots/botserv"
"code.google.com/p/go.net/websocket"
)
var addr = flag.String("addr", ":8666", "http service address")
var profile = flag.String("pprof", "", "if specified will run with pprof")
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")
var verbose = flag.Bool("verbose", false, "")
var config = flag.String("config", "~/.config/hackerbots/config.json", "location of config file")
var delta float32
func main() {
log.SetFlags(log.Ldate | log.Ltime | log.Lshortfile)
log.Printf("Starting Server...")
var err error
rand.Seed(time.Now().UnixNano())
flag.Parse()
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))
}()
log.Println("serving profile info at http://localhost:8667/debug/pprof/")
}
conf, err := botserv.LoadConfig(*config)
if err != nil {
log.Fatal(err)
}
delta = (float32(conf.Tick) / 1000.0) * float32(conf.Timescale)
sm := http.NewServeMux()
c := botserv.Controller{
Idg: botserv.NewIdGenerator(),
Conf: conf,
Games: botserv.MapLock{
M: make(map[string]*botserv.Game),
},
Memprofile: *mprofile,
Profile: *profile,
}
sm.Handle("/", botserv.JsonHandler(c.Index))
sm.Handle("/ws/", websocket.Handler(c.AddPlayer))
sm.Handle("/game/start/", botserv.JsonHandler(c.StartGame))
sm.Handle("/game/list/", botserv.JsonHandler(c.ListGames))
sm.Handle("/game/stats/", botserv.JsonHandler(c.GameStats))
sm.Handle("/game/bw/", botserv.JsonHandler(c.BW))
sm.Handle("/game/stop/", botserv.JsonHandler(c.StopGame))
sm.HandleFunc("/fuck/shit/up/", c.KillServer)
err = http.ListenAndServe(*addr, sm)
if err != nil {
log.Fatal("unable to start server")
}
}