1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
| package main
import ( "bytes" "encoding/json" "fmt" "io/ioutil" "log" "net/http" )
type weatherData struct { LocationName string `json: locationName` Weather string `json: weather` Temperature int `json: temperature` Celsius bool `json: celsius` TempForecast []int `json: temp_forecast` Wind windData `json: wind` }
type windData struct { Direction string `json: direction` Speed int `json: speed` }
type loc struct { Lat float32 `json: lat` Lon float32 `json: lon` }
func weatherHandler(w http.ResponseWriter, r *http.Request) { location := loc{}
log.Println(r.Method) jsn, err := ioutil.ReadAll(r.Body) if err != nil { log.Fatal("Error reading the body", err) }
err = json.Unmarshal(jsn, &location) if err != nil { log.Fatal("Decoding error: ", err) }
log.Printf("Received: %v\n", location) weather := weatherData{ LocationName: "Zzyzx", Weather: "cloudy", Temperature: 31, Celsius: true, TempForecast: []int{30, 32, 29}, Wind: windData{ Direction: "S", Speed: 20, }, }
weatherJson, err := json.Marshal(weather) if err != nil { fmt.Fprintf(w, "Error: %s", err) } w.Header().Set("Content-Type", "application/json") w.Write(weatherJson)
}
func server() { http.HandleFunc("/", weatherHandler) http.ListenAndServe(":8080", nil) }
func client() { locJson, err := json.Marshal(loc{Lat: 35.14326, Lon: -116.104}) req, err := http.NewRequest("POST", "http://localhost:8080", bytes.NewBuffer(locJson)) req.Header.Set("Content-Type", "application/json") client := &http.Client{} resp, err := client.Do(req) if err != nil { log.Fatal(err) } body, err := ioutil.ReadAll(resp.Body) fmt.Println("Response: ", string(body)) resp.Body.Close() }
func main() { go server() client() }
|