Пример #1
0
func (api *API) WeatherByCity(w http.ResponseWriter, req *http.Request) {
	city_name := mux.Vars(req)["city"]

	log.Println("By Name:", city_name)

	current_city := <-mw.FindByName(city_name)
	current_weather := <-mw.GetWeather(current_city.Coords)

	if current_city.Error != nil {
		http.NotFound(w, req)
	} else {
		out := api.outputWeatherAsJSON(current_city, current_weather)
		w.Write(out)
	}
}
Пример #2
0
func (api *API) WeatherByCoords(w http.ResponseWriter, req *http.Request) {
	vars := mux.Vars(req)

	lat, _ := strconv.ParseFloat(vars["lat"], 64)
	lng, _ := strconv.ParseFloat(vars["lng"], 64)

	log.Println("By Coords:", lat, lng)

	coords := mw.Coordinates{lat, lng}
	current_city := mw.FindByCoords(coords)
	current_weather := <-mw.GetWeather(coords)

	out := api.outputWeatherAsJSON(<-current_city, current_weather)

	w.Write(out)
}
Пример #3
0
func (h *Homepage) weatherApp() {
	coords := h.getCoords()
	city := <-mw.FindByCoords(coords)
	weather := <-mw.GetWeather(city.Coords)

	h.cw = &CityWeather{City: city, Weather: weather}

	h.handleUnit()
	h.saveCityCache(city)

	t, _ := template.ParseFiles("./website/index.html")
	out, err := json.Marshal(h.cw)
	h.cw.JSON = string(out)
	h.cw.Weather.Temperature = math.Floor(h.cw.Weather.Temperature)
	err = t.Execute(h.w, h.cw)

	if err != nil {
		http.Error(h.w, err.Error(), http.StatusInternalServerError)
	}
}