Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added http serve #1

Merged
merged 1 commit into from
Oct 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();
}
}