2022-07-26 01:07:04 -04:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"net"
|
|
|
|
"os"
|
|
|
|
"strconv"
|
|
|
|
|
|
|
|
pb "codeberg.org/andcscott/OpenWeatherMap-gRPC-API/proto"
|
|
|
|
"github.com/joho/godotenv"
|
|
|
|
"google.golang.org/grpc"
|
|
|
|
)
|
|
|
|
|
2022-07-26 03:52:33 -04:00
|
|
|
type Server struct {
|
2022-07-26 01:07:04 -04:00
|
|
|
pb.WeatherServiceServer
|
2022-07-27 02:42:40 -04:00
|
|
|
ApiKey string
|
2022-07-26 01:07:04 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
|
2022-07-26 19:42:25 -04:00
|
|
|
// Load .env file
|
2022-07-26 01:07:04 -04:00
|
|
|
err := godotenv.Load()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalln(err)
|
|
|
|
}
|
|
|
|
|
2022-07-26 19:42:25 -04:00
|
|
|
// Read PORT from .env
|
2022-07-26 01:07:04 -04:00
|
|
|
port, _ := strconv.Atoi(os.Getenv("PORT"))
|
|
|
|
|
2022-07-26 19:42:25 -04:00
|
|
|
// Start server
|
2022-07-26 01:07:04 -04:00
|
|
|
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
|
2022-07-26 01:07:04 -04:00
|
|
|
s := grpc.NewServer()
|
2022-07-27 02:42:40 -04:00
|
|
|
pb.RegisterWeatherServiceServer(s, &Server{ApiKey: os.Getenv("API_KEY")})
|
2022-07-26 01:07:04 -04:00
|
|
|
if err = s.Serve(lis); err != nil {
|
|
|
|
log.Fatalf("gRPC Server error: %v\n", err)
|
|
|
|
}
|
|
|
|
}
|