Added grpc connection to SimplrWeather

This commit is contained in:
Andrew Scott 2022-08-02 22:16:38 -04:00
parent 5dd913e1ec
commit 84990100c6
Signed by: a
GPG key ID: 3EB62D0BBB8DB381

View file

@ -1,13 +1,13 @@
package main package main
import ( import (
"bufio"
"fmt"
"log" "log"
"os" "os"
"strings"
pb "codeberg.org/andcscott/weather-cli/proto"
"github.com/joho/godotenv" "github.com/joho/godotenv"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
) )
const version string = "0.2.0" const version string = "0.2.0"
@ -44,12 +44,19 @@ type application struct {
Forecast forecast Forecast forecast
Config config Config config
Version string Version string
Client pb.RouteGuideClient
} }
func main() { func main() {
conn, err := grpc.Dial("localhost:50051", grpc.WithTransportCredentials(insecure.NewCredentials()))
if err != nil {
log.Fatalln(err)
}
defer conn.Close()
// Load .env // Load .env
err := godotenv.Load() err = godotenv.Load()
if err != nil { if err != nil {
log.Fatalln(err) log.Fatalln(err)
} }
@ -66,48 +73,9 @@ func main() {
Config: cfg, Config: cfg,
Forecast: fcst, Forecast: fcst,
Version: version, Version: version,
Client: pb.NewRouteGuideClient(conn),
} }
// Display main menu mainMenu(&app)
fmt.Println("\n=====================================================")
fmt.Printf("| Welcome to the OpenWeatherMap-gRPC Client! v%s |\n", app.Version)
fmt.Println("=====================================================")
fmt.Printf("New in version %s:\n", app.Version)
fmt.Println(" - Advanced option: Get historical data back to 1972")
fmt.Println("New in version 0.1.0")
fmt.Println(" - Default option: Automatically determine location")
fmt.Println(" - Advanced option: Enter exact location for anywhere in the world")
fmt.Println(" - Advanced option: Change back and forth between imperial and metric units of measurement")
var option string
// Menu loop
for option != "0" {
fmt.Print("\nMain Menu\n---------\n\n")
fmt.Println("1. Today's forecast (use current location, default)")
fmt.Println("2. Advanced options (Change units, precise location, etc.)")
fmt.Print("0. Exit\n\n")
// Read user input
reader := bufio.NewReader(os.Stdin)
input, err := reader.ReadString('\n')
if err != nil {
log.Println(err)
}
option = strings.TrimSuffix(input, "\n")
// Check user input
if option == "1" || option == "" {
getLocation(&app)
getCurrent(&app)
printWeather(&app)
} else if option == "2" {
advancedMenu(&app)
} else if option == "0" {
return
} else {
fmt.Print("\nOops! An error occurred, please choose a valid option.\n\n")
}
}
} }