Skip to content
Closed
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
11 changes: 9 additions & 2 deletions src/Browzarr.jl
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,23 @@ end

const SERVERS = Dict{Int, BrowzarrServer}()
const SERVERS_LOCK = ReentrantLock()
const DEFAULT_PORT = 3000
const DEFAULT_AUTOPORT = true # automatically pick next port if current in use

include("mimeTypes.jl")
include("servers.jl")

function browzarr(; port::Int = 3000, open::Union{Bool, Nothing} = nothing, store::Union{String, Nothing} = nothing)
function browzarr(;
port::Integer = DEFAULT_PORT,
autoport::Bool = DEFAULT_AUTOPORT,
open::Union{Bool, Nothing} = nothing,
store::Union{String, Nothing} = nothing
)
notebook = in_notebook()
vscode = in_vscode()
open_browser_flag = isnothing(open) ? !(notebook || vscode) : open

srv = start_browzarr(; port, store)
srv = start_browzarr(; port, autoport, store)

if notebook && !open_browser_flag
display("text/html", browzarr_iframe(srv))
Expand Down
24 changes: 20 additions & 4 deletions src/servers.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,22 @@
function start_browzarr(; port::Int = 3000, host::String = "127.0.0.1", store::Union{String, Nothing} = nothing)
function start_browzarr(;
port::Integer = DEFAULT_PORT,
autoport::Bool = DEFAULT_AUTOPORT,
host::String = "127.0.0.1",
store::Union{String, Nothing} = nothing
)
return lock(SERVERS_LOCK) do
haskey(SERVERS, port) && error("Server already running on port $port")

# If autoport is enabled, keep incrementing the port number until we find a free one
while autoport && haskey(SERVERS, port)
@warn "Port $port is already in use, trying next port..."
port += 1
end
Comment on lines +10 to +13

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The current implementation only checks if the port is present in the internal SERVERS dictionary. It does not check if the port is already bound by another process on the system. To more robustly find an 'open' port as the PR title suggests, you might want to handle potential 'address in use' errors during the HTTP.serve! call or use a utility to check system-level port availability.


# If autoport is disabled and the port is already in use, throw an error
if !autoport && haskey(SERVERS, port)
error("Server already running on port $port")
end

# The `browzarr` npm tarball unpacks under `package/` and the static build lives in `out/`.
dir = joinpath(artifact"Browzarr", "package", "out")
handler = static_handler(dir, store)
Expand Down Expand Up @@ -29,7 +45,7 @@ function stop!(srv::BrowzarrServer)
return @info "Browzarr server stopped" port = srv.port
end

function stop!(port::Int)
function stop!(port::Integer)
srv = lock(SERVERS_LOCK) do
get(SERVERS, port, nothing)
end
Expand Down Expand Up @@ -60,7 +76,7 @@ running_servers() = lock(SERVERS_LOCK) do
collect(values(SERVERS))
end

get_server(port::Int) = lock(SERVERS_LOCK) do
get_server(port::Integer) = lock(SERVERS_LOCK) do
get(SERVERS, port, nothing)
end

Expand Down
Loading