-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
47 lines (41 loc) · 981 Bytes
/
main.go
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
package main
import (
"fmt"
"log"
"net/http"
"os"
"github.com/Pallinder/go-randomdata"
"github.com/shawnmorreau/noshotv2-backend/pkg/noshotv2"
)
// func serveWs(game *noshotv2.Game, w http.ResponseWriter, r *http.Request) {
func serveWs(lobby *noshotv2.Lobby, w http.ResponseWriter, r *http.Request) {
fmt.Println("Endpoint hit")
conn, err := noshotv2.Upgrade(w, r)
if err != nil {
fmt.Fprintf(w, "%+v\n", err)
}
//I'd rather see the Human constructor be used here
player := &noshotv2.Human{
Conn: conn,
Lobby: lobby,
ID: randomdata.SillyName(),
}
lobby.AddPlayerToLobby <- player
player.Read()
}
func setupRoutes() {
lobby := noshotv2.NewLobby()
go lobby.Start()
http.HandleFunc("/ws", func(w http.ResponseWriter, r *http.Request) {
serveWs(lobby, w, r)
})
}
func main() {
setupRoutes()
port := os.Getenv("PORT")
if port == "" {
port = "5000"
}
fmt.Println("Starting on port " + port)
log.Fatal(http.ListenAndServe(":"+port, nil))
}