OpenWeather-gRPC-API/server/main.go

46 lines
823 B
Go
Raw Permalink Normal View History

package main
import (
"fmt"
"log"
"net"
"os"
"strconv"
pb "codeberg.org/andcscott/OpenWeatherMap-gRPC-API/proto"
"github.com/joho/godotenv"
"google.golang.org/grpc"
)
type Server struct {
pb.WeatherServiceServer
2022-07-27 02:42:40 -04:00
ApiKey string
}
func main() {
2022-07-26 19:42:25 -04:00
// Load .env file
err := godotenv.Load()
if err != nil {
log.Fatalln(err)
}
2022-07-26 19:42:25 -04:00
// Read PORT from .env
port, _ := strconv.Atoi(os.Getenv("PORT"))
2022-07-26 19:42:25 -04:00
// Start server
lis, err := net.Listen("tcp", fmt.Sprintf(":%d", port))
if err != nil {
log.Fatalf("Error starting server: %v\n", err)
} else {
log.Printf("Listening on port %d...\n", port)
}
2022-07-26 19:42:25 -04:00
// Initialize gRPC server
s := grpc.NewServer()
2022-07-27 02:42:40 -04:00
pb.RegisterWeatherServiceServer(s, &Server{ApiKey: os.Getenv("API_KEY")})
if err = s.Serve(lis); err != nil {
log.Fatalf("gRPC Server error: %v\n", err)
}
}