Last active
June 13, 2020 09:13
-
-
Save robertotambunan/d522bcc668d152b8feb4722a09a8cca4 to your computer and use it in GitHub Desktop.
Revisions
-
robertotambunan renamed this gist
Jun 13, 2020 . 1 changed file with 2 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -21,8 +21,8 @@ func main() { // handling template call func templateWorldHandler(w http.ResponseWriter, r *http.Request) { var data AllData data.Nations = getWorldCoronaData() t := template.Must(template.ParseFiles("templates/index-world.html")) t.Execute(w, data) } -
robertotambunan created this gist
Jun 13, 2020 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,48 @@ import ( "html/template" "log" "net/http" "os" ) type AllData struct { Nations []AttributeNationData } func main() { port := os.Getenv("PORT") if port == "" { log.Fatal("$PORT must be set") } http.HandleFunc("/world", templateWorldHandler) log.Println("running in port :", port) http.ListenAndServe(":"+port, nil) } // handling template call func templateWorldHandler(w http.ResponseWriter, r *http.Request) { var data AllData data.Nations = getWorldCoronaData() t := template.Must(template.ParseFiles("templates/index-world.html")) t.Execute(w, data) } // fetch data func getWorldCoronaData() (result []AttributeNationData) { req, err := http.NewRequest("GET", dataURLNation, nil) if err != nil { log.Println("NewRequest: ", err) return } client := &http.Client{} resp, err := client.Do(req) if err != nil { log.Println("Do: ", err) return } defer resp.Body.Close() if err := json.NewDecoder(resp.Body).Decode(&result); err != nil { log.Println("NewDecoder", err) } return }