package common import ( "encoding/json" "errors" "fmt" "github.com/IBM/sarama" "io" "log" "net/http" ) type YaHuRes struct { Rate float64 } func GetExchangeRate(nowCurrency, targetCurrency string) (float64, error) { res, err := http.Get("http://www.google.com?now=" + nowCurrency + "&targetCurrency=" + targetCurrency) if err != nil { return 0, err } body, err := io.ReadAll(res.Body) res.Body.Close() if res.StatusCode != 200 { log.Fatalf("Response failed with status code: %d and\nbody: %s\n", res.StatusCode, body) return 0, errors.New(fmt.Sprintf("Response failed with status code: %d and\nbody: %s\n", res.StatusCode, body)) } if err != nil { return 0, err } var yahuRes YaHuRes err = json.Unmarshal(body, &yahuRes) if err != nil { return 0, err } return yahuRes.Rate, err } func SendKafka(key string, info string) error { config := sarama.NewConfig() config.Producer.RequiredAcks = sarama.WaitForAll config.Producer.Retry.Max = 5 config.Producer.Return.Successes = true producer, err := sarama.NewSyncProducer([]string{"localhost:9092"}, config) if err != nil { fmt.Println("Failed to create producer:", err) return err } defer producer.Close() message := &sarama.ProducerMessage{ Topic: key, Value: sarama.StringEncoder(info), } _, _, err = producer.SendMessage(message) return err }