Skip to content

Commit

Permalink
Merge pull request #1 from sleipnir/main
Browse files Browse the repository at this point in the history
Added http serve
  • Loading branch information
sleipnir authored Oct 9, 2023
2 parents f02d0a4 + 24a3c5a commit 0a11f6f
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 59 deletions.
2 changes: 2 additions & 0 deletions example/spawn_dart_example.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,6 @@ import 'package:spawn_dart/spawn_dart.dart';

void main() {
var spawnSystem = SpawnSystem();

spawnSystem.start();
}
56 changes: 0 additions & 56 deletions lib/src/handler.dart

This file was deleted.

40 changes: 40 additions & 0 deletions lib/src/service.dart
Original file line number Diff line number Diff line change
@@ -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<int>);

ActorInvocationResponse response = ActorInvocationResponse.getDefault();
response.actorName = 'test';

Uint8List actorInvocationResp = response.writeToBuffer();

return Response.ok(
actorInvocationResp,
headers: {
..._headers,
},
);
});

return router;
}
}
32 changes: 29 additions & 3 deletions lib/src/spawn_dart_base.dart
Original file line number Diff line number Diff line change
@@ -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<void> start() async {}
Future<void> 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();
}
}

0 comments on commit 0a11f6f

Please sign in to comment.