feat(proj): init
This commit is contained in:
@@ -0,0 +1,58 @@
|
||||
import 'package:please_pay_me/core/config/env_file.dart';
|
||||
|
||||
/// Runtime configuration.
|
||||
///
|
||||
/// Values are resolved in this order:
|
||||
/// 1. `--dart-define=PPM_*` / `--dart-define-from-file=.env` (CI and `run.ps1`)
|
||||
/// 2. key/value map parsed from `mobile/.env` (tests and explicit loaders)
|
||||
/// 3. production cabinet if nothing is set
|
||||
class AppConfig {
|
||||
static const productionOrigin = 'https://please-pay-me.ru';
|
||||
|
||||
const AppConfig({
|
||||
required this.apiBaseUrl,
|
||||
required this.webCabinetUrl,
|
||||
this.demoMode = false,
|
||||
});
|
||||
|
||||
factory AppConfig.fromEnvironment({Map<String, String> file = const {}}) {
|
||||
const definedApi = String.fromEnvironment('PPM_API_BASE_URL');
|
||||
const definedWeb = String.fromEnvironment('PPM_WEB_URL');
|
||||
const definedDemo = String.fromEnvironment('PPM_DEMO');
|
||||
|
||||
return AppConfig.fromMap({
|
||||
...file,
|
||||
if (definedApi.isNotEmpty) 'PPM_API_BASE_URL': definedApi,
|
||||
if (definedWeb.isNotEmpty) 'PPM_WEB_URL': definedWeb,
|
||||
if (definedDemo.isNotEmpty) 'PPM_DEMO': definedDemo,
|
||||
});
|
||||
}
|
||||
|
||||
factory AppConfig.fromMap(Map<String, String> values) {
|
||||
final apiBase = normalizeUrl(values['PPM_API_BASE_URL'] ?? '');
|
||||
final webUrl = normalizeUrl(values['PPM_WEB_URL'] ?? '');
|
||||
final demoForced = parseEnvFlag(values['PPM_DEMO']);
|
||||
final resolvedApi = apiBase.isEmpty ? productionOrigin : apiBase;
|
||||
final resolvedWeb = webUrl.isEmpty ? resolvedApi : webUrl;
|
||||
|
||||
return AppConfig(
|
||||
apiBaseUrl: resolvedApi,
|
||||
webCabinetUrl: resolvedWeb,
|
||||
demoMode: demoForced,
|
||||
);
|
||||
}
|
||||
|
||||
static const demo = AppConfig(apiBaseUrl: '', webCabinetUrl: '', demoMode: true);
|
||||
|
||||
final String apiBaseUrl;
|
||||
final String webCabinetUrl;
|
||||
final bool demoMode;
|
||||
|
||||
AppConfig copyWith({String? apiBaseUrl, String? webCabinetUrl, bool? demoMode}) {
|
||||
return AppConfig(
|
||||
apiBaseUrl: apiBaseUrl ?? this.apiBaseUrl,
|
||||
webCabinetUrl: webCabinetUrl ?? this.webCabinetUrl,
|
||||
demoMode: demoMode ?? this.demoMode,
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user