Example #1
0
func main() {
	runtime.GOMAXPROCS(runtime.NumCPU())

	configPath := flag.String("config", "server_config.json", "Path to server config")
	flag.Parse()
	config = sutil.LoadServerConfig(*configPath)

	dbc = db.NewDatabase(config.DbPass)
	if dbc == nil {
		log.Fatalln("Could not create Database object, which is required.")
	}
	mailer = util.NewMailer(config.MailPass)

	r := mux.NewRouter()
	r.HandleFunc("/confirm-email/{hashKey}", func(w http.ResponseWriter, r *http.Request) {
		handle(processors["/confirm-email"], w, r)
	})
	r.PathPrefix("/").HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		processor, ok := processors[r.URL.Path]
		if !ok {
			http.ServeFile(w, r, "."+r.URL.Path)
			return
		}

		handle(processor, w, r)
	})

	http.Handle("/", r)

	err := http.ListenAndServe(config.Host+":"+config.WwwPort, nil)
	if err != nil {
		log.Fatalln("ListenAndServe:", err)
	}
}
Example #2
0
func main() {
	runtime.GOMAXPROCS(runtime.NumCPU())

	// setupPrompt()
	setupSigInt() // Print newline on SIG_INT
	configPath := flag.String("config", "server_config.default.json", "Path to server config")
	flag.Parse()
	config = util.LoadServerConfig(*configPath)

	dbc = db.NewDatabase(config.DbPass)

	// Set up the world
	var world *game.World
	generator := maps.NewSimplexHills(time.Now().Unix())
	if config.PersistEnabled {
		log.Println("Running with persist ENABLED. Loading world from", config.WorldBaseDir)
		persister := persist.New(config.WorldBaseDir, generator)
		world = game.NewWorld(persister.MapGenerator())
		persister.ListenForChanges(world)
	} else {
		log.Println("Running with persist DISABLED. Changes to the world will not be saved.")
		world = game.NewWorld(generator)
	}
	globalGame = NewGame(world)

	go globalGame.Run()

	// Uncomment this to run a quick profile.
	// 	go doProfile()

	// Generate our protocol and make it available for clients to download
	protoJs := proto.GenerateJs()
	r := mux.NewRouter()
	r.HandleFunc("/js/proto.js", func(w http.ResponseWriter, r *http.Request) {
		headers := w.Header()
		headers["Content-Type"] = []string{"application/javascript; charset=utf-8"}
		fmt.Fprint(w, protoJs)
	})
	r.PathPrefix("/sockets/main/").HandlerFunc(mainSocketHandler)
	r.PathPrefix("/sockets/chunk/").HandlerFunc(chunkSocketHandler)
	r.PathPrefix("/").HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		handler(w, r, config.ClientAssets)
	})

	http.Handle("/", r)

	addr := config.Host + ":" + config.ServerPort
	log.Println("Starting game server on", addr)
	err := http.ListenAndServe(addr, nil)
	if err != nil {
		log.Fatal("ListenAndServe:", err)
	}
}