It would be nice to allow my Dart web app to hit different servers depending on what environment it was deployed on:
http://dev.myapp.com/someService
http://testing.myapp.com/someService
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.)
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
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'];
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With