Skip to content

Nartikov/test

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 

Repository files navigation

package main

import (
	"encoding/json"
	"fmt"
	"io"
	"log"
	"net/http"
)

type Weather struct {
	Name string `json:"name"`
	Main Main   `json:"main"`
}
type Main struct {
	Temp float32 `json:"temp"`
}

func getWeather(cityName string) (string, error) {
	key := "d36ae2d09e02a52d8c957339d213d776"
	url := "https://api.openweathermap.org/data/2.5/weather?q=" + cityName + "&appid=" + key + "&lang=ru&units=metric"
	resp, err := http.Get(url)
	if err != nil {
		return "", err
	}
	defer resp.Body.Close()
	body, err := io.ReadAll(resp.Body)
	if err != nil {
		return "", err
	}
	var data Weather
	json.Unmarshal(body, &data)
	answer := data.Name + " -> " + fmt.Sprintf("%f", data.Main.Temp)
	return answer, nil
}

func main() {
	http.HandleFunc("/", HelloHandler)

	fmt.Println("Server started at port 8081")
	log.Fatal(http.ListenAndServe(":8083", nil))
	
}
func HelloHandler(w http.ResponseWriter, r *http.Request) {
	city, err := getWeather("Tokyo")
	if err != nil {
		log.Println(err.Error())
		return
	}
	fmt.Println(city)
	if r.Method == "POST" {
		r.ParseForm()
		// они все тут
		params := r.Form
		fmt.Println(w, params)
	}
	fmt.Fprintf(w, city)

}

Releases

No releases published

Packages

No packages published