init static method
Initializes this Config by applying values from the following sources (in the following order):
- compile-time environment variables;
- bundled configuration file (
conf.toml
); - default values.
Implementation
static Future<void> init() async {
WidgetsFlutterBinding.ensureInitialized();
final Map<String, dynamic> document =
TomlDocument.parse(
await rootBundle.loadString('assets/conf.toml'),
).toMap();
graphql =
const bool.hasEnvironment('SOCAPP_HTTP_GRAPHQL')
? const String.fromEnvironment('SOCAPP_HTTP_GRAPHQL')
: (document['server']?['http']?['graphql'] ?? graphql);
port =
const bool.hasEnvironment('SOCAPP_HTTP_PORT')
? const int.fromEnvironment('SOCAPP_HTTP_PORT')
: (document['server']?['http']?['port'] ?? port);
url =
const bool.hasEnvironment('SOCAPP_HTTP_URL')
? const String.fromEnvironment('SOCAPP_HTTP_URL')
: (document['server']?['http']?['url'] ?? url);
String wsUrl =
const bool.hasEnvironment('SOCAPP_WS_URL')
? const String.fromEnvironment('SOCAPP_WS_URL')
: (document['server']?['ws']?['url'] ?? 'ws://localhost');
int wsPort =
const bool.hasEnvironment('SOCAPP_WS_PORT')
? const int.fromEnvironment('SOCAPP_WS_PORT')
: (document['server']?['ws']?['port'] ?? 80);
files =
const bool.hasEnvironment('SOCAPP_FILES_URL')
? const String.fromEnvironment('SOCAPP_FILES_URL')
: (document['files']?['url'] ?? files);
sentryDsn =
const bool.hasEnvironment('SOCAPP_SENTRY_DSN')
? const String.fromEnvironment('SOCAPP_SENTRY_DSN')
: (document['sentry']?['dsn'] ?? sentryDsn);
downloads =
const bool.hasEnvironment('SOCAPP_DOWNLOADS_DIRECTORY')
? const String.fromEnvironment('SOCAPP_DOWNLOADS_DIRECTORY')
: (document['downloads']?['directory'] ?? downloads);
downloadable =
const bool.hasEnvironment('SOCAPP_DOWNLOADS_DOWNLOADABLE')
? const bool.fromEnvironment('SOCAPP_DOWNLOADS_DOWNLOADABLE')
: (document['downloads']?['downloadable'] ?? downloadable);
appStoreUrl =
const bool.hasEnvironment('SOCAPP_DOWNLOADS_APP_STORE_URL')
? const String.fromEnvironment('SOCAPP_DOWNLOADS_APP_STORE_URL')
: (document['downloads']?['app_store_url'] ?? appStoreUrl);
googlePlayUrl =
const bool.hasEnvironment('SOCAPP_DOWNLOADS_GOOGLE_PLAY_URL')
? const String.fromEnvironment('SOCAPP_DOWNLOADS_GOOGLE_PLAY_URL')
: (document['downloads']?['google_play_url'] ?? googlePlayUrl);
userAgentProduct =
const bool.hasEnvironment('SOCAPP_USER_AGENT_PRODUCT')
? const String.fromEnvironment('SOCAPP_USER_AGENT_PRODUCT')
: (document['user']?['agent']?['product'] ?? userAgentProduct);
String version =
const bool.hasEnvironment('SOCAPP_USER_AGENT_VERSION')
? const String.fromEnvironment('SOCAPP_USER_AGENT_VERSION')
: (document['user']?['agent']?['version'] ?? '');
userAgentVersion = version.isNotEmpty ? version : Pubspec.ref;
clsid =
const bool.hasEnvironment('SOCAPP_WINDOWS_CLSID')
? const String.fromEnvironment('SOCAPP_WINDOWS_CLSID')
: (document['windows']?['clsid'] ?? clsid);
vapidKey =
const bool.hasEnvironment('SOCAPP_FCM_VAPID_KEY')
? const String.fromEnvironment('SOCAPP_FCM_VAPID_KEY')
: (document['fcm']?['vapidKey'] ?? vapidKey);
origin = url;
link =
const bool.hasEnvironment('SOCAPP_LINK_PREFIX')
? const String.fromEnvironment('SOCAPP_LINK_PREFIX')
: (document['link']?['prefix'] ?? link);
logLevel = me.LogLevel.values.firstWhere(
(e) =>
const bool.hasEnvironment('SOCAPP_LOG_LEVEL')
? e.name == const String.fromEnvironment('SOCAPP_LOG_LEVEL')
: e.name == document['log']?['level'],
orElse:
() =>
kDebugMode || kProfileMode
? me.LogLevel.debug
: me.LogLevel.debug,
);
appcast =
const bool.hasEnvironment('SOCAPP_APPCAST_URL')
? const String.fromEnvironment('SOCAPP_APPCAST_URL')
: (document['appcast']?['url'] ?? appcast);
copyright =
const bool.hasEnvironment('SOCAPP_LEGAL_COPYRIGHT')
? const String.fromEnvironment('SOCAPP_LEGAL_COPYRIGHT')
: (document['legal']?['copyright'] ?? copyright);
support =
const bool.hasEnvironment('SOCAPP_LEGAL_SUPPORT')
? const String.fromEnvironment('SOCAPP_LEGAL_SUPPORT')
: (document['legal']?['support'] ?? support);
repository =
const bool.hasEnvironment('SOCAPP_LEGAL_REPOSITORY')
? const String.fromEnvironment('SOCAPP_LEGAL_REPOSITORY')
: (document['legal']?['repository'] ?? repository);
scheme =
const bool.hasEnvironment('SOCAPP_LINK_SCHEME')
? const String.fromEnvironment('SOCAPP_LINK_SCHEME')
: (document['link']?['scheme'] ?? scheme);
geoEndpoint =
const bool.hasEnvironment('SOCAPP_GEO_ENDPOINT')
? const String.fromEnvironment('SOCAPP_GEO_ENDPOINT')
: (document['geo']?['endpoint'] ?? geoEndpoint);
ipEndpoint =
const bool.hasEnvironment('SOCAPP_IP_ENDPOINT')
? const String.fromEnvironment('SOCAPP_IP_ENDPOINT')
: (document['ip']?['endpoint'] ?? ipEndpoint);
// Change default values to browser's location on web platform.
if (PlatformUtils.isWeb) {
if (document['server']?['http']?['url'] == null &&
!const bool.hasEnvironment('SOCAPP_HTTP_URL')) {
url = '${Uri.base.scheme}://${Uri.base.host}';
}
if (document['server']?['http']?['port'] == null &&
!const bool.hasEnvironment('SOCAPP_HTTP_PORT')) {
port = Uri.base.scheme == 'https' ? 443 : 80;
}
if (document['server']?['ws']?['url'] == null &&
!const bool.hasEnvironment('SOCAPP_WS_URL')) {
wsUrl =
(Uri.base.scheme == 'https' ? 'wss://' : 'ws://') + Uri.base.host;
}
if (document['server']?['ws']?['port'] == null &&
!const bool.hasEnvironment('SOCAPP_WS_PORT')) {
wsPort = Uri.base.scheme == 'https' ? 443 : 80;
}
}
if (document['files']?['url'] == null &&
!const bool.hasEnvironment('SOCAPP_FILES_URL')) {
files = '$url/files';
}
bool confRemote =
const bool.hasEnvironment('SOCAPP_CONF_REMOTE')
? const bool.fromEnvironment('SOCAPP_CONF_REMOTE')
: (document['conf']?['remote'] ?? true);
// If [confRemote], then try to fetch and merge the remotely available
// configuration.
if (confRemote) {
try {
final response = await (await PlatformUtils.dio).fetch(
RequestOptions(path: '$url:$port/conf'),
);
if (response.statusCode == 200) {
dynamic remote;
try {
remote = TomlDocument.parse(response.data.toString()).toMap();
} catch (e) {
remote = loadYaml(response.data.toString());
}
confRemote = remote['conf']?['remote'] ?? confRemote;
if (confRemote) {
graphql = remote['server']?['http']?['graphql'] ?? graphql;
port = _asInt(remote['server']?['http']?['port']) ?? port;
url = remote['server']?['http']?['url'] ?? url;
wsUrl = remote['server']?['ws']?['url'] ?? wsUrl;
wsPort = _asInt(remote['server']?['ws']?['port']) ?? wsPort;
files = remote['files']?['url'] ?? files;
sentryDsn = remote['sentry']?['dsn'] ?? sentryDsn;
downloads = remote['downloads']?['directory'] ?? downloads;
userAgentProduct =
remote['user']?['agent']?['product'] ?? userAgentProduct;
userAgentVersion =
remote['user']?['agent']?['version'] ?? userAgentVersion;
vapidKey = remote['fcm']?['vapidKey'] ?? vapidKey;
link = remote['link']?['prefix'] ?? link;
appcast = remote['appcast']?['url'] ?? appcast;
copyright =
remote['legal']?[Uri.base.host]?['copyright'] ??
remote['legal']?['copyright'] ??
copyright;
support = remote['legal']?['support'] ?? support;
repository = remote['legal']?['repository'] ?? repository;
geoEndpoint = remote['geo']?['endpoint'] ?? geoEndpoint;
ipEndpoint = remote['ip']?['endpoint'] ?? ipEndpoint;
if (remote['log']?['level'] != null) {
logLevel = me.LogLevel.values.firstWhere(
(e) => e.name == remote['log']?['level'],
orElse: () => logLevel,
);
}
origin = url;
}
}
} catch (e) {
Log.info('Remote configuration fetch failed.', 'Config');
}
}
if (PlatformUtils.isWeb) {
if ((Uri.base.scheme == 'https' && Uri.base.port != 443) ||
Uri.base.scheme == 'http' && Uri.base.port != 80) {
origin = '${Uri.base.scheme}://${Uri.base.host}:${Uri.base.port}';
} else {
origin = '${Uri.base.scheme}://${Uri.base.host}';
}
}
if (link.isEmpty) {
link = '$origin${Routes.chatDirectLink}';
}
ws = '$wsUrl:$wsPort$graphql';
// Notification Service Extension needs those to send message received
// notification to backend.
if (PlatformUtils.isIOS) {
IosUtils.writeDefaults('url', url);
IosUtils.writeDefaults('endpoint', graphql);
}
}