Created
September 10, 2017 11:07
-
-
Save hadv/9d02306d1cd8472e321330ec658928aa to your computer and use it in GitHub Desktop.
This file contains 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 characters
func main() { | |
mw := multiWeatherProvider{ | |
openWeatherMap{apiKey: "5bd6d7d469feee97788f51744f8c2910"}, | |
apixu{apiKey: "ff8b321075a54e7288794851162712"}, | |
} | |
http.HandleFunc("/weather/", func(w http.ResponseWriter, r *http.Request) { | |
begin := time.Now() | |
city := strings.SplitN(r.URL.Path, "/", 3)[2] | |
temp, err := mw.temperature(city) | |
if err != nil { | |
http.Error(w, err.Error(), http.StatusInternalServerError) | |
return | |
} | |
w.Header().Set("Content-Type", "application/json; charset=utf-8") | |
json.NewEncoder(w).Encode(map[string]interface{}{ | |
"city": city, | |
"temp": temp, | |
"took": time.Since(begin).String(), | |
}) | |
}) | |
http.ListenAndServe(":8080", nil) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment