-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
75 lines (60 loc) · 1.48 KB
/
server.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
package main
import (
"fmt"
"github.com/gorilla/mux"
"html/template"
"log"
"net/http"
cs "github.com/ryanjb1/coronavirus_stats/coronavirusstats"
)
const (
LookupURL string = "https://www.worldometers.info/coronavirus/"
)
func PrintOverallData() {
fmt.Println("\nCoronavirus data:")
for _, overallItem := range cs.OverallData {
fmt.Printf("%s - %d\n", overallItem.Title, overallItem.Data)
}
}
func PrintCoronaData() {
fmt.Println("\nCountry\t| Total Cases\t| NewCases\t| TotalDeaths\t| NewDeaths\t| TotalRecovered\t| ActiveCases\t| Serious\t| TotalCasesOneMil\t|")
for _, item := range cs.CoronaData {
fmt.Printf("%s\t| %d\t| %d\t| %d\t| %d\t| %d\t| %d\t| %d\t| %d\t|\n",
item.Country,
item.TotalCases,
item.NewCases,
item.TotalDeaths,
item.NewDeaths,
item.TotalRecovered,
item.ActiveCases,
item.Serious,
item.TotalCasesOneMil,
)
}
}
type CoronaData struct {
PageTitle string
Data []cs.CoronaVirusStat
}
func startServer() {
r := mux.NewRouter()
tmpl := template.Must(template.ParseFiles("index.html"))
r.HandleFunc("/", func(writer http.ResponseWriter, request *http.Request) {
data := CoronaData{
PageTitle: "Corona Stats",
Data: cs.CoronaData,
}
tmpl.Execute(writer, data)
})
http.ListenAndServe(":8888", r)
}
func main() {
docs, err := cs.GetURLInfo(LookupURL)
if err != nil {
log.Println(err)
}
cs.GetOverallData(docs)
cs.GetCountriesData(docs)
fmt.Printf("Corona stats live on http://localhost:8888")
startServer()
}