82 lines
2.7 KiB
Dart
82 lines
2.7 KiB
Dart
import 'dart:convert';
|
||
|
||
import 'package:shared_preferences/shared_preferences.dart';
|
||
|
||
import 'server_configuration.dart';
|
||
|
||
class EnvConfig {
|
||
static const String environment = String.fromEnvironment(
|
||
'ENV',
|
||
defaultValue: 'test',
|
||
);
|
||
|
||
static String get sentryDsn {
|
||
// 根据环境切换 DSN(去 Sentry 后台创建两个项目:Test 和 Prod)
|
||
if (environment == 'prod') {
|
||
return 'https://d5fdda78335793935593efa3f4ed11bb@o4510984107261952.ingest.de.sentry.io/4510984527937616';
|
||
}
|
||
return 'https://1a72051f8ac6c21cde96ded18a318217@o4510984107261952.ingest.de.sentry.io/4510984468824144';
|
||
}
|
||
|
||
static const bool isProduction = environment == 'prod';
|
||
|
||
static const _serverConfigurationKey = 'server_configuration_$environment';
|
||
static ServerConfiguration _configuration = ServerConfiguration(
|
||
production: isProduction,
|
||
);
|
||
|
||
static ServerConfiguration get serverConfiguration => _configuration;
|
||
|
||
static void loadServerConfiguration(SharedPreferences preferences) {
|
||
try {
|
||
final saved = preferences.getString(_serverConfigurationKey);
|
||
if (saved != null) {
|
||
_configuration = ServerConfiguration.fromJson(
|
||
jsonDecode(saved) as Map<String, dynamic>,
|
||
production: isProduction,
|
||
);
|
||
return;
|
||
}
|
||
} on FormatException {
|
||
// Malformed preferences fall back to the build's endpoints.
|
||
} on TypeError {
|
||
// Older or invalid preference values may have a different type.
|
||
}
|
||
_configuration = ServerConfiguration(production: isProduction);
|
||
}
|
||
|
||
static Future<void> saveServerConfiguration(
|
||
SharedPreferences preferences,
|
||
ServerConfiguration configuration,
|
||
) async {
|
||
final saved = await preferences.setString(
|
||
_serverConfigurationKey,
|
||
jsonEncode(configuration.toJson()),
|
||
);
|
||
if (!saved) throw StateError('Unable to save server configuration');
|
||
_configuration = configuration;
|
||
}
|
||
|
||
static Uri address(ServerService service) => _configuration.address(service);
|
||
|
||
static String baseAddress(ServerService service) =>
|
||
address(service).toString().replaceFirst(RegExp(r'/+$'), '');
|
||
|
||
static String get tcpIp => address(ServerService.tcp).host;
|
||
static int get tcpPort => address(ServerService.tcp).port;
|
||
static String get baseUrl => baseAddress(ServerService.http);
|
||
|
||
static String get webRtcStreamAuthority =>
|
||
Uri(scheme: 'webrtc', host: address(ServerService.webRtc).host).authority;
|
||
|
||
static Uri webRtcPlayAddress(Uri stream) {
|
||
final configured = address(ServerService.webRtc);
|
||
return stream.host == configured.host
|
||
? configured
|
||
: ServerService.webRtc.defaultAddress(
|
||
production: isProduction,
|
||
commonHost: stream.host,
|
||
);
|
||
}
|
||
}
|