Does Dart have a standard way to externalize settings like Java properties?

I am looking for the right way to externalize settings in a Dart server application.

In Java, a common way would be a properties file. Is there something similar in Darth?

+6
source share
5 answers

You can use the Dart script for your settings. It makes no sense to use a different format if there is no specific reason. With a simple import, you have it available in a typical way.

+4
source

You can use global variables, for example:

DB_URL = 'localhost:5432/mydb'; DB_PASS = 'my_pass'; 

then you can create a different configuration file for each environment. For example, for production, you can create a production_config.dart file that can contain:

 loadConfig() { DB_URL = '123.123.123.123:5432/mydb'; DB_PASS = 'my_prod_pass'; } 

Then in your main function you can call production_config.loadConfig if the environment is production, for example:

 import 'production_config.dart' as prodConfig; main(List<String> args) { var ENV = getEnvFromArgs(args); if(ENV == 'PROD') { prodConfig.loadConfig(); } //do other stuff here } 

Thus, if you want to move from development to production, you need to pass an argument to your dart program, for example:

 dart myprogram.dart -env=PROD 

The advantages of this approach are that for this you do not need to create separate properties, a json or yaml file, and you do not need to analyze them. In addition, properties are type-ckecked.

+2
source

When the Resource class is implemented, I would just use the JSON file that was deployed with my program.

+2
source
 main() { var env = const String.fromEnvironment("ENV", defaultValue: "local"); print("Env === " + env); } 

Providing an environment as an option when starting the Dart pub serve application --port = 9002 --define ENV = dev

Literature:

http://blog.sethladd.com/2013/12/compile-time-dead-code-elimination-with.html https://github.com/dart-lang/sdk/issues/27998

0
source

I like to put the configuration in the Dart class, like what Gunter Zochbauer talked about , but there is also the option to use the safe_config package. In doing so, you enter the values ​​in the yaml file. Quoting from documents:

You define a subclass of Configuration with these properties:

 class ApplicationConfiguration extends Configuration { ApplicationConfiguration(String fileName) : super.fromFile(File(fileName)); int port; String serverHeader; } 

Your YAML file must contain these two keys, case sensitive:

 port: 8000 serverHeader: booyah/1 

To read your configuration file:

 var config = new ApplicationConfiguration("config.yaml"); print("${config.port}"); // -> 8000 print("${config.serverHeader}"); // -> "booyah/1" 

See also an example from an installation in the Aqueduct.

0
source

All Articles