https://github.com/pnxtech/hydradart
Hydra for Dartlang - A light-weight library for building distributed applications such as microservices.
https://github.com/pnxtech/hydradart
Last synced: 9 months ago
JSON representation
Hydra for Dartlang - A light-weight library for building distributed applications such as microservices.
- Host: GitHub
- URL: https://github.com/pnxtech/hydradart
- Owner: pnxtech
- License: mit
- Created: 2022-05-06T00:26:40.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2022-05-24T17:13:40.000Z (over 3 years ago)
- Last Synced: 2025-04-11T04:06:05.488Z (9 months ago)
- Language: Dart
- Size: 260 KB
- Stars: 0
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# HydraDart
Hydra for [Dartlang](https://dart.dev) - A light-weight library for building distributed applications such as microservices.

## Prior work
Hydra for Dart is based on the Hydra approach to building microservices.
See related repos at: https://github.com/pnxtech
This implementation for Dart is especially based on HydraPy for Python.
## Example
In this example HydraDart uses [Dart Shelf](https://pub.dev/packages/shelf) and [Dart Self Router](https://pub.dev/packages/shelf_router) to allow a Dart service to be discoverable within a Docker Swarm or Kubernetes cluster.
```dart
import 'dart:io';
import 'dart:convert';
import 'package:shelf_router/shelf_router.dart';
import 'package:shelf/shelf.dart';
import 'package:shelf/shelf_io.dart' as io;
import './hydra.dart';
class API {
helloHandler(Request request) {
return Response.ok('hello-world');
}
userHandler(Request request, String user) {
return Response.ok('hello $user');
}
}
void main(List args) async {
var hydra = Hydra();
var router = Router();
var api = API();
hydra.bindRouter(router);
hydra.addRoute('/v1/dart', 'get', api.helloHandler);
hydra.addRoute('/v1/dart/user/', 'get', api.userHandler);
// Load configuration file
File configFile = File('./configs/dart-svcs-config.json');
Future futureContent = configFile.readAsString();
futureContent.then((config) async {
Map configMap = jsonDecode(config);
// Use any available host or container IP (usually `0.0.0.0`).
final ip = InternetAddress.anyIPv4;
// Configure a pipeline that logs requests.
final routerHandler =
Pipeline().addMiddleware(logRequests()).addHandler(router);
// For running in containers, we respect the PORT environment variable.
final port = configMap['hydra']['servicePort'];
final server = await io.serve(routerHandler, ip, port);
print('Server listening on port ${server.port}');
hydra.init(configMap);
});
}
```
In the example above our server application loads a `dart-svcs-config.json` file and passes it along to the `hydra.init()` member function.
```json
{
"hydra": {
"serviceName": "dart-svcs",
"serviceIP": "",
"servicePort": 7134,
"serviceType": "test",
"serviceDescription": "Dart experimental service",
"plugins": {
"hydraLogger": {
"logToConsole": true,
"onlyLogLocally": false
}
},
"redis": {
"urlxxx": "redis://redis:6379/15",
"host": "redis",
"port": 6379,
"db": 15
}
}
}
```