diff --git a/init.go b/init.go index c7589dd..be7abab 100644 --- a/init.go +++ b/init.go @@ -19,102 +19,77 @@ import ( "github.com/PretendoNetwork/mario-kart-7/globals" ) +const ( + envPostgresURI = "PN_MK7_POSTGRES_URI" + envKerberosPassword = "PN_MK7_KERBEROS_PASSWORD" + envAuthServerPort = "PN_MK7_AUTHENTICATION_SERVER_PORT" + envSecureServerHost = "PN_MK7_SECURE_SERVER_HOST" + envSecureServerPort = "PN_MK7_SECURE_SERVER_PORT" + envAccountGRPCHost = "PN_MK7_ACCOUNT_GRPC_HOST" + envAccountGRPCPort = "PN_MK7_ACCOUNT_GRPC_PORT" + envAccountGRPCAPIKey = "PN_MK7_ACCOUNT_GRPC_API_KEY" +) + func init() { globals.Logger = plogger.NewLogger() - var err error - - err = godotenv.Load() - if err != nil { + if err := godotenv.Load(); err != nil { globals.Logger.Warning("Error loading .env file") } - postgresURI := os.Getenv("PN_MK7_POSTGRES_URI") - kerberosPassword := os.Getenv("PN_MK7_KERBEROS_PASSWORD") - authenticationServerPort := os.Getenv("PN_MK7_AUTHENTICATION_SERVER_PORT") - secureServerHost := os.Getenv("PN_MK7_SECURE_SERVER_HOST") - secureServerPort := os.Getenv("PN_MK7_SECURE_SERVER_PORT") - accountGRPCHost := os.Getenv("PN_MK7_ACCOUNT_GRPC_HOST") - accountGRPCPort := os.Getenv("PN_MK7_ACCOUNT_GRPC_PORT") - accountGRPCAPIKey := os.Getenv("PN_MK7_ACCOUNT_GRPC_API_KEY") - - if strings.TrimSpace(postgresURI) == "" { - globals.Logger.Error("PN_MK7_POSTGRES_URI environment variable not set") - os.Exit(0) + // Chargez les variables d'environnement et les validez + postgresURI := getEnvOrExit(envPostgresURI, true) + kerberosPassword := os.Getenv(envKerberosPassword) + if kerberosPassword == "" { + globals.Logger.Warningf("%s not set. Using default password: %q", envKerberosPassword, globals.KerberosPassword) + kerberosPassword = globals.KerberosPassword } - if strings.TrimSpace(kerberosPassword) == "" { - globals.Logger.Warningf("PN_MK7_KERBEROS_PASSWORD environment variable not set. Using default password: %q", globals.KerberosPassword) - } else { - globals.KerberosPassword = kerberosPassword + authServerPort := validatePort(getEnvOrExit(envAuthServerPort, true), envAuthServerPort) + secureServerHost := getEnvOrExit(envSecureServerHost, true) + secureServerPort := validatePort(getEnvOrExit(envSecureServerPort, true), envSecureServerPort) + accountGRPCHost := getEnvOrExit(envAccountGRPCHost, true) + accountGRPCPort := validatePort(getEnvOrExit(envAccountGRPCPort, true), envAccountGRPCPort) + accountGRPCAPIKey := os.Getenv(envAccountGRPCAPIKey) + if accountGRPCAPIKey == "" { + globals.Logger.Warning("Insecure gRPC server detected. " + envAccountGRPCAPIKey + " environment variable not set") } + // Initialiser les comptes et les connexions gRPC + globals.KerberosPassword = kerberosPassword globals.AuthenticationServerAccount = nex.NewAccount(types.NewPID(1), "Quazal Authentication", globals.KerberosPassword) globals.SecureServerAccount = nex.NewAccount(types.NewPID(2), "Quazal Rendez-Vous", globals.KerberosPassword) - if strings.TrimSpace(authenticationServerPort) == "" { - globals.Logger.Error("PN_MK7_AUTHENTICATION_SERVER_PORT environment variable not set") - os.Exit(0) - } - - if port, err := strconv.Atoi(authenticationServerPort); err != nil { - globals.Logger.Errorf("PN_MK7_AUTHENTICATION_SERVER_PORT is not a valid port. Expected 0-65535, got %s", authenticationServerPort) - os.Exit(0) - } else if port < 0 || port > 65535 { - globals.Logger.Errorf("PN_MK7_AUTHENTICATION_SERVER_PORT is not a valid port. Expected 0-65535, got %s", authenticationServerPort) - os.Exit(0) - } - - if strings.TrimSpace(secureServerHost) == "" { - globals.Logger.Error("PN_MK7_SECURE_SERVER_HOST environment variable not set") - os.Exit(0) - } - - if strings.TrimSpace(secureServerPort) == "" { - globals.Logger.Error("PN_MK7_SECURE_SERVER_PORT environment variable not set") - os.Exit(0) - } - - if port, err := strconv.Atoi(secureServerPort); err != nil { - globals.Logger.Errorf("PN_MK7_SECURE_SERVER_PORT is not a valid port. Expected 0-65535, got %s", secureServerPort) - os.Exit(0) - } else if port < 0 || port > 65535 { - globals.Logger.Errorf("PN_MK7_SECURE_SERVER_PORT is not a valid port. Expected 0-65535, got %s", secureServerPort) - os.Exit(0) - } - - if strings.TrimSpace(accountGRPCHost) == "" { - globals.Logger.Error("PN_MK7_ACCOUNT_GRPC_HOST environment variable not set") - os.Exit(0) - } - - if strings.TrimSpace(accountGRPCPort) == "" { - globals.Logger.Error("PN_MK7_ACCOUNT_GRPC_PORT environment variable not set") - os.Exit(0) + // Connexion au serveur gRPC + conn, err := grpc.Dial(fmt.Sprintf("%s:%d", accountGRPCHost, accountGRPCPort), grpc.WithTransportCredentials(insecure.NewCredentials())) + if err != nil { + exitOnError(fmt.Sprintf("Failed to connect to account gRPC server: %v", err)) } + globals.GRPCAccountClientConnection = conn + globals.GRPCAccountClient = pb.NewAccountClient(conn) + globals.GRPCAccountCommonMetadata = metadata.Pairs("X-API-Key", accountGRPCAPIKey) - if port, err := strconv.Atoi(accountGRPCPort); err != nil { - globals.Logger.Errorf("PN_MK7_ACCOUNT_GRPC_PORT is not a valid port. Expected 0-65535, got %s", accountGRPCPort) - os.Exit(0) - } else if port < 0 || port > 65535 { - globals.Logger.Errorf("PN_MK7_ACCOUNT_GRPC_PORT is not a valid port. Expected 0-65535, got %s", accountGRPCPort) - os.Exit(0) - } + // Connexion à la base de données PostgreSQL + database.ConnectPostgres() +} - if strings.TrimSpace(accountGRPCAPIKey) == "" { - globals.Logger.Warning("Insecure gRPC server detected. PN_MK7_ACCOUNT_GRPC_API_KEY environment variable not set") +func getEnvOrExit(key string, required bool) string { + value := strings.TrimSpace(os.Getenv(key)) + if value == "" && required { + exitOnError(fmt.Sprintf("%s environment variable not set", key)) } + return value +} - globals.GRPCAccountClientConnection, err = grpc.Dial(fmt.Sprintf("%s:%s", accountGRPCHost, accountGRPCPort), grpc.WithTransportCredentials(insecure.NewCredentials())) - if err != nil { - globals.Logger.Criticalf("Failed to connect to account gRPC server: %v", err) - os.Exit(0) +func validatePort(portStr, envVarName string) int { + port, err := strconv.Atoi(portStr) + if err != nil || port < 0 || port > 65535 { + exitOnError(fmt.Sprintf("%s is not a valid port. Expected 0-65535, got %s", envVarName, portStr)) } + return port +} - globals.GRPCAccountClient = pb.NewAccountClient(globals.GRPCAccountClientConnection) - globals.GRPCAccountCommonMetadata = metadata.Pairs( - "X-API-Key", accountGRPCAPIKey, - ) - - database.ConnectPostgres() +func exitOnError(message string) { + globals.Logger.Critical(message) + os.Exit(1) }