Exemple #1
0
func (api *API) CityByCoords(w http.ResponseWriter, req *http.Request) {
	vars := mux.Vars(req)

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

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

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

	out, _ := json.Marshal(current_city)
	w.Write(out)
}
Exemple #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)
}
Exemple #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)
	}
}