1
0
mirror of https://github.com/flutter/samples.git synced 2025-11-09 06:18:49 +00:00

Re-add Craig Labenz's code sharing sample (#1466)

* Revert "Revert "Add code sharing sample (#1444)" (#1464)"

This reverts commit dc50c46ef1.

* Opting out failing tests on Windows for CI

Co-authored-by: Craig Labenz <craig.labenz@gmail.com>
This commit is contained in:
Brett Morgan
2022-10-18 13:15:31 -04:00
committed by GitHub
parent 62d5d8d095
commit d99fb974a0
152 changed files with 5229 additions and 1 deletions

View File

@@ -0,0 +1,36 @@
import 'dart:convert';
import 'dart:io';
import 'package:shared/shared.dart';
import 'package:shelf/shelf.dart';
import 'package:shelf/shelf_io.dart';
import 'package:shelf_router/shelf_router.dart';
int count = 0;
// Configure routes.
final _router = Router()
..post('/', _incrementHandler)
..get('/', _getValueHandler);
Future<Response> _incrementHandler(Request request) async {
final incr = Increment.fromJson(json.decode(await request.readAsString()));
count += incr.by;
return Response.ok(json.encode(Count(count).toJson()));
}
Response _getValueHandler(Request request) =>
Response.ok(json.encode(Count(count).toJson()));
void main(List<String> args) async {
// Use any available host or container IP (usually `0.0.0.0`).
final ip = InternetAddress.anyIPv4;
// Configure a pipeline that logs requests.
final handler = Pipeline().addMiddleware(logRequests()).addHandler(_router);
// For running in containers, we respect the PORT environment variable.
final port = int.parse(Platform.environment['PORT'] ?? '8080');
final server = await serve(handler, ip, port);
print('Server listening on port ${server.port}');
}