Skip to content

Instantly share code, notes, and snippets.

@graphicbeacon
Created July 12, 2018 18:14
Show Gist options
  • Save graphicbeacon/57e478bd917f7a5f723ebb8882378177 to your computer and use it in GitHub Desktop.
Save graphicbeacon/57e478bd917f7a5f723ebb8882378177 to your computer and use it in GitHub Desktop.
Sample code for "Deploying Dart 2 apps to Heroku" on Medium
import 'dart:io' show Platform;
import 'dart:async' show runZoned;
import 'package:path/path.dart' show join, dirname;
import 'package:shelf/shelf_io.dart' as io;
import 'package:shelf_static/shelf_static.dart';
void main() {
// Assumes the server lives in bin/ and that `webdev build` ran
var pathToBuild = join(dirname(Platform.script.toFilePath()), '..', 'build');
var handler = createStaticHandler(pathToBuild, defaultDocument: 'index.html');
var portEnv = Platform.environment['PORT'];
var port = portEnv == null ? 9999 : int.parse(portEnv);
runZoned(() {
io.serve(handler, '0.0.0.0', port);
print("Serving $pathToBuild on port $port");
}, onError: (e, stackTrace) => print('Oh noes! $e $stackTrace'));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment