-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
75 lines (64 loc) · 1.57 KB
/
main.go
File metadata and controls
75 lines (64 loc) · 1.57 KB
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
package main
import (
"encoding/json"
"fmt"
"net/http"
"os"
)
func main() {
apiKey := os.Getenv("TOKEN")
userAgent := os.Getenv("USER_AGENT")
fmt.Println(apiKey)
fmt.Println(userAgent)
client := &mlbClient{
token: apiKey,
userAgent: userAgent,
}
client.Init()
s := server{
mlbClient: client,
}
fs := http.FileServer(http.Dir("./baseball_frontend/build/"))
http.Handle("/", fs)
http.HandleFunc("/CHRIS", s.chrisHandler)
fmt.Println(http.ListenAndServe(":8081", nil))
}
type server struct {
mlbClient *mlbClient
}
func setHeaders(w http.ResponseWriter) {
w.Header().Set("Content-Type", "application/json;charset=UTF-8")
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Set("Access-Control-Allow-Headers", "*")
w.WriteHeader(http.StatusOK)
}
func sendErr(w http.ResponseWriter, err error) {
respBody := responseBody{
Status: http.StatusInternalServerError,
Error: err.Error(),
}
encoder := json.NewEncoder(w)
if err := encoder.Encode(respBody); err != nil {
fmt.Println(err)
}
}
type responseBody struct {
Status int `json:"status"`
Error string `json:"error"`
Teams []*fantasyTeam `json:"teams"`
}
func (s *server) chrisHandler(w http.ResponseWriter, r *http.Request) {
setHeaders(w)
leagueStandings := getFantasyChris()
mlb := s.mlbClient.getMLBStandings()
populateScores(leagueStandings, mlb)
leagueStandings.Rank()
respBody := responseBody{
Status: http.StatusOK,
Teams: leagueStandings.Teams,
}
encoder := json.NewEncoder(w)
if err := encoder.Encode(respBody); err != nil {
fmt.Println(err)
}
}