-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
69 lines (56 loc) · 1.39 KB
/
main.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
package main
import (
"encoding/json"
"github.com/gorilla/mux"
"html/template"
"io/ioutil"
"log"
"net/http"
"time"
)
type Country struct {
Name string
Capital string
Region string
SubRegion string
Flag string
Alpha2Code string
Alpha3Code string
}
func main() {
r := mux.NewRouter()
r.HandleFunc("/", HomeHandler)
r.HandleFunc("/api/countries/americas", CountriesHandler)
http.Handle("/", r)
srv := &http.Server{
Handler: r,
Addr: "0.0.0.0:8087",
WriteTimeout: 15 * time.Second,
ReadTimeout: 15 * time.Second,
}
log.Fatal(srv.ListenAndServe())
}
func CitiesHandler(writer http.ResponseWriter, request *http.Request) {
}
func CountriesHandler(writer http.ResponseWriter, request *http.Request) {
_ = json.NewEncoder(writer).Encode(ListCountries());
}
func HomeHandler(writer http.ResponseWriter, request *http.Request) {
t := template.Must(template.ParseFiles("templates/index.html"))
if err := t.ExecuteTemplate(writer, "index.html", ListCountries()); err != nil {
http.Error(writer, err.Error(), http.StatusInternalServerError)
}
}
func ListCountries() []Country {
response, err := http.Get("https://restcountries.eu/rest/v2/region/americas")
checkError(err)
data, _ := ioutil.ReadAll(response.Body)
var countries []Country
_ = json.Unmarshal([]byte(data), &countries)
return countries
}
func checkError(err error) {
if err != nil {
panic(err)
}
}