4

It would be nice to allow my Dart web app to hit different servers depending on what environment it was deployed on:

  • DEV: http://dev.myapp.com/someService
  • QA: http://testing.myapp.com/someService
  • LIVE: http://myapp.com/someService

In Java, typically you'd have a deployment descriptor (myapp.properties) that the app reads off the runtime classpath, allowing you to specify a myapp.properties on DEV like so:

service.url=dev.myapp.com/someService

And on QA like so:

service.url=qa.myapp/com/someService

etc. It looks like Dart offers something comparable however its server-side/command-line only...

So how do Dart web developers achieve the same thing, where you don't need to hardcode all of your various environments' servers into the app? (Obviously, this question extends beyond service URLs and really applies to any environment-specific property.)

Community
  • 1
  • 1
IAmYourFaja
  • 55,468
  • 181
  • 466
  • 756

2 Answers2

5

You can use the String.fromEnvironment constant constructors to get values passed to the dart2js compilers. For a full explaination on this new functionality check out Seth Ladd's blog post: Compile-time dead code elimination with dart2js

Matt B
  • 6,192
  • 1
  • 20
  • 27
  • Thanks @Matt B (+1) - can I pass those arguments into dart2j *through* `pub build` somehow? In other words: `pub build -Denv=DEV`, or `pub build -Denv=QA`, etc.? Or do I have to add arguments for non-production environments (`-Denv=QA`) and omit all the arguments when doing my production build (`pub build`)? – IAmYourFaja Dec 27 '13 at 17:53
  • 1
    Yes this was added just before 1.0. Here's the stable docs: https://api.dartlang.org/docs/channels/stable/latest/dart_core/String.html#fromEnvironment and at the moment it doesn't appear pub build supports them. I filed a bug for it here: http://dartbug.com/15806 – Matt B Dec 27 '13 at 18:05
  • Awesome! Thanks again @Matt B (+1) - unfortunately that leaves me in sort of a stale mate. I need to use String.fromEnvironment, but I'm currently building all of my web files via `pub build`. If `pub build` doesn't support the -D args, then I'll need to use dart2js directly. **But**, if I need to use dart2js directly, I'm wondering what I'll be missing out on that `pub build` does for me automagically. Do you know offhand what extra steps I'll need to take in order to run dart2j but still accomplish all the same things that `pub build` does? Thanks again! – IAmYourFaja Dec 27 '13 at 18:09
2

To keep the same build you can read a variable from html which could be generated on server side.

For instance the server could generate (or replace with templating) the html file with :

<script>
  // var serviceUrl = "@VALUE_OF_VAR@";
  var serviceUrl = "dev.myapp.com/someService";
</script>

and in dart client file :

import 'dart:js' as js;
main() {
  var serviceUrl = js.context['serviceUrl'];
}
Alexandre Ardhuin
  • 71,959
  • 15
  • 151
  • 132