diff --git a/server/current.go b/server/current.go index 4aa7734..86f6a14 100644 --- a/server/current.go +++ b/server/current.go @@ -6,7 +6,6 @@ import ( "io/ioutil" "log" "net/http" - "os" pb "codeberg.org/andcscott/OpenWeatherMap-gRPC-API/proto" ) @@ -17,9 +16,9 @@ func (s *Server) Current(ctx context.Context, in *pb.RequestCurrent) (*pb.SendCu log.Println("'Current' function called...") url := "https://pro.openweathermap.org/data/2.5/weather?" - lat, lon := getLocation(in.City) + lat, lon := getLocation(in.City, s.ApiKey) units := "&units=imperial" - token := "&appid=" + os.Getenv("API_KEY") + token := "&appid=" + s.ApiKey url = url + fmt.Sprintf("lat=%f", lat) + fmt.Sprintf("&lon=%f", lon) + units + token @@ -34,7 +33,5 @@ func (s *Server) Current(ctx context.Context, in *pb.RequestCurrent) (*pb.SendCu log.Printf("Error reading weather response: %v", err) } - return &pb.SendCurrent{ - Payload: string(body), - }, nil + return &pb.SendCurrent{Payload: string(body)}, nil } diff --git a/server/extended.go b/server/extended.go index c9ae4dd..e9a28ac 100644 --- a/server/extended.go +++ b/server/extended.go @@ -6,7 +6,6 @@ import ( "io/ioutil" "log" "net/http" - "os" pb "codeberg.org/andcscott/OpenWeatherMap-gRPC-API/proto" ) @@ -17,10 +16,10 @@ func (s *Server) Extended(ctx context.Context, in *pb.RequestExtended) (*pb.Send log.Println("'Extended' function called...") url := "https://api.openweathermap.org/data/2.5/forecast/daily?" - lat, lon := getLocation(in.City) + lat, lon := getLocation(in.City, s.ApiKey) days := "&cnt=" + fmt.Sprint(in.Days) units := "&units=imperial" - token := "&appid=" + os.Getenv("API_KEY") + token := "&appid=" + s.ApiKey url = url + fmt.Sprintf("lat=%f", lat) + fmt.Sprintf("&lon=%f", lon) + units + days + token diff --git a/server/location.go b/server/location.go index 468546e..47985fa 100644 --- a/server/location.go +++ b/server/location.go @@ -6,7 +6,6 @@ import ( "io/ioutil" "log" "net/http" - "os" pb "codeberg.org/andcscott/OpenWeatherMap-gRPC-API/proto" ) @@ -23,7 +22,7 @@ func (s *Server) Location(ctx context.Context, in *pb.RequestLocation) (*pb.Send url := "http://api.openweathermap.org/geo/1.0/direct?q=" city := in.City - token := "&appid=" + os.Getenv("API_KEY") + token := "&appid=" + s.ApiKey url = url + city + token @@ -53,10 +52,10 @@ func (s *Server) Location(ctx context.Context, in *pb.RequestLocation) (*pb.Send // Used internally to fetch precise locations for Current and Extended // Receives gRPC requests (interface) and the location (string) // Returns the latitude (float32) and longitude (float32) for a given location -func getLocation(city string) (float32, float32) { +func getLocation(city string, key string) (float32, float32) { url := "http://api.openweathermap.org/geo/1.0/direct?q=" - token := "&appid=" + os.Getenv("API_KEY") + token := "&appid=" + key url = url + city + token diff --git a/server/main.go b/server/main.go index 7bcfbc1..6096f78 100644 --- a/server/main.go +++ b/server/main.go @@ -14,6 +14,7 @@ import ( type Server struct { pb.WeatherServiceServer + ApiKey string } func main() { @@ -37,7 +38,7 @@ func main() { // Initialize gRPC server s := grpc.NewServer() - pb.RegisterWeatherServiceServer(s, &Server{}) + pb.RegisterWeatherServiceServer(s, &Server{ApiKey: os.Getenv("API_KEY")}) if err = s.Serve(lis); err != nil { log.Fatalf("gRPC Server error: %v\n", err) }