Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add command line arguments to all examples. Were already on some. #410

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 27 additions & 2 deletions pkgs/shelf/example/example.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,34 @@
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

import 'dart:io';

import 'package:args/args.dart';
import 'package:shelf/shelf.dart';
import 'package:shelf/shelf_io.dart' as shelf_io;

void main() async {
void main(List<String> args) async {
final parser = _getParser();

String address;
int port;
try {
final result = parser.parse(args);
address = result['address'] as String;
port = int.parse(result['port'] as String);
} on FormatException catch (e) {
stderr
..writeln(e.message)
..writeln(parser.usage);
// http://linux.die.net/include/sysexits.h
// #define EX_USAGE 64 /* command line usage error */
exit(64);
}

var handler =
const Pipeline().addMiddleware(logRequests()).addHandler(_echoRequest);

var server = await shelf_io.serve(handler, 'localhost', 8080);
var server = await shelf_io.serve(handler, address, port);

// Enable content compression
server.autoCompress = true;
Expand All @@ -19,3 +39,8 @@ void main() async {

Response _echoRequest(Request request) =>
Response.ok('Request for "${request.url}"');

ArgParser _getParser() => ArgParser()
..addOption('port', abbr: 'p', defaultsTo: '8080', help: 'Port to listen on')
..addOption('address',
abbr: 'a', defaultsTo: 'localhost', help: 'Address to listen on');
1 change: 1 addition & 0 deletions pkgs/shelf/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ dependencies:
stream_channel: ^2.1.0

dev_dependencies:
args: ^2.0.0
dart_flutter_team_lints: ^2.0.0
http: '>=0.13.0 <2.0.0'
test: ^1.16.0
40 changes: 35 additions & 5 deletions pkgs/shelf_proxy/example/example.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,45 @@
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

import 'dart:io';

import 'package:args/args.dart';
import 'package:shelf/shelf_io.dart' as shelf_io;
import 'package:shelf_proxy/shelf_proxy.dart';

Future<void> main() async {
Future<void> main(List<String> args) async {
final parser = _getParser();

String address;
int port;
String targetAddress;
try {
final result = parser.parse(args);
address = result['address'] as String;
port = int.parse(result['port'] as String);
targetAddress = result['targetAddress'] as String;
} on FormatException catch (e) {
stderr
..writeln(e.message)
..writeln(parser.usage);
// http://linux.die.net/include/sysexits.h
// #define EX_USAGE 64 /* command line usage error */
exit(64);
}

final server = await shelf_io.serve(
proxyHandler('https://dart.dev'),
'localhost',
8080,
proxyHandler(targetAddress),
address,
port,
);

print('Proxying at http://${server.address.host}:${server.port}');
print(
'Proxying for $targetAddress at http://${server.address.host}:${server.port}');
}

ArgParser _getParser() => ArgParser()
..addOption('port', abbr: 'p', defaultsTo: '8080', help: 'Port to listen on')
..addOption('address',
abbr: 'a', defaultsTo: 'localhost', help: 'Address to listen on')
..addOption('targetAddress',
abbr: 't', defaultsTo: 'https://dart.dev', help: 'Address proxying for');
1 change: 1 addition & 0 deletions pkgs/shelf_proxy/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,6 @@ dependencies:
shelf: ^1.0.0

dev_dependencies:
args: ^2.0.0
dart_flutter_team_lints: ^2.0.0
test: ^1.6.0
28 changes: 26 additions & 2 deletions pkgs/shelf_router/example/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@
// limitations under the License.

import 'dart:async' show Future;
import 'dart:io';

import 'package:args/args.dart';
import 'package:shelf/shelf.dart';
import 'package:shelf/shelf_io.dart' as shelf_io;
import 'package:shelf_router/shelf_router.dart';
Expand Down Expand Up @@ -79,8 +81,30 @@ class Api {
}

// Run shelf server and host a [Service] instance on port 8080.
void main() async {
void main(List<String> args) async {
final parser = _getParser();

String address;
int port;
try {
final result = parser.parse(args);
address = result['address'] as String;
port = int.parse(result['port'] as String);
} on FormatException catch (e) {
stderr
..writeln(e.message)
..writeln(parser.usage);
// http://linux.die.net/include/sysexits.h
// #define EX_USAGE 64 /* command line usage error */
exit(64);
}

final service = Service();
final server = await shelf_io.serve(service.handler, 'localhost', 8080);
final server = await shelf_io.serve(service.handler, address, port);
print('Server running on localhost:${server.port}');
}

ArgParser _getParser() => ArgParser()
..addOption('port', abbr: 'p', defaultsTo: '8080', help: 'Port to listen on')
..addOption('address',
abbr: 'a', defaultsTo: 'localhost', help: 'Address to listen on');
1 change: 1 addition & 0 deletions pkgs/shelf_router/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ dependencies:
shelf: ^1.0.0

dev_dependencies:
args: ^2.0.0
dart_flutter_team_lints: ^2.0.0
http: '>=0.13.0 <2.0.0'
test: ^1.16.0
28 changes: 26 additions & 2 deletions pkgs/shelf_router_generator/example/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@
// limitations under the License.

import 'dart:async' show Future;
import 'dart:io';

import 'package:args/args.dart';
import 'package:shelf/shelf.dart';
import 'package:shelf/shelf_io.dart' as shelf_io;
import 'package:shelf_router/shelf_router.dart';
Expand Down Expand Up @@ -73,8 +75,30 @@ class Api {
}

// Run shelf server and host a [Service] instance on port 8080.
void main() async {
void main(List<String> args) async {
final parser = _getParser();

String address;
int port;
try {
final result = parser.parse(args);
address = result['address'] as String;
port = int.parse(result['port'] as String);
} on FormatException catch (e) {
stderr
..writeln(e.message)
..writeln(parser.usage);
// http://linux.die.net/include/sysexits.h
// #define EX_USAGE 64 /* command line usage error */
exit(64);
}

final service = Service();
final server = await shelf_io.serve(service.handler, 'localhost', 8080);
final server = await shelf_io.serve(service.handler, address, port);
print('Server running on localhost:${server.port}');
}

ArgParser _getParser() => ArgParser()
..addOption('port', abbr: 'p', defaultsTo: '8080', help: 'Port to listen on')
..addOption('address',
abbr: 'a', defaultsTo: 'localhost', help: 'Address to listen on');
1 change: 1 addition & 0 deletions pkgs/shelf_router_generator/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ dependencies:
source_gen: ^1.0.0

dev_dependencies:
args: ^2.0.0
build_runner: ^2.0.0
build_verify: ^3.0.0
dart_flutter_team_lints: ^2.0.0
Expand Down
10 changes: 7 additions & 3 deletions pkgs/shelf_static/example/example.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@ import 'package:shelf_static/shelf_static.dart';
void main(List<String> args) {
final parser = _getParser();

String address;
int port;
bool logging;
bool listDirectories;

try {
final result = parser.parse(args);
address = result['address'] as String;
port = int.parse(result['port'] as String);
logging = result['logging'] as bool;
listDirectories = result['list-directories'] as bool;
Expand Down Expand Up @@ -47,14 +49,16 @@ void main(List<String> args) {
final handler = pipeline.addHandler(createStaticHandler('example/files',
defaultDocument: defaultDoc, listDirectories: listDirectories));

io.serve(handler, 'localhost', port).then((server) {
io.serve(handler, address, port).then((server) {
print('Serving at http://${server.address.host}:${server.port}');
});
}

ArgParser _getParser() => ArgParser()
..addFlag('logging', abbr: 'l', defaultsTo: true)
..addOption('port', abbr: 'p', defaultsTo: '8080')
..addFlag('logging', abbr: 'l', defaultsTo: true, help: 'Enable logging')
..addOption('port', abbr: 'p', defaultsTo: '8080', help: 'Port to listen on')
..addOption('address',
abbr: 'a', defaultsTo: 'localhost', help: 'Address to listen on')
..addFlag('list-directories',
abbr: 'f',
negatable: false,
Expand Down