-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
87 lines (78 loc) · 2.21 KB
/
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package main
import (
"fmt"
"flag"
"log"
"net/http"
"github.com/julienschmidt/httprouter"
"os/exec"
"github.com/rs/cors"
"strings"
"time"
)
var addr = flag.String("addr", ":6565", "http service address")
func createUuid() string {
// return UUID
out, err := exec.Command("uuidgen").Output()
if err != nil {
log.Fatal(err)
}
return strings.Replace(string(out[:]), "\n", "", -1)
}
func timeTrack(start time.Time, name string) {
elapsed := time.Since(start)
log.Printf("%s took %s", name, elapsed)
}
func main() {
flag.Parse()
var trumpTower *Tower
trumpTower = newTower()
// show draft creator page
router := httprouter.New()
// must POST to this route with rules, bidders and players
router.POST("/new-room", func(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
// hit gold league app to get FA, and owners
owners := fetchOwners()
players := fetchFreeAgents()
// only take 12 bidders
var bidders []*Bidder
// convert owners to bidders
for _, v := range owners {
b := ownerToBidder(v)
bidders = append(bidders, b)
}
//
roomId := newRoom(trumpTower, bidders, players)
// // return room id
w.Header().Set("Content-Type", "application/json")
fmt.Fprintf(w, `{"result":"%s"}`, roomId)
})
router.GET("/rooms/:roomNumber/connect", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
// see if draft room exists
log.Println("trying to connect....")
log.Println(ps.ByName("roomNumber"))
hub := trumpTower.rooms[ps.ByName("roomNumber")]
if hub != nil {
serveWs(hub, w, r)
} else {
log.Println("draft room does not exist")
http.Error(w, "Draft Room does not exist", 401)
return
}
})
router.GET("/rooms", func(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
json := trumpTower.getRoomsJson()
w.Write(json)
})
router.GET("/ping", func(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
time.Sleep(time.Second)
response := Response{"PING", map[string]interface{}{"time": time.Now().UnixNano()}}
json := responseToJson(response)
w.Write(json)
})
handler := cors.Default().Handler(router)
err := http.ListenAndServe(*addr, handler)
if err != nil {
log.Fatal("ListenAndServe: ", err)
}
}