Use ApiKey field in server struct

This commit is contained in:
Andrew Scott 2022-07-27 02:42:40 -04:00
parent 91119b44c2
commit 6a92b805e0
Signed by: a
GPG key ID: 3EB62D0BBB8DB381
4 changed files with 10 additions and 14 deletions

View file

@ -6,7 +6,6 @@ import (
"io/ioutil" "io/ioutil"
"log" "log"
"net/http" "net/http"
"os"
pb "codeberg.org/andcscott/OpenWeatherMap-gRPC-API/proto" 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...") log.Println("'Current' function called...")
url := "https://pro.openweathermap.org/data/2.5/weather?" url := "https://pro.openweathermap.org/data/2.5/weather?"
lat, lon := getLocation(in.City) lat, lon := getLocation(in.City, s.ApiKey)
units := "&units=imperial" 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 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) log.Printf("Error reading weather response: %v", err)
} }
return &pb.SendCurrent{ return &pb.SendCurrent{Payload: string(body)}, nil
Payload: string(body),
}, nil
} }

View file

@ -6,7 +6,6 @@ import (
"io/ioutil" "io/ioutil"
"log" "log"
"net/http" "net/http"
"os"
pb "codeberg.org/andcscott/OpenWeatherMap-gRPC-API/proto" 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...") log.Println("'Extended' function called...")
url := "https://api.openweathermap.org/data/2.5/forecast/daily?" 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) days := "&cnt=" + fmt.Sprint(in.Days)
units := "&units=imperial" 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 url = url + fmt.Sprintf("lat=%f", lat) + fmt.Sprintf("&lon=%f", lon) + units + days + token

View file

@ -6,7 +6,6 @@ import (
"io/ioutil" "io/ioutil"
"log" "log"
"net/http" "net/http"
"os"
pb "codeberg.org/andcscott/OpenWeatherMap-gRPC-API/proto" 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=" url := "http://api.openweathermap.org/geo/1.0/direct?q="
city := in.City city := in.City
token := "&appid=" + os.Getenv("API_KEY") token := "&appid=" + s.ApiKey
url = url + city + token 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 // Used internally to fetch precise locations for Current and Extended
// Receives gRPC requests (interface) and the location (string) // Receives gRPC requests (interface) and the location (string)
// Returns the latitude (float32) and longitude (float32) for a given location // 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=" url := "http://api.openweathermap.org/geo/1.0/direct?q="
token := "&appid=" + os.Getenv("API_KEY") token := "&appid=" + key
url = url + city + token url = url + city + token

View file

@ -14,6 +14,7 @@ import (
type Server struct { type Server struct {
pb.WeatherServiceServer pb.WeatherServiceServer
ApiKey string
} }
func main() { func main() {
@ -37,7 +38,7 @@ func main() {
// Initialize gRPC server // Initialize gRPC server
s := grpc.NewServer() s := grpc.NewServer()
pb.RegisterWeatherServiceServer(s, &Server{}) pb.RegisterWeatherServiceServer(s, &Server{ApiKey: os.Getenv("API_KEY")})
if err = s.Serve(lis); err != nil { if err = s.Serve(lis); err != nil {
log.Fatalf("gRPC Server error: %v\n", err) log.Fatalf("gRPC Server error: %v\n", err)
} }