Skip to content

Instantly share code, notes, and snippets.

@hadv
Created September 10, 2017 11:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hadv/9d02306d1cd8472e321330ec658928aa to your computer and use it in GitHub Desktop.
Save hadv/9d02306d1cd8472e321330ec658928aa to your computer and use it in GitHub Desktop.
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