|
1 | | -import 'package:functions/models.dart'; |
| 1 | +// import 'package:functions/models.dart'; |
2 | 2 |
|
3 | | -import 'rpc_client.g.dart'; |
| 3 | +// import 'rpc_client.g.dart'; |
| 4 | + |
| 5 | +// void main() async { |
| 6 | +// final client = RpcClient('http://localhost:8080'); |
| 7 | +// final r6 = await client.users.details.get('hello', user: User(name: 'world')); |
| 8 | +// } |
| 9 | +import 'dart:convert'; |
| 10 | +import 'package:globe_functions/src/spec/serializer.dart'; |
| 11 | +import 'package:shelf/shelf.dart'; |
| 12 | +import 'package:shelf/shelf_io.dart' as shelf_io; |
| 13 | +import 'package:functions/functions.dart' as i0; |
| 14 | +import 'package:functions/api/users/details.dart' as i1; |
| 15 | +import 'package:functions/api/users.dart' as i2; |
| 16 | +import 'package:functions/models.dart' as i3; |
4 | 17 |
|
5 | 18 | void main() async { |
6 | | - final client = RpcClient('https://example.com'); |
| 19 | + final handler = const Pipeline().addMiddleware(logRequests()).addHandler(_onRequest); |
| 20 | + final server = await shelf_io.serve(handler, 'localhost', 8080); |
| 21 | + print('Serving at http://${server.address.host}:${server.port}'); |
| 22 | +} |
| 23 | + |
| 24 | +Future<Response> _onRequest(Request request) async { |
| 25 | + if (request.method == 'POST' && request.url.path == '_rpc') { |
| 26 | + return _onRpcRequest(request); |
| 27 | + } |
| 28 | + |
| 29 | + return Response.ok('Not found...'); |
| 30 | +} |
| 31 | + |
| 32 | +Future<Response> _onRpcRequest(Request request) async { |
| 33 | + final body = jsonDecode(await request.readAsString()); |
| 34 | + final id = body['id'] as String; |
| 35 | + final named = body['named'] as Map<String, dynamic>; |
| 36 | + final positional = body['positional'] as List<dynamic>; |
| 37 | + |
| 38 | + if (id == 'users.details.get') { |
| 39 | + final param0 = Serializers.instance.deserialize<String>(positional[0]); |
| 40 | + final userParam = named['user'] == null ? null : i0.Person.fromJson(named['user'] as Map<String, dynamic>); |
| 41 | + final result = await i1.get( |
| 42 | + param0, |
| 43 | + user: userParam |
| 44 | + ); |
| 45 | + final serializedResult = result.toJson(); |
| 46 | + return Response.ok( |
| 47 | + jsonEncode({'result': serializedResult, 'error': null}), |
| 48 | + headers: {'content-type': 'application/json'}, |
| 49 | + ); |
| 50 | + } |
| 51 | + |
| 52 | + if (id == 'users.example1') { |
| 53 | + final param0 = positional[0] == null ? null : Serializers.instance.deserialize<String>(positional[0]); |
| 54 | + final result = await i2.example1( |
| 55 | + param0 |
| 56 | + ); |
| 57 | + final serializedResult = result == null ? null : result.toJson(); |
| 58 | + return Response.ok( |
| 59 | + jsonEncode({'result': serializedResult, 'error': null}), |
| 60 | + headers: {'content-type': 'application/json'}, |
| 61 | + ); |
| 62 | + } |
7 | 63 |
|
8 | | - final r5 = await client.users.details(); |
9 | | - final r6 = await client.users.details.get('hello', user: User(name: 'world')); |
| 64 | + if (id == 'users.example2') { |
| 65 | + final result = await i2.example2( |
| 66 | + |
| 67 | + ); |
| 68 | + final serializedResult = result.toJson(); |
| 69 | + return Response.ok( |
| 70 | + jsonEncode({'result': serializedResult, 'error': null}), |
| 71 | + headers: {'content-type': 'application/json'}, |
| 72 | + ); |
| 73 | + } |
| 74 | + |
| 75 | + if (id == 'users.example3') { |
| 76 | + final result = await i2.example3( |
| 77 | + |
| 78 | + ); |
| 79 | + final serializedResult = Serializers.instance.serialize<DateTime>(result); |
| 80 | + return Response.ok( |
| 81 | + jsonEncode({'result': serializedResult, 'error': null}), |
| 82 | + headers: {'content-type': 'application/json'}, |
| 83 | + ); |
| 84 | + } |
| 85 | + |
| 86 | + if (id == 'users.example4') { |
| 87 | + final result = await i2.example4( |
| 88 | + |
| 89 | + ); |
| 90 | + final serializedResult = Serializers.instance.serialize<String>(result); |
| 91 | + return Response.ok( |
| 92 | + jsonEncode({'result': serializedResult, 'error': null}), |
| 93 | + headers: {'content-type': 'application/json'}, |
| 94 | + ); |
| 95 | + } |
| 96 | + |
| 97 | + |
| 98 | + // No matching function found |
| 99 | + return Response.notFound( |
| 100 | + jsonEncode({ |
| 101 | + 'result': null, |
| 102 | + 'error': 'Function not found: $id' |
| 103 | + }), |
| 104 | + headers: {'content-type': 'application/json'}, |
| 105 | + ); |
10 | 106 | } |
| 107 | + |
0 commit comments