buildGateway function
Builds a reverse-proxy / gateway OmnyHub from a declarative config map
(typically parsed from JSON by the omnyhub CLI).
Schema:
{
"listen": [
{ "protocol": "http", "address": "0.0.0.0", "port": 8080 },
{ "protocol": "https", "port": 8443, "cert": "c.pem", "key": "k.pem" },
{ "protocol": "https", "port": 443,
"letsencrypt": { "domains": [{"name":"x","email":"y"}],
"cacheDir": "/var/omnyhub/certs", "production": false } }
],
"routes": [
{ "path": "/api", "target": "http://localhost:9000", "stripPrefix": "/api" },
{ "host": "drive.example.com", "target": "http://localhost:9001", "priority": 10 }
]
}
Implementation
Future<OmnyHub> buildGateway(
Map<String, dynamic> config, {
Logger logger = const NoopLogger(),
}) async {
final transports = <Transport>[];
for (final entry in _list(config, 'listen')) {
transports.add(_buildTransport(Json.asObject(entry, 'listen entry')));
}
if (transports.isEmpty) {
throw const ValidationException('config.listen must not be empty');
}
final hub = OmnyHub(transports: transports, logger: logger);
var index = 0;
for (final entry in _list(config, 'routes')) {
final route = Json.asObject(entry, 'route');
final target = Json.requireString(route, 'target');
final name = Json.optString(route, 'name') ?? 'route-${index++}';
final proxy = ProxyService(
Upstream.uri(target),
name: name,
stripPrefix: Json.optString(route, 'stripPrefix'),
);
await hub.route(
_buildRule(route),
proxy,
priority: Json.optInt(route, 'priority', 0)!,
);
}
return hub;
}