server/main.go

58 lines
1.3 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"
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")
var tick = flag.Int("tick", 60, "")
2013-08-19 20:43:26 -07:00
var verbose = flag.Bool("verbose", false, "")
var width = flag.Float64("width", 800, "width of field")
var height = flag.Float64("height", 550, "height of field")
2013-09-28 23:00:29 -07:00
var profile = flag.String("pprof", "", "if specified will run with pprof")
var debug = flag.Bool("debug", false, "automatically create games if they don't exist")
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
// 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-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)
}
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()
delta = float32(*tick) / 1000
2013-08-19 20:43:26 -07:00
http.Handle("/ws/", websocket.Handler(addPlayer))
2013-08-28 23:46:12 -07:00
http.Handle("/game/start/", JsonHandler(startGame))
2013-09-01 22:04:10 -07:00
http.Handle("/game/list/", JsonHandler(listGames))
http.HandleFunc("/game/stop/", stopGame)
2013-09-28 23:00:29 -07:00
http.HandleFunc("/fuck/shit/up/", killServer)
2013-08-19 20:43:26 -07:00
err := http.ListenAndServe(*addr, nil)
if err != nil {
log.Fatal("unable to start server")
}
}