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"
"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
}

View file

@ -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

View file

@ -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

View file

@ -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)
}