| | | 1 | | using DotNetEnv; |
| | | 2 | | using Microsoft.Extensions.Logging; |
| | | 3 | | using System.Text.Json; |
| | | 4 | | |
| | | 5 | | namespace Orchestrator; |
| | | 6 | | |
| | | 7 | | public static class EnvironmentHelper |
| | | 8 | | { |
| | | 9 | | public static void LoadEnvironmentVariables(ILogger logger) |
| | | 10 | | { |
| | | 11 | | try |
| | | 12 | | { |
| | | 13 | | // Use PathUtility to get the correct .env file path |
| | 0 | 14 | | var envPath = PathUtility.GetEnvFilePath("Orchestrator"); |
| | | 15 | | |
| | 0 | 16 | | if (File.Exists(envPath)) |
| | | 17 | | { |
| | 0 | 18 | | Env.Load(envPath); |
| | 0 | 19 | | logger.LogInformation("Loaded .env file from: {EnvPath}", envPath); |
| | | 20 | | } |
| | | 21 | | else |
| | | 22 | | { |
| | 0 | 23 | | logger.LogWarning("No .env file found at: {EnvPath}", envPath); |
| | 0 | 24 | | logger.LogInformation("Please create a .env file in the secrets directory based on .env.example"); |
| | 0 | 25 | | logger.LogInformation("Alternatively, set environment variables directly"); |
| | | 26 | | } |
| | | 27 | | |
| | | 28 | | // Load Firebase credentials if available |
| | 0 | 29 | | LoadFirebaseCredentials(logger); |
| | 0 | 30 | | } |
| | 0 | 31 | | catch (Exception ex) |
| | | 32 | | { |
| | 0 | 33 | | logger.LogWarning(ex, "Could not load .env file: {Message}", ex.Message); |
| | 0 | 34 | | } |
| | 0 | 35 | | } |
| | | 36 | | |
| | | 37 | | private static void LoadFirebaseCredentials(ILogger logger) |
| | | 38 | | { |
| | | 39 | | try |
| | | 40 | | { |
| | | 41 | | // Check if Firebase credentials are already set via environment variables |
| | 0 | 42 | | var existingFirebaseJson = Environment.GetEnvironmentVariable("FIREBASE_SERVICE_ACCOUNT_JSON"); |
| | 0 | 43 | | var existingProjectId = Environment.GetEnvironmentVariable("FIREBASE_PROJECT_ID"); |
| | | 44 | | |
| | 0 | 45 | | if (!string.IsNullOrEmpty(existingFirebaseJson) && !string.IsNullOrEmpty(existingProjectId)) |
| | | 46 | | { |
| | 0 | 47 | | logger.LogInformation("Firebase credentials already set via environment variables"); |
| | 0 | 48 | | return; |
| | | 49 | | } |
| | | 50 | | |
| | | 51 | | // Try to load from firebase.json file |
| | 0 | 52 | | var firebaseJsonPath = PathUtility.GetFirebaseJsonPath(); |
| | | 53 | | |
| | 0 | 54 | | if (File.Exists(firebaseJsonPath)) |
| | | 55 | | { |
| | 0 | 56 | | var firebaseJson = File.ReadAllText(firebaseJsonPath); |
| | | 57 | | |
| | | 58 | | // Parse the JSON to extract project_id |
| | | 59 | | try |
| | | 60 | | { |
| | 0 | 61 | | using var document = JsonDocument.Parse(firebaseJson); |
| | 0 | 62 | | var root = document.RootElement; |
| | | 63 | | |
| | 0 | 64 | | if (root.TryGetProperty("project_id", out var projectIdElement)) |
| | | 65 | | { |
| | 0 | 66 | | var projectId = projectIdElement.GetString(); |
| | | 67 | | |
| | 0 | 68 | | if (!string.IsNullOrEmpty(projectId)) |
| | | 69 | | { |
| | | 70 | | // Set both environment variables |
| | 0 | 71 | | Environment.SetEnvironmentVariable("FIREBASE_SERVICE_ACCOUNT_JSON", firebaseJson); |
| | 0 | 72 | | Environment.SetEnvironmentVariable("FIREBASE_PROJECT_ID", projectId); |
| | | 73 | | |
| | 0 | 74 | | logger.LogInformation("Loaded Firebase credentials from: {FirebasePath}", firebaseJsonPath); |
| | 0 | 75 | | logger.LogInformation("Firebase project ID: {ProjectId}", projectId); |
| | | 76 | | } |
| | | 77 | | else |
| | | 78 | | { |
| | 0 | 79 | | logger.LogWarning("Firebase JSON file is missing or has empty project_id field"); |
| | | 80 | | } |
| | | 81 | | } |
| | | 82 | | else |
| | | 83 | | { |
| | 0 | 84 | | logger.LogWarning("Firebase JSON file is missing project_id field"); |
| | | 85 | | } |
| | 0 | 86 | | } |
| | 0 | 87 | | catch (JsonException ex) |
| | | 88 | | { |
| | 0 | 89 | | logger.LogError(ex, "Failed to parse Firebase JSON file: {Message}", ex.Message); |
| | 0 | 90 | | } |
| | | 91 | | } |
| | | 92 | | else |
| | | 93 | | { |
| | 0 | 94 | | logger.LogInformation("No Firebase credentials file found at: {FirebasePath}", firebaseJsonPath); |
| | 0 | 95 | | logger.LogInformation("Firebase integration will be disabled unless FIREBASE_PROJECT_ID and FIREBASE_SER |
| | | 96 | | } |
| | 0 | 97 | | } |
| | 0 | 98 | | catch (Exception ex) |
| | | 99 | | { |
| | 0 | 100 | | logger.LogWarning(ex, "Could not load Firebase credentials: {Message}", ex.Message); |
| | 0 | 101 | | } |
| | 0 | 102 | | } |
| | | 103 | | } |