From 24a3c5a9602d01b1047c0e66a029627f02bc2705 Mon Sep 17 00:00:00 2001 From: Adriano Santos Date: Mon, 9 Oct 2023 19:00:23 -0300 Subject: [PATCH] Added http serve --- example/spawn_dart_example.dart | 2 ++ lib/src/handler.dart | 56 --------------------------------- lib/src/service.dart | 40 +++++++++++++++++++++++ lib/src/spawn_dart_base.dart | 32 +++++++++++++++++-- 4 files changed, 71 insertions(+), 59 deletions(-) delete mode 100644 lib/src/handler.dart create mode 100644 lib/src/service.dart diff --git a/example/spawn_dart_example.dart b/example/spawn_dart_example.dart index 5b48b4c..9640fab 100644 --- a/example/spawn_dart_example.dart +++ b/example/spawn_dart_example.dart @@ -2,4 +2,6 @@ import 'package:spawn_dart/spawn_dart.dart'; void main() { var spawnSystem = SpawnSystem(); + + spawnSystem.start(); } diff --git a/lib/src/handler.dart b/lib/src/handler.dart deleted file mode 100644 index 026fdb5..0000000 --- a/lib/src/handler.dart +++ /dev/null @@ -1,56 +0,0 @@ -import 'dart:convert'; -import 'dart:io'; - -import 'package:shelf/shelf.dart'; -import 'package:shelf/shelf_io.dart' as shelf_io; -import 'package:shelf_router/shelf_router.dart' as shelf_router; -import 'package:spawn_dart/src/protocol/eigr/functions/protocol/actors/protocol.pb.dart'; - -class HttpServerController { - static const _headers = { - 'content-type': 'application/octet-stream', - }; - - Future start() async { - final port = int.parse(Platform.environment['PORT'] ?? '8080'); - - final cascade = Cascade().add(_router); - - final server = await shelf_io.serve( - logRequests().addHandler(cascade.handler), - InternetAddress.anyIPv4, - port, - ); - - print('Serving at http://${server.address.host}:${server.port}'); - - // Used for tracking uptime of the server. - _watch.start(); - } - -// Router instance to handler requests. - final _router = shelf_router.Router() - ..post('/api/v1/actors/actions', _actorHandler); - - String _jsonEncode(Object? data) => - const JsonEncoder.withIndent(' ').convert(data); - - Response _actorHandler(Request request) { - ActorInvocation actorInvocationRequest = - ActorInvocation.fromBuffer(request.read().toList() as List); - - return Response.ok( - _jsonEncode({}), - headers: { - ..._headers, - }, - ); - } - - final _watch = Stopwatch(); - - final _dartVersion = () { - final version = Platform.version; - return version.substring(0, version.indexOf(' ')); - }(); -} diff --git a/lib/src/service.dart b/lib/src/service.dart new file mode 100644 index 0000000..5ffadc7 --- /dev/null +++ b/lib/src/service.dart @@ -0,0 +1,40 @@ +import 'dart:io'; +import 'dart:typed_data'; + +import 'package:shelf/shelf.dart'; +import 'package:shelf_router/shelf_router.dart'; +import 'package:spawn_dart/src/protocol/eigr/functions/protocol/actors/protocol.pb.dart'; + +class Service { + static const _headers = { + 'content-type': 'application/octet-stream', + }; + + final _dartVersion = () { + final version = Platform.version; + return version.substring(0, version.indexOf(' ')); + }(); + + Handler get handler { + final router = Router(); + + router.post('/api/v1/actors/actions', (Request request) async { + ActorInvocation actorInvocationRequest = + ActorInvocation.fromBuffer(request.read().toList() as List); + + ActorInvocationResponse response = ActorInvocationResponse.getDefault(); + response.actorName = 'test'; + + Uint8List actorInvocationResp = response.writeToBuffer(); + + return Response.ok( + actorInvocationResp, + headers: { + ..._headers, + }, + ); + }); + + return router; + } +} diff --git a/lib/src/spawn_dart_base.dart b/lib/src/spawn_dart_base.dart index a2b8c98..737ae88 100644 --- a/lib/src/spawn_dart_base.dart +++ b/lib/src/spawn_dart_base.dart @@ -1,16 +1,42 @@ +import 'dart:io'; + import 'package:logger/logger.dart'; +import 'package:shelf/shelf.dart'; +import 'package:shelf/shelf_io.dart' as shelf_io; +import 'package:spawn_dart/src/service.dart'; + class SpawnSystem { final _logger = Logger( printer: LogfmtPrinter(), ); - late int port; - late String host; + final _watch = Stopwatch(); + + late int serverPort = int.parse(Platform.environment['PORT'] ?? '8080'); + + SpawnSystem port(int port) { + serverPort = port; + return this; + } void registerActor() { _logger.d('Registering Actor...'); } - Future start() async {} + Future start() async { + //final port = int.parse(Platform.environment['PORT'] ?? '8080'); + final controller = Service(); + + final server = await shelf_io.serve( + logRequests().addHandler(controller.handler), + InternetAddress.anyIPv4, + serverPort, + ); + + print('Serving at http://${server.address.host}:${server.port}'); + + // Used for tracking uptime of the server. + _watch.start(); + } }