Skip to content

Commit

Permalink
server/simple: better demonstrate route logic (dart-lang#107)
Browse files Browse the repository at this point in the history
Co-authored-by: Kathy Walrath <[email protected]>
  • Loading branch information
kevmoo and kwalrath authored May 13, 2021
1 parent 1a01ca9 commit 74b6fa3
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 7 deletions.
17 changes: 16 additions & 1 deletion server/simple/bin/server.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// 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:convert';
import 'dart:io';

import 'package:shelf/shelf.dart';
Expand Down Expand Up @@ -47,6 +48,20 @@ final _router = shelf_router.Router()
..get(
'/time',
(request) => Response.ok(DateTime.now().toUtc().toIso8601String()),
);
)
..get('/sum/<a|[0-9]+>/<b|[0-9]+>', _sumHandler);

Response _helloWorldHandler(Request request) => Response.ok('Hello, World!');

Response _sumHandler(request, String a, String b) {
final aNum = int.parse(a);
final bNum = int.parse(b);
return Response.ok(
const JsonEncoder.withIndent(' ')
.convert({'a': aNum, 'b': bNum, 'sum': aNum + bNum}),
headers: {
'content-type': 'application/json',
'Cache-Control': 'public, max-age=604800',
},
);
}
27 changes: 23 additions & 4 deletions server/simple/public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="icon" sizes="64x64" href="64.png">
<title>Dart Server</title>
<title>pkg:shelf example</title>
</head>

<style>
Expand All @@ -21,19 +21,38 @@
p {
margin-top: 1rem;
font-size: large;
max-width: 600px;
}

img {
max-width: 70vw;
}

code {
background: #eee;
padding: 3px;
}
</style>

<body>
<h1>Welcome to Dashland!</h1>
<h1>pkg:shelf example</h1>
<img src="/dashland.jpeg" alt="Dashland">
<p>This file is being served by <a href="https://pub.dev/packages/shelf_static">package:shelf_static</a> from the <code>/public</code> directory.</p>
<p>This file is being served by <a
href="https://pub.dev/packages/shelf_static">package:shelf_static</a> from the
<code>/public</code> directory.</p>
<!-- TODO: Link to new pkg:shelf docs once https://github.com/dart-lang/site-www/issues/3058 is fixed -->
<p>There are code-based handlers at <a href="/helloworld">/helloworld</a> and <a href="/time">/time</a></p>
<p>This app uses <a
href="https://pub.dev/packages/shelf_router">package:shelf_router</a> to define several code-based handlers:</p>
<ul>
<li><a href="/helloworld">/helloworld</a></li>
<li><a href="/time">/time</a>
<li><code>/sum/&lt;a|[0-9]+&gt;/&lt;b|[0-9]+&gt;</code></li>
</ul>

<p>
The last handler is defined with regular expressions to handle requests to <a href="/sum/1/2">/sum/1/2</a> and <a href="/sum/35/7">/sum/35/7</a>,
but not <a href="/sum/bob/john">/sum/bob/john</a> or <a href="/sum/1/2/3">/sum/1/2/3</a>.
<p>
</body>

</html>
7 changes: 5 additions & 2 deletions server/simple/test/test_definitions.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ void runTests(
testServer('root', (host) async {
final response = await get(Uri.parse(host));
expect(response.statusCode, 200);
expect(response.body, contains('Welcome to Dashland!'));
expect(response.body, contains('pkg:shelf example'));
expect(response.headers, contains('last-modified'));
expect(response.headers, contains('date'));
expect(response.headers, containsPair('content-type', 'text/html'));
});

testServer('time', (host) async {
Expand All @@ -35,6 +38,6 @@ void runTests(
response = await post(Uri.parse('$host'));
// https://github.com/dart-lang/shelf_static/issues/53 - should 405
expect(response.statusCode, 200);
expect(response.body, contains('Welcome to Dashland!'));
expect(response.body, contains('pkg:shelf example'));
});
}

0 comments on commit 74b6fa3

Please sign in to comment.