1+ import 'package:flutter/foundation.dart' ;
2+ import 'package:flutter_dotenv/flutter_dotenv.dart' ;
3+ import '../services/logger.dart' ;
4+
5+ /// Configuration for the BijbelQuiz Backend API Proxy
6+ ///
7+ /// This class manages the configuration for routing API requests through
8+ /// the secure backend proxy at backend.bijbelquiz.app
9+ class BackendConfig {
10+ static String ? _baseUrl;
11+ static String ? _authToken;
12+ static bool _initialized = false ;
13+ static bool _isExplicitlyConfigured = false ;
14+
15+ /// Default production backend URL
16+ static const String defaultBackendUrl = 'https://backend.bijbelquiz.app' ;
17+
18+ /// Backend base URL
19+ static String get baseUrl {
20+ if (_baseUrl != null ) return _baseUrl! ;
21+
22+ // Try to load from environment
23+ final envUrl = dotenv.env['BACKEND_URL' ];
24+ if (envUrl != null && envUrl.isNotEmpty) {
25+ return envUrl;
26+ }
27+
28+ // Default production URL
29+ return defaultBackendUrl;
30+ }
31+
32+ /// Check if backend is explicitly configured (not just using defaults)
33+ static bool get isConfigured => _isExplicitlyConfigured;
34+
35+ /// Gemini API endpoint
36+ static String get geminiEndpoint => '$baseUrl /api/gemini' ;
37+
38+ /// Supabase API endpoint
39+ static String get supabaseEndpoint => '$baseUrl /api/supabase' ;
40+
41+ /// PostHog API endpoint
42+ static String get posthogEndpoint => '$baseUrl /api/posthog' ;
43+
44+ /// Health check endpoint
45+ static String get healthEndpoint => '$baseUrl /api/health' ;
46+
47+ /// Initialize the backend configuration
48+ static Future <void > initialize () async {
49+ if (_initialized) return ;
50+
51+ AppLogger .info ('Initializing Backend configuration...' );
52+
53+ // Load from environment variables
54+ _baseUrl = dotenv.env['BACKEND_URL' ];
55+
56+ if (_baseUrl == null || _baseUrl! .isEmpty) {
57+ AppLogger .warning ('BACKEND_URL not set in environment, using default' );
58+ _baseUrl = defaultBackendUrl;
59+ } else {
60+ // Mark as explicitly configured when URL comes from environment
61+ _isExplicitlyConfigured = true ;
62+ }
63+
64+ AppLogger .info ('Backend URL configured: $_baseUrl ' );
65+ _initialized = true ;
66+ }
67+
68+ /// Reset configuration for testing purposes
69+ @visibleForTesting
70+ static void reset () {
71+ _baseUrl = null ;
72+ _authToken = null ;
73+ _initialized = false ;
74+ _isExplicitlyConfigured = false ;
75+ }
76+
77+ /// Set the authentication token for API requests
78+ static void setAuthToken (String ? token) {
79+ _authToken = token;
80+ AppLogger .info (token != null
81+ ? 'Auth token set'
82+ : 'Auth token cleared' );
83+ }
84+
85+ /// Get the current authentication token
86+ static Future <String ?> getAuthToken () async {
87+ // Return cached token if available
88+ if (_authToken != null ) return _authToken;
89+
90+ // Otherwise, try to get from secure storage or Supabase auth
91+ // This would typically integrate with your auth service
92+ return null ;
93+ }
94+
95+ /// Clear the authentication token
96+ static void clearAuthToken () {
97+ _authToken = null ;
98+ AppLogger .info ('Auth token cleared' );
99+ }
100+
101+ /// Check backend health
102+ ///
103+ /// Note: This method is not yet fully implemented. It currently throws
104+ /// [UnimplementedError] to indicate that a real health check should be
105+ /// implemented using an HTTP GET to the backend health endpoint.
106+ static Future <bool > checkHealth () async {
107+ throw UnimplementedError (
108+ 'Health check is not implemented. '
109+ 'Implement by making an HTTP GET request to $healthEndpoint '
110+ 'and returning true only on a successful 200 response.'
111+ );
112+ }
113+ }
0 commit comments