8

I'm looking for the right way to externalize the settings in my server Dart application.

In Java the common way would be a property file. Exists something similar in Dart?

lascarayf
  • 3,423
  • 3
  • 19
  • 24

5 Answers5

5

You can just use a Dart script for your settings. No point in using a different format if there is no specific reason. With a simple import you have it available in a typed way.

Günter Zöchbauer
  • 623,577
  • 216
  • 2,003
  • 1,567
  • The point of an external config file is usually so that development and production environments can use different values. This wouldn't work so well with the solution you propose. – Pixel Elephant Jul 29 '15 at 21:46
  • If you don't check it in you have just that. You also wouldn't check in a settings file with service passwords. This is an advantage of a scripting language without a compilation step. – Günter Zöchbauer Jul 29 '15 at 21:47
  • When you say a "Dart script", are you talking about something like [this](https://stackoverflow.com/a/35848325/3681880)? – Suragch Apr 09 '19 at 17:32
  • @Suragch yes, that's what I mean. – Günter Zöchbauer Apr 10 '19 at 03:36
4

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

lrn
  • 64,680
  • 7
  • 105
  • 121
2

You could use a global variables, for example:

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

then you could create a different configuration file for every enviroment. For example, for production you could create a production_config.dart which could contains:

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

Then in your main function you could call production_config.loadConfig if 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
}

In that way if you want to change from development to production you only need to pass an argument to your dart program for example:

dart myprogram.dart -env=PROD

The advantages of this approach are that you don't need to create a separate properties, json or yaml file for this, and you don't need to parse them. Furthermore the properties are type-ckecked.

Luis Vargas
  • 2,466
  • 2
  • 15
  • 32
  • Yes it's a good solution but again you can't load the config from external file. If you want to make a change in production URL you need to rebuild and redeploy. – lascarayf Jul 31 '15 at 17:22
2

I like putting configuration in a Dart class like what Günter Zöchbauer was talking about, but there is also the option of using the safe_config package. With this you enter the values in a yaml file. Quoting from the docs:

You define a subclass of Configuration with those properties:

class ApplicationConfiguration extends Configuration {
  ApplicationConfiguration(String fileName) : 
      super.fromFile(File(fileName));

  int port;
  String serverHeader;
}

Your YAML file should contain those two, case-sensitive keys:

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 a setup in Aqueduct.

Suragch
  • 484,302
  • 314
  • 1,365
  • 1,393
0
main() { 
  var env = const String.fromEnvironment("ENV", defaultValue: "local");
  print("Env === " + env);

}

Give environment as option while running Dart App pub serve --port=9002 --define ENV=dev

References:

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