Skip to content

Commit

Permalink
Add --bind to specify serving address with relaxed default (0.0.0.0) (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
omochi committed Jun 3, 2024
1 parent 305c204 commit b8048c4
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 7 deletions.
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

0 comments on commit b8048c4

Please sign in to comment.