2026-11-21 14:26:18 +08:00
|
|
|
|
import 'dart:convert';
|
|
|
|
|
|
|
|
|
|
|
|
import 'package:shared_preferences/shared_preferences.dart';
|
|
|
|
|
|
|
|
|
|
|
|
import 'server_configuration.dart';
|
|
|
|
|
|
|
2026-01-21 13:27:55 +08:00
|
|
|
|
class EnvConfig {
|
|
|
|
|
|
static const String environment = String.fromEnvironment(
|
|
|
|
|
|
'ENV',
|
2026-10-12 14:21:09 +08:00
|
|
|
|
defaultValue: 'test',
|
2026-01-21 13:27:55 +08:00
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
static String get sentryDsn {
|
|
|
|
|
|
// 根据环境切换 DSN(去 Sentry 后台创建两个项目:Test 和 Prod)
|
|
|
|
|
|
if (environment == 'prod') {
|
2026-03-06 13:52:14 +08:00
|
|
|
|
return 'https://d5fdda78335793935593efa3f4ed11bb@o4510984107261952.ingest.de.sentry.io/4510984527937616';
|
2026-01-21 13:27:55 +08:00
|
|
|
|
}
|
2026-03-06 13:52:14 +08:00
|
|
|
|
return 'https://1a72051f8ac6c21cde96ded18a318217@o4510984107261952.ingest.de.sentry.io/4510984468824144';
|
2026-01-21 13:27:55 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-11-11 16:33:04 +08:00
|
|
|
|
static const bool isProduction = environment == 'prod';
|
2026-07-11 16:46:53 +08:00
|
|
|
|
|
2026-11-21 14:26:18 +08:00
|
|
|
|
static const _serverConfigurationKey = 'server_configuration_$environment';
|
|
|
|
|
|
static ServerConfiguration _configuration = ServerConfiguration(
|
|
|
|
|
|
production: isProduction,
|
|
|
|
|
|
);
|
2026-09-01 08:59:20 +08:00
|
|
|
|
|
2026-11-21 14:26:18 +08:00
|
|
|
|
static ServerConfiguration get serverConfiguration => _configuration;
|
2026-09-01 08:59:20 +08:00
|
|
|
|
|
2026-11-21 14:26:18 +08:00
|
|
|
|
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);
|
|
|
|
|
|
}
|
2026-09-14 12:35:20 +08:00
|
|
|
|
|
2026-11-21 14:26:18 +08:00
|
|
|
|
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,
|
|
|
|
|
|
);
|
|
|
|
|
|
}
|
2026-01-21 13:27:55 +08:00
|
|
|
|
}
|