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

This commit is contained in:
omochimetaru 2024-06-03 15:07:03 +08:00 committed by GitHub
parent 305c204495
commit b8048c4830
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 44 additions and 7 deletions

View File

@ -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
@ -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!)
},

View File

@ -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."
@ -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
@ -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,

View File

@ -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
@ -57,6 +58,7 @@ struct BrowserTestRunner: TestRunner {
init(
testFilePath: AbsolutePath,
bindingAddress: String,
host: String,
port: Int,
headless: Bool,
@ -64,6 +66,7 @@ struct BrowserTestRunner: TestRunner {
terminal: InteractiveWriter
) {
self.testFilePath = testFilePath
self.bindingAddress = bindingAddress
self.host = host
self.port = port
self.headless = headless
@ -77,6 +80,7 @@ struct BrowserTestRunner: TestRunner {
builder: nil,
mainWasmPath: testFilePath,
verbose: true,
bindingAddress: bindingAddress,
port: port,
host: host,
customIndexPath: nil,

View File

@ -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?
@ -146,6 +147,7 @@ public actor Server {
builder: BuilderProtocol?,
mainWasmPath: AbsolutePath,
verbose: Bool,
bindingAddress: String,
port: Int,
host: String,
customIndexPath: AbsolutePath?,
@ -156,6 +158,7 @@ public actor Server {
self.builder = builder
self.mainWasmPath = mainWasmPath
self.verbose = verbose
self.bindingAddress = bindingAddress
self.port = port
self.host = host
self.customIndexPath = customIndexPath
@ -163,6 +166,12 @@ public actor Server {
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(
@ -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