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

Add --bind to specify serving address with relaxed default (0.0.0.0) #475

Merged
merged 4 commits into from
Jun 3, 2024
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
18 changes: 15 additions & 3 deletions Sources/CartonFrontend/Commands/CartonFrontendDevCommand.swift
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,25 @@ struct CartonFrontendDevCommand: AsyncParsableCommand {
@Flag(name: .shortAndLong, help: "Don't clear terminal window after files change.")
var verbose = false

@Option(
name: .shortAndLong,
help: """
Set the address where the development server will listen for connections.
"""
)
var bind: String = "0.0.0.0"

@Option(name: .shortAndLong, help: "Set the HTTP port the development server will run on.")
var port = 8080

@Option(
name: .shortAndLong,
help: "Set the location where the development server will run. Default is `127.0.0.1`."
help: """
Set the location where the development server will run.
The default value is derived from the –-bind option.
"""
)
var host = "127.0.0.1"
var host: String?

@Flag(name: .long, help: "Skip automatically opening app in system browser.")
var skipAutoOpen = false
Expand Down Expand Up @@ -150,8 +161,9 @@ struct CartonFrontendDevCommand: AsyncParsableCommand {
mainWasmPath: AbsolutePath(
validating: mainWasmPath, relativeTo: localFileSystem.currentWorkingDirectory!),
verbose: verbose,
bindingAddress: bind,
port: port,
host: host,
host: Server.Configuration.host(bindOption: bind, hostOption: host),
customIndexPath: customIndexPage.map {
try AbsolutePath(validating: $0, relativeTo: localFileSystem.currentWorkingDirectory!)
},
Expand Down
18 changes: 15 additions & 3 deletions Sources/CartonFrontend/Commands/CartonFrontendTestCommand.swift
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,14 @@ struct CartonFrontendTestCommand: AsyncParsableCommand {
@Option(help: "Turn on runtime checks for various behavior.")
private var sanitize: SanitizeVariant?

@Option(
name: .shortAndLong,
help: """
Set the address where the development server will listen for connections.
"""
)
var bind: String = "0.0.0.0"

@Option(
name: .shortAndLong,
help: "Set the HTTP port the testing server will run on for browser environment."
Expand All @@ -64,9 +72,12 @@ struct CartonFrontendTestCommand: AsyncParsableCommand {

@Option(
name: .shortAndLong,
help: "Set the location where the testing server will run. Default is `127.0.0.1`."
help: """
Set the location where the development server will run.
The default value is derived from the –-bind option.
"""
)
var host = "127.0.0.1"
var host: String?

@Option(help: "Use the given bundle instead of building the test target")
var prebuiltTestBundlePath: String
Expand Down Expand Up @@ -117,7 +128,8 @@ struct CartonFrontendTestCommand: AsyncParsableCommand {
case .browser:
try await BrowserTestRunner(
testFilePath: bundlePath,
host: host,
bindingAddress: bind,
host: Server.Configuration.host(bindOption: bind, hostOption: host),
port: port,
headless: headless,
resourcesPaths: resources,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ enum BrowserTestRunnerError: Error, CustomStringConvertible {

struct BrowserTestRunner: TestRunner {
let testFilePath: AbsolutePath
let bindingAddress: String
let host: String
let port: Int
let headless: Bool
Expand All @@ -57,13 +58,15 @@ struct BrowserTestRunner: TestRunner {

init(
testFilePath: AbsolutePath,
bindingAddress: String,
host: String,
port: Int,
headless: Bool,
resourcesPaths: [String],
terminal: InteractiveWriter
) {
self.testFilePath = testFilePath
self.bindingAddress = bindingAddress
self.host = host
self.port = port
self.headless = headless
Expand All @@ -77,6 +80,7 @@ struct BrowserTestRunner: TestRunner {
builder: nil,
mainWasmPath: testFilePath,
verbose: true,
bindingAddress: bindingAddress,
port: port,
host: host,
customIndexPath: nil,
Expand Down
11 changes: 10 additions & 1 deletion Sources/CartonKit/Server/Server.swift
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ public actor Server {
let builder: BuilderProtocol?
let mainWasmPath: AbsolutePath
let verbose: Bool
let bindingAddress: String
let port: Int
let host: String
let customIndexPath: AbsolutePath?
Expand All @@ -146,6 +147,7 @@ public actor Server {
builder: BuilderProtocol?,
mainWasmPath: AbsolutePath,
verbose: Bool,
bindingAddress: String,
port: Int,
host: String,
customIndexPath: AbsolutePath?,
Expand All @@ -156,13 +158,20 @@ public actor Server {
self.builder = builder
self.mainWasmPath = mainWasmPath
self.verbose = verbose
self.bindingAddress = bindingAddress
self.port = port
self.host = host
self.customIndexPath = customIndexPath
self.resourcesPaths = resourcesPaths
self.entrypoint = entrypoint
self.terminal = terminal
}

public static func host(bindOption: String, hostOption: String?) -> String {
if let hostOption { return hostOption }
if bindOption == "0.0.0.0" { return "127.0.0.1" }
return bindOption
}
}

public init(
Expand Down Expand Up @@ -304,7 +313,7 @@ public actor Server {
}
// Enable SO_REUSEADDR for the accepted Channels
.childChannelOption(ChannelOptions.socketOption(.so_reuseaddr), value: 1)
.bind(host: configuration.host, port: configuration.port)
.bind(host: configuration.bindingAddress, port: configuration.port)
.get()

self.serverChannel = channel
Expand Down