example of proxy using `pub serve`
diff --git a/example/example_server.dart b/example/example_server.dart
new file mode 100644
index 0000000..39be43c
--- /dev/null
+++ b/example/example_server.dart
@@ -0,0 +1,54 @@
+library shelf_static.example;
+
+import 'dart:convert';
+
+import 'package:shelf/shelf.dart';
+import 'package:shelf/shelf_io.dart' as io;
+import 'package:shelf_proxy/shelf_proxy.dart';
+
+const _PUB_PORT = 7777;
+final _encoder = new JsonEncoder.withIndent(' ');
+
+void main() {
+
+ //
+ // The api handler responds to requests to '/api' with a JSON document
+ // containing an incrementing 'count' value.
+ //
+ int apiCount = 0;
+ var apiHandler = (Request request) {
+ if (request.url.path == '/api') {
+ apiCount++;
+ var json = _encoder.convert({ 'count': apiCount});
+ var headers = { 'Content-Type' : 'application/json' };
+ return new Response.ok(json, headers: headers);
+ }
+
+ return new Response.notFound('');
+ };
+
+ //
+ // Cascade sends requests to `apiHandler` first. If that handler returns a
+ // 404, the request is next sent to the proxy handler pointing at pub
+ //
+ var cascade = new Cascade()
+ .add(apiHandler)
+ .add(createProxyHandler(Uri.parse('http://localhost:$_PUB_PORT')));
+
+ //
+ // Creates a pipeline handler which first logs requests and then sends them
+ // to the cascade.
+ //
+ var handler = const Pipeline().addMiddleware(logRequests())
+ .addHandler(cascade.handler);
+
+ //
+ // Serve the combined handler on localhost at port 8080.
+ //
+ io.serve(handler, 'localhost', 8080).then((server) {
+ print('Serving at http://${server.address.host}:${server.port}');
+ print('`pub serve` should be running at port $_PUB_PORT '
+ 'on the example dir.');
+ print(' command: pub serve --port $_PUB_PORT example/');
+ });
+}
diff --git a/example/index.dart b/example/index.dart
new file mode 100644
index 0000000..1931626
--- /dev/null
+++ b/example/index.dart
@@ -0,0 +1,13 @@
+import 'dart:convert';
+import 'dart:html';
+
+void main() {
+ var span = querySelector('#count') as SpanElement;
+
+ HttpRequest.getString('/api').then((value) {
+ return JSON.decode(value);
+ }).then((obj) {
+ var count = obj['count'];
+ span.text = count.toString();
+ });
+}
diff --git a/example/index.html b/example/index.html
new file mode 100644
index 0000000..690b362
--- /dev/null
+++ b/example/index.html
@@ -0,0 +1,16 @@
+<!DOCTYPE html>
+
+<html>
+ <head>
+ <meta charset="utf-8">
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
+ <title>shelf_proxy example</title>
+ </head>
+
+ <body>
+ <p id="text">API access count: <span id='count'>??</span></p>
+ <script type="application/dart" src="index.dart"></script>
+ <!-- for this next line to work, your pubspec.yaml file must have a dependency on 'browser' -->
+ <script src="packages/browser/dart.js"></script>
+ </body>
+</html>
diff --git a/pubspec.yaml b/pubspec.yaml
index c9c4365..ada7eb1 100644
--- a/pubspec.yaml
+++ b/pubspec.yaml
@@ -6,6 +6,7 @@
environment:
sdk: '>=1.0.0 <2.0.0'
dependencies:
+ browser: any
http_parser: '>=0.0.2+2 <0.1.0'
mime: '>=0.9.0 <0.10.0'
shelf: '>=0.5.3 <0.6.0'