diff --git a/bricks/dart_frog_dev_server/__brick__/server.dart b/bricks/dart_frog_dev_server/__brick__/server.dart index 617fde268..9dcb9243c 100644 --- a/bricks/dart_frog_dev_server/__brick__/server.dart +++ b/bricks/dart_frog_dev_server/__brick__/server.dart @@ -11,13 +11,13 @@ import 'package:dart_frog/dart_frog.dart'; {{#middleware}}import '{{{path}}}' as {{#snakeCase}}{{{name}}}{{/snakeCase}}; {{/middleware}} void main() async { - final address = InternetAddress.anyIPv6; + final address = InternetAddress.tryParse('{{{host}}}') ?? InternetAddress.anyIPv6; final port = int.tryParse(Platform.environment['PORT'] ?? '{{{port}}}') ?? {{{port}}};{{#invokeCustomInit}} await entrypoint.init(address, port);{{/invokeCustomInit}} hotReload(() => createServer(address, port)); } -Future createServer(InternetAddress address, int port) { +Future createServer(Object address, int port) { final handler = Cascade(){{#serveStaticFiles}}.add(createStaticFileHandler()){{/serveStaticFiles}}.add(buildRootHandler()).handler; {{#invokeCustomEntrypoint}}return entrypoint.run(handler, address, port);{{/invokeCustomEntrypoint}}{{^invokeCustomEntrypoint}}return serve(handler, address, port);{{/invokeCustomEntrypoint}} } diff --git a/bricks/dart_frog_dev_server/hooks/pre_gen.dart b/bricks/dart_frog_dev_server/hooks/pre_gen.dart index 778ea4c36..67b82d947 100644 --- a/bricks/dart_frog_dev_server/hooks/pre_gen.dart +++ b/bricks/dart_frog_dev_server/hooks/pre_gen.dart @@ -56,6 +56,7 @@ Future preGen( ); context.vars = { + 'host': context.vars['host'] ?? '', 'port': context.vars['port'] ?? '8080', 'directories': configuration.directories .map((c) => c.toJson()) diff --git a/bricks/dart_frog_dev_server/hooks/test/pre_gen_test.dart b/bricks/dart_frog_dev_server/hooks/test/pre_gen_test.dart index d1eaa58b9..2ec6af468 100644 --- a/bricks/dart_frog_dev_server/hooks/test/pre_gen_test.dart +++ b/bricks/dart_frog_dev_server/hooks/test/pre_gen_test.dart @@ -162,6 +162,43 @@ void main() { equals( { 'port': customPort, + 'host': '', + 'directories': [], + 'routes': [], + 'middleware': [], + 'globalMiddleware': false, + 'serveStaticFiles': false, + 'invokeCustomEntrypoint': false, + 'invokeCustomInit': false, + }, + ), + ); + }); + + test('retains custom host if specified', () async { + const customHost = '192.168.1.2'; + context.vars['host'] = customHost; + const configuration = RouteConfiguration( + middleware: [], + directories: [], + routes: [], + rogueRoutes: [], + endpoints: {}, + ); + final exitCalls = []; + await preGen( + context, + buildConfiguration: (_) => configuration, + exit: exitCalls.add, + ); + expect(exitCalls, isEmpty); + verifyNever(() => logger.err(any())); + expect( + context.vars, + equals( + { + 'port': '8080', + 'host': '192.168.1.2', 'directories': [], 'routes': [], 'middleware': [], @@ -198,6 +235,7 @@ void main() { equals( { 'port': customPort, + 'host': '', 'directories': [], 'routes': [], 'middleware': [], @@ -234,6 +272,7 @@ void main() { equals( { 'port': customPort, + 'host': '', 'directories': [], 'routes': [], 'middleware': [], @@ -335,6 +374,7 @@ void main() { equals( { 'port': '8080', + 'host': '', 'directories': [ { 'name': '_',