Skip to content
Merged
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
6 changes: 4 additions & 2 deletions server/simple/README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
![Welcome Dart Server](public/welcome_dart_server.png)

A simple Dart HTTP server using [package:shelf](https://pub.dev/packages/shelf).

- Listens on "any IP" (0.0.0.0) instead of loop-back (localhost, 127.0.0.1) to
Expand Down Expand Up @@ -28,13 +30,13 @@ or follow

### With OS-only runtime

Use [`tool/deploy_source.sh`](tool/deploy_source.sh) to build and
Use [`tool/deploy_source.dart`](tool/deploy_source.dart) to build and
deploy using the
[OS-only runtimes](https://docs.cloud.google.com/docs/buildpacks/osonly)
feature.

> [!NOTE]
> As of January 2026, this feature is in "Preview" and
> As of May 2026, this feature is in "Preview" and
> requires the ["beta" gcloud component][gcloud-beta].

[cloud-beta]: https://docs.cloud.google.com/sdk/docs/components#alpha_and_beta_components
9 changes: 6 additions & 3 deletions server/simple/bin/server.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import 'package:shelf_static/shelf_static.dart' as shelf_static;
Future<void> main() async {
// If the "PORT" environment variable is set, listen to it. Otherwise, 8080.
// https://cloud.google.com/run/docs/reference/container-contract#port
final port = int.parse(Platform.environment['PORT'] ?? '8080');
final port = int.tryParse(Platform.environment['PORT'] ?? '8080') ?? 8080;

// See https://pub.dev/documentation/shelf/latest/shelf/Cascade-class.html
final cascade = Cascade()
Expand Down Expand Up @@ -63,8 +63,11 @@ String _jsonEncode(Object? data) =>
const _jsonHeaders = {'content-type': 'application/json'};

Response _sumHandler(Request request, String a, String b) {
final aNum = int.parse(a);
final bNum = int.parse(b);
final aNum = int.tryParse(a);
final bNum = int.tryParse(b);
if (aNum == null || bNum == null) {
return Response.badRequest(body: 'Invalid integer arguments');
}
return Response.ok(
_jsonEncode({'a': aNum, 'b': bNum, 'sum': aNum + bNum}),
headers: {
Expand Down
Binary file removed server/simple/public/dashland.jpeg
Binary file not shown.
75 changes: 27 additions & 48 deletions server/simple/public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,57 +7,36 @@
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="icon" sizes="64x64" href="64.png">
<title>pkg:shelf example</title>
<link rel="stylesheet" href="style.css">
</head>

<style>
body {
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
font-family: sans-serif;
}

p {
margin-top: 1rem;
font-size: large;
max-width: 600px;
}

img {
max-width: 70vw;
}

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

<body>
<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>
<!-- TODO: Link to new pkg:shelf docs once https://github.com/dart-lang/site-www/issues/3058 is fixed -->
<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><a href="/info.json">/info.json</a> (also embedded as an <code>iframe</code> below)</li>
<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>

<iframe width="400" height="100" src="info.json"></iframe>
<img src="/welcome_dart_server.png" alt="Welcome Dart Server" width="519" height="300">
<p>This application is built using the following packages:</p>
<dl>
<dt><a href="https://pub.dev/packages/shelf">package:shelf</a></dt>
<dd>Provides the core HTTP server middleware and request/response handling.</dd>
<dt><a href="https://pub.dev/packages/shelf_static">package:shelf_static</a></dt>
<dd>Serves static files from the local filesystem (including this page and images from the <code>/public</code>
directory).</dd>
<dt><a href="https://pub.dev/packages/shelf_router">package:shelf_router</a></dt>
<dd>Defines dynamic route handlers and regular expression URL pattern matching.</dd>
</dl>

<p>The router defines several code-based handlers:</p>
<ul>
<li><a href="/helloworld">/helloworld</a><br>Returns plain text.
</li>
<li><a href="/time">/time</a><br>Returns <code>application/json</code> with the current UTC date and
time.</li>
<li><code>/sum/&lt;a|[0-9]+&gt;/&lt;b|[0-9]+&gt;</code><br>Uses 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>.
</li>
<li><a href="/info.json">/info.json</a><br>Returns the info.json shown in the iframe below.</li>
</ul>

<iframe width="650" height="300" src="info.json"></iframe>

</body>

Expand Down
134 changes: 134 additions & 0 deletions server/simple/public/style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
:root {
--bg-color: #f8fafc;
--text-color: #1e293b;
--link-color: #2563eb;
--link-hover: #1d4ed8;
--card-bg: #ffffff;
--border-color: #e2e8f0;
--code-bg: #f1f5f9;
--accent-color: #0284c7;
}

@media (prefers-color-scheme: dark) {
:root {
--bg-color: #0f172a;
--text-color: #f1f5f9;
--link-color: #38bdf8;
--link-hover: #7dd3fc;
--card-bg: #1e293b;
--border-color: #334155;
--code-bg: #0b0f19;
--accent-color: #38bdf8;
}
}

body {
display: flex;
flex-direction: column;
align-items: center;
font-family: system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto,
Oxygen, Ubuntu, Cantarell, sans-serif;
background-color: var(--bg-color);
color: var(--text-color);
margin: 0;
padding: 2rem 1rem;
line-height: 1.6;
}

p {
margin-top: 1rem;
font-size: 1.1rem;
max-width: 650px;
width: 100%;
}

a {
color: var(--link-color);
text-decoration: none;
font-weight: 500;
transition: color 0.2s ease-in-out;
}

a:hover {
color: var(--link-hover);
text-decoration: underline;
}

img {
max-width: 100%;
height: auto;
box-shadow: 0 20px 25px -5px rgba(0, 0, 0, 0.1),
0 8px 10px -6px rgba(0, 0, 0, 0.1);
transition: transform 0.3s ease;
}

img:hover {
transform: scale(1.02);
}

code {
background-color: var(--code-bg);
padding: 0.2rem 0.4rem;
font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, monospace;
font-size: 0.95em;
color: var(--accent-color);
}

dl,
ul {
background-color: var(--card-bg);
border: 1px solid var(--border-color);
padding: 1.5rem 2rem;
max-width: 650px;
width: 100%;
box-sizing: border-box;
box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.05);
margin: 1rem 0;
}

dt {
font-weight: 700;
font-size: 1.2rem;
margin-top: 1rem;
color: var(--link-color);
}

dt:first-child {
margin-top: 0;
}

dd {
margin-bottom: 1.2rem;
margin-left: 0;
padding-left: 1rem;
border-left: 3px solid var(--link-color);
color: var(--text-color);
}

ul {
list-style-type: none;
padding-left: 1.5rem;
}

li {
margin-bottom: 0.5rem;
position: relative;
}

li::before {
content: "•";
color: var(--link-color);
font-weight: bold;
display: inline-block;
width: 1em;
margin-left: -1em;
}

iframe {
border: 1px solid var(--border-color);
background-color: var(--card-bg);
box-shadow: 0 10px 15px -3px rgba(0, 0, 0, 0.1);
margin-top: 2rem;
max-width: 100%;
box-sizing: border-box;
}
Binary file added server/simple/public/welcome_dart_server.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 2 additions & 0 deletions server/simple/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ dependencies:
shelf_static: ^1.1.3

dev_dependencies:
args: ^2.7.0
http: ^1.6.0
lints: ^6.0.0
path: ^1.9.0
test: ^1.27.0
test_process: ^2.1.1
13 changes: 13 additions & 0 deletions server/simple/test/test_definitions.dart
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,17 @@ void runTests(
expect(response.statusCode, 200);
expect(response.body, contains('pkg:shelf example'));
});

testServer('sum', (host) async {
var response = await get(Uri.parse('$host/sum/1/2'));
expect(response.statusCode, 200);
expect(response.body, contains('"sum": 3'));

// Test integer overflow / invalid integer handling.
response = await get(
Uri.parse('$host/sum/9999999999999999999999999999999999999999999/2'),
);
expect(response.statusCode, 400);
expect(response.body, 'Invalid integer arguments');
});
}
Loading
Loading