Skip to content

Commit

Permalink
fixup! Implement ExposedThing functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
JKRhb committed Aug 15, 2024
1 parent 0a59efe commit 6606886
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 8 deletions.
8 changes: 7 additions & 1 deletion example/exposed_thing/http_server.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,13 @@ String property = "hi :)";
void main() async {
final servient = Servient.create(
clientFactories: [HttpClientFactory()],
servers: [HttpServer(HttpConfig(port: 3000))],
servers: [
HttpServer(
HttpConfig(
port: 3000,
),
),
],
);

final wot = await servient.start();
Expand Down
18 changes: 15 additions & 3 deletions lib/src/binding_http/http_config.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,28 @@
//
// SPDX-License-Identifier: BSD-3-Clause

import "dart:io";

/// Allows for configuring the behavior of HTTP clients and servers.
class HttpConfig {
/// Creates a new [HttpConfig] object.
HttpConfig({this.port, this.secure});
HttpConfig({
int? port,
this.secure = false,
InternetAddress? bindAddress,
}) : port = port ?? (secure ? 443 : 80),
bindAddress = bindAddress ?? InternetAddress.anyIPv4;

/// Custom port number that should be used by a server.
///
/// Defaults to 80 for HTTP and 443 for HTTPS.
int? port;
final int port;

/// Indicates if the client or server should use HTTPS.
bool? secure;
bool secure;

/// The IP address the HTTP server should bind to.
///
/// Defaults to [InternetAddress.anyIPv4].
final InternetAddress bindAddress;
}
9 changes: 5 additions & 4 deletions lib/src/binding_http/http_server.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,17 @@ final class HttpServer implements ProtocolServer {
HttpServer(HttpConfig? httpConfig)
// TODO(JKRhb): Check if the scheme should be determined differently.
: scheme = httpConfig?.secure ?? false ? "https" : "http",
port = _portFromConfig(httpConfig);
port = _portFromConfig(httpConfig),
bindAddress = httpConfig?.bindAddress ?? io.InternetAddress.anyIPv4;

@override
final String scheme;

@override
final int port;

// FIXME
final Object _bindAddress = io.InternetAddress.loopbackIPv4;
/// The [io.InternetAddress] this server is bound to.
final io.InternetAddress bindAddress;

io.HttpServer? _server;

Expand Down Expand Up @@ -219,7 +220,7 @@ final class HttpServer implements ProtocolServer {
throw StateError("Server already started");
}

_server = await shelf_io.serve(_handleRequest, _bindAddress, port);
_server = await shelf_io.serve(_handleRequest, bindAddress, port);

_servient = servient;
}
Expand Down

0 comments on commit 6606886

Please sign in to comment.