~sircmpwn/cells

cells/main.go -rw-r--r-- 1.2 KiB View raw
99dbe4f3Drew DeVault Refactor chunk coords into separate func 2 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package main

import (
	"flag"
	"log"
	"net/http"

	"github.com/gorilla/websocket"
)

var addr = flag.String("addr", "localhost:8080", "http service address")

func socket(w http.ResponseWriter, r *http.Request) {
	upgrader := websocket.Upgrader{
		CheckOrigin: func(r *http.Request) bool { return true },
	}
	c, err := upgrader.Upgrade(w, r, nil)
	if err != nil {
		log.Print("upgrade:", err)
		return
	}
	log.Println("Client connected")
	defer c.Close()

	player := Player{
		Position: Position{0, 0},
		Color:    assignColor(),
	}
	conn := &Connection{
		Conn:   c,
		Player: player,
	}
	conn.Run()
}

func index(w http.ResponseWriter, r *http.Request) {
	if r.Method != "GET" {
		http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
		return
	}
	http.ServeFile(w, r, "index.html")
}

func indexjs(w http.ResponseWriter, r *http.Request) {
	if r.Method != "GET" {
		http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
		return
	}
	http.ServeFile(w, r, "index.js")
}

func main() {
	flag.Parse()
	log.SetFlags(0)
	http.HandleFunc("/socket", socket)
	http.HandleFunc("/", index)
	http.HandleFunc("/index.js", indexjs)
	log.Fatal(http.ListenAndServe(*addr, nil))
}