-
Notifications
You must be signed in to change notification settings - Fork 12
/
server2_shelf_mirrored_for_client_test.dart
54 lines (44 loc) · 1.7 KB
/
server2_shelf_mirrored_for_client_test.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import 'package:jsonrpc2/src/server_base.dart';
import 'package:mirror_dispatcher/mirror_dispatcher.dart';
import 'package:shelf_cors_headers/shelf_cors_headers.dart';
import 'package:shelf_router/shelf_router.dart';
import 'package:shelf/shelf.dart';
import 'package:shelf/shelf_io.dart' as shelf_io;
import 'rpc_methods.dart';
final port = 8394;
///
/// Test server for test_client.dart. Uses dart:mirrors.
///
class Service {
/// APIs are in reflected_rpc_methods.dart
Router get handler {
final jRpcHeader = {'Content-Type': 'application/json; charset=utf-8'};
final router = Router();
router.post('/friend/<friendName>',
(Request request, String friendName) async {
var dispatcher = MirrorDispatcher(Friend(friendName));
var body = await request.readAsString();
return Response.ok(await jsonRpc(body, dispatcher), headers: jRpcHeader);
});
router.post('/sum', (Request request) async {
return Response.ok(
await jsonRpc(await request.readAsString(),
MirrorDispatcher(ExampleMethodsClass())),
headers: jRpcHeader);
});
// router.post('/', (Request request) async {
// var dispatcher = MirrorDispatcher(ExampleMethodsClass());
// var body = await request.readAsString();
// return Response.ok(await jsonRpc(body, dispatcher), headers: jRpcHeader);
// });
return router;
}
}
void main() async {
final service = Service();
// corsHeaders is needed for chrome client tests
var handler =
const Pipeline().addMiddleware(corsHeaders()).addHandler(service.handler);
final server = await shelf_io.serve(handler, '127.0.0.1', port);
print('Server running on localhost:${server.port}');
}