forked from strider-/dreamer
/
dreamer.go
162 lines (138 loc) · 4.08 KB
/
dreamer.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package main
/*
JSON API service that returns the detailed win/loss records for the current fight card.
Acts either as a FastCGI listener (reverse proxy for Nginx or Apache), or a local webserver.
TODO:
-Maybe make the port a parameter?
-Create an Upstart conf instead of using nohup & detatching from the terminal
*/
import (
"code.google.com/p/gorest"
"flag"
"fmt"
"net"
"net/http"
"net/http/fcgi"
"sort"
"spicerack"
)
var (
fastcgi = flag.Bool("fcgi", false, "Run under FastCGI mode")
dbUser, dbPass, dbName string
illumEmail, illumPass string
theShiznit, statsUrl string
webClient *http.Client
)
func main() {
flag.Parse()
loadConfig()
gorest.RegisterService(new(DreamService))
var err error
webClient, err = spicerack.LogIntoSaltyBet(illumEmail, illumPass)
if err != nil {
fmt.Printf("Error logging into Salty Bet: %v\n", err)
}
if !*fastcgi {
fmt.Println("Running Locally")
static := []string{"index", "search", "ds.js", "s.js", "ta.css"}
for _, p := range static {
http.HandleFunc(fmt.Sprintf("/%s", p), staticPage)
}
http.Handle("/", gorest.Handle())
fmt.Println(http.ListenAndServe(":9000", nil))
} else {
fmt.Println("Running as FastCGI")
l, _ := net.Listen("tcp", ":9000")
fmt.Println(fcgi.Serve(l, gorest.Handle()))
}
}
func staticPage(w http.ResponseWriter, r *http.Request) {
file := r.URL.Path[1:]
if file == "index" || file == "search" {
file += ".html"
}
http.ServeFile(w, r, file)
}
func loadConfig() {
conf, _ := spicerack.GofigFromEnv("ME_CONF")
salty, _ := conf.Map("salty")
dbUser = salty["db_user"].(string)
dbName = salty["db_name"].(string)
dbPass = salty["db_pass"].(string)
illumEmail = salty["illum_email"].(string)
illumPass = salty["illum_pword"].(string)
theShiznit = salty["the_shiznit"].(string)
statsUrl = salty["ajax_stats"].(string)
}
type DreamService struct {
gorest.RestService `root:"/api" consumes:"application/json" produces:"application/json"`
getFighters gorest.EndPoint `method:"GET" path:"/a" output:"[]FighterInfo"`
getHistory gorest.EndPoint `method:"GET" path:"/h/{CharId:int}" output:"spicerack.History"`
getCurrentFight gorest.EndPoint `method:"GET" path:"/f" output:"FightData"`
}
type FightData struct {
History []spicerack.History
Stats spicerack.FighterStats
Alert string
}
type FighterInfo struct {
Cid int
Name string
}
type ByName []FighterInfo
func (f ByName) Len() int { return len(f) }
func (f ByName) Swap(i, j int) { f[i], f[j] = f[j], f[i] }
func (f ByName) Less(i, j int) bool { return f[i].Name < f[j].Name }
func (serv DreamService) GetFighters() (fighters []FighterInfo) {
db := spicerack.Db(dbUser, dbPass, dbName)
defer db.Close()
names, err := db.GetFighterNames()
fighters = make([]FighterInfo, 0, len(names))
if err != nil {
serv.ResponseBuilder().SetResponseCode(500)
} else {
serv.ResponseBuilder().SetResponseCode(200)
for k, v := range names {
fighters = append(fighters, FighterInfo{Cid: k, Name: v})
}
sort.Sort(ByName(fighters))
}
return
}
func (serv DreamService) GetHistory(CharId int) (h spicerack.History) {
db := spicerack.Db(dbUser, dbPass, dbName)
defer db.Close()
f, err := db.GetFighter(CharId)
if err != nil || f.Id == 0 {
serv.ResponseBuilder().SetResponseCode(404)
return
}
h = *db.GetHistory(f)
serv.ResponseBuilder().SetResponseCode(200)
return
}
func (serv DreamService) GetCurrentFight() FightData {
db := spicerack.Db(dbUser, dbPass, dbName)
defer db.Close()
fc, err := spicerack.GetSecretData(theShiznit)
if err != nil {
serv.ResponseBuilder().SetResponseCode(500)
return *new(FightData)
}
fs, err := spicerack.GetFighterStats(webClient, statsUrl)
if err != nil {
serv.ResponseBuilder().SetResponseCode(500)
return *new(FightData)
}
card := &FightData{
History: make([]spicerack.History, 2),
Stats: *fs,
}
red, _ := db.GetFighter(fc.RedName)
blue, _ := db.GetFighter(fc.BlueName)
card.History[0] = *db.GetHistory(red)
card.History[1] = *db.GetHistory(blue)
card.Alert = fc.Alert
serv.ResponseBuilder().SetResponseCode(200)
return *card
}