-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfifa.go
103 lines (91 loc) · 2.39 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
package main
import (
"encoding/json"
"fmt"
"html/template"
"io/ioutil"
"net/http"
"os"
"sync"
)
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 AllMatchesPage struct {
Title string
Match []Matches
}
type HomePage struct {
Title string
Links map[string]string
}
func allmatches(c chan []Matches) {
defer wg.Done()
resp, err := http.Get("http://worldcup.sfg.io/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)
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("http://worldcup.sfg.io/matches/today")
if err != nil {
fmt.Println("No json for you")
}
defer resp.Body.Close()
bytes, _ := ioutil.ReadAll(resp.Body)
json.Unmarshal(bytes, &matches)
page := AllMatchesPage{Title: "FIFA WORLDCUP 2K18 TODAY'S BATTLES", Match: matches}
t, _ := template.ParseFiles("matches.html")
t.Execute(w, page)
}
func fifa(w http.ResponseWriter, r *http.Request) {
links := make(map[string]string)
links["/matches"] = "FIFA WORLDCUP 2K18 ALL MACTHES"
links["/matches/today"] = "ALL MACTHES TO BE PLAYED TODAY"
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("/matches", fifaMatches)
http.HandleFunc("/matches/today", todayMatches)
//http.ListenAndServe("localhost:8000", nil)
server.ListenAndServe()
}