-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfifa.go
177 lines (160 loc) · 4.29 KB
/
fifa.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
package main
import (
"encoding/json"
"fmt"
"html/template"
"io/ioutil"
"net/http"
"os"
"sort"
"strings"
"sync"
"time"
)
var wg sync.WaitGroup
var matches []Matches
type Matches struct {
Venue string `json:"venue"`
Location string `json:"location"`
Datetiming string `json:"datetime"`
Status string `json:"status"`
HomeTeam HomeTeam `json:"home_team"`
AwayTeam AwayTeam `json:"away_team"`
Winner string `json:"winner"`
WinnerCode string `json:"winner_code"`
}
type HomeTeam struct {
Country string `json:"country"`
Code string `json:"code"`
Goals int `json:"goals"`
}
type AwayTeam struct {
Country string `json:"country"`
Code string `json:"code"`
Goals int `json:"goals"`
}
type AllTeams struct {
ID int `json:"id"`
Country string `json:"country"`
FifaCode string `json:"fifa_code"`
GroupID int `json:"group_id"`
GroupLetter string `json:"group_letter"`
}
type Stats struct {
ID int `json:"id"`
GamesPlayed int `json:"games_played"`
Wins int `json:"wins"`
Losses int `json:"losses"`
Draws int `json:"draws"`
Points int `json:"points"`
}
type AllMatchesPage struct {
Title string
Match []Matches
}
type AllTeamsPage struct {
Title string
Teams map[AllTeams]Stats
}
type HomePage struct {
Title string
Links map[string]string
}
func date() {
for i := range matches {
t, err := time.Parse(time.RFC3339, matches[i].Datetiming)
if err != nil {
fmt.Println(err)
}
timestr := t.String()
pidx := strings.Index(timestr, "+")
uidx := strings.Index(timestr, "U")
matches[i].Datetiming = timestr[:pidx] + timestr[uidx:]
}
}
func allmatches(c chan []Matches) {
defer wg.Done()
resp, err := http.Get("https://world-cup-json.herokuapp.com/matches")
if err != nil {
fmt.Println("No json for you")
}
defer resp.Body.Close()
bytes, _ := ioutil.ReadAll(resp.Body)
json.Unmarshal(bytes, &matches)
c <- matches
}
func fifaMatches(w http.ResponseWriter, r *http.Request) {
queue := make(chan []Matches, 50)
wg.Add(1)
go allmatches(queue)
wg.Wait()
close(queue)
date()
page := AllMatchesPage{Title: "FIFA WORLDCUP 2K18 MATCHES", Match: matches}
t, _ := template.ParseFiles("matches.html")
t.Execute(w, page)
}
func todayMatches(w http.ResponseWriter, r *http.Request) {
resp, err := http.Get("https://world-cup-json.herokuapp.com/matches/today")
if err != nil {
fmt.Println("No json for you")
}
defer resp.Body.Close()
bytes, _ := ioutil.ReadAll(resp.Body)
json.Unmarshal(bytes, &matches)
date()
page := AllMatchesPage{Title: "FIFA WORLDCUP 2K18 TODAY'S BATTLES", Match: matches}
t, _ := template.ParseFiles("matches.html")
t.Execute(w, page)
}
func allteams(w http.ResponseWriter, r *http.Request) {
resp, err := http.Get("https://world-cup-json.herokuapp.com/teams/")
if err != nil {
fmt.Println("No json for you")
}
defer resp.Body.Close()
bytes, _ := ioutil.ReadAll(resp.Body)
var teams []AllTeams
json.Unmarshal(bytes, &teams)
response, err := http.Get("https://world-cup-json.herokuapp.com/teams/results")
if err != nil {
fmt.Println("No json for you")
}
defer response.Body.Close()
bites, _ := ioutil.ReadAll(response.Body)
var stats []Stats
json.Unmarshal(bites, &stats)
sort.Slice(teams, func(i, j int) bool {
return teams[i].ID < teams[j].ID
})
sort.Slice(stats, func(i, j int) bool {
return stats[i].ID < stats[j].ID
})
teamdetails := make(map[AllTeams]Stats)
for i := 0; i < len(teams); i++ {
teamdetails[teams[i]] = stats[i]
}
page := AllTeamsPage{Title: "POINTS TABLE(GROUPWISE) FIFA WORLDCUP 2K18", Teams: teamdetails}
t, _ := template.ParseFiles("teams.html")
t.Execute(w, page)
}
func fifa(w http.ResponseWriter, r *http.Request) {
links := make(map[string]string)
links["/teams"] = "POINTS TABLE OF ALL THE TEAMS"
links["/matches"] = "LIST OF ALL THE MATCHES"
links["/matches/today"] = "LIST OF TODAY'S MATCHES"
page := HomePage{Title: "FIFA WORLDCUP 2K18", Links: links}
t, _ := template.ParseFiles("home.html")
t.Execute(w, page)
}
func main() {
server := http.Server{
Addr: ":" + os.Getenv("PORT"), //
}
http.HandleFunc("/", fifa)
http.HandleFunc("/teams", allteams)
http.HandleFunc("/matches", fifaMatches)
http.HandleFunc("/matches/today", todayMatches)
//http.ListenAndServe("localhost:8000", nil)
server.ListenAndServe()
}