Skip to content

Commit

Permalink
Cleanup notebook config code and remove janky test
Browse files Browse the repository at this point in the history
  • Loading branch information
fonsp committed Sep 12, 2023
1 parent 72a5847 commit e1bd21c
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 63 deletions.
36 changes: 13 additions & 23 deletions src/webserver/WebServer.jl
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@ function open_in_default_browser(url::AbstractString)::Bool
end
end

isurl(s::String) = startswith(s, "http://") || startswith(s, "https://")

function swallow_exception(f, exception_type::Type{T}) where {T}
try
f()
Expand Down Expand Up @@ -86,18 +84,6 @@ function run(port::Integer; kwargs...)
"
end

# open notebook(s) on startup

open_notebook!(session::ServerSession, notebook::Nothing) = Nothing

open_notebook!(session::ServerSession, notebook::AbstractString) = SessionActions.open(session, notebook)

function open_notebook!(session::ServerSession, notebook::AbstractVector{<:AbstractString})
for nb in notebook
SessionActions.open(session, nb)
end
end


const is_first_run = Ref(true)

Expand Down Expand Up @@ -136,8 +122,14 @@ function run(session::ServerSession)
store_session_middleware = create_session_context_middleware(session)
app = pluto_router |> auth_middleware |> store_session_middleware

notebook_at_startup = session.options.server.notebook
open_notebook!(session, notebook_at_startup)
let n = session.options.server.notebook
SessionActions.open.((session,),
n === nothing ? [] :
n isa AbstractString ? [n] :
n;
run_async=true,
)
end

host = session.options.server.host
hostIP = parse(Sockets.IPAddr, host)
Expand Down Expand Up @@ -319,10 +311,6 @@ function run(session::ServerSession)
end
precompile(run, (ServerSession, HTTP.Handlers.Router{Symbol("##001")}))

get_favorite_notebook(notebook::Nothing) = nothing
get_favorite_notebook(notebook::String) = notebook
get_favorite_notebook(notebook::AbstractVector) = first(notebook)

function pretty_address(session::ServerSession, hostIP, port)
root = if session.options.server.root_url !== nothing
@assert endswith(session.options.server.root_url, "/")
Expand Down Expand Up @@ -352,10 +340,12 @@ function pretty_address(session::ServerSession, hostIP, port)
if session.options.security.require_secret_for_access
url_params["secret"] = session.secret
end
fav_notebook = get_favorite_notebook(session.options.server.notebook)
fav_notebook = let n = session.options.server.notebook
n isa AbstractVector ? (isempty(n) ? nothing : first(n)) : n
end
new_root = if fav_notebook !== nothing
key = isurl(fav_notebook) ? "url" : "path"
url_params[key] = string(fav_notebook)
# since this notebook already started running, this will get redicted to that session
url_params["path"] = string(fav_notebook)
root * "open"
else
root
Expand Down
41 changes: 1 addition & 40 deletions test/Configuration.jl
Original file line number Diff line number Diff line change
Expand Up @@ -126,42 +126,6 @@ end
@async schedule(server_task, InterruptException(); error=true)
end

@testset "Open Notebooks at Startup" begin
port = 1338
host = "localhost"
local_url(suffix) = "http://$host:$port/$suffix"

urls = [
"https://raw.githubusercontent.com/fonsp/Pluto.jl/v0.12.16/sample/Basic.jl",
"https://gist.githubusercontent.com/fonsp/4e164a262a60fc4bdd638e124e629d64/raw/8ffe93c680e539056068456a62dea7bf6b8eb622/basic_pkg_notebook.jl",
]
nbnames = download.(urls)

server_running() = HTTP.get(local_url("favicon.ico")).status == 200 && HTTP.get(local_url("edit")).status == 200

# without notebook at startup
server_task = @async Pluto.run(port=port, launch_browser=false, workspace_use_distributed=false, require_secret_for_access=false, require_secret_for_open_links=false)
@test poll(5) do
server_running()
end
@async schedule(server_task, InterruptException(); error=true)

# with a single notebook at startup
server_task = @async Pluto.run(notebook=first(nbnames), port=port, launch_browser=false, workspace_use_distributed=false, require_secret_for_access=false, require_secret_for_open_links=false)
@test poll(5) do
server_running()
end
@async schedule(server_task, InterruptException(); error=true)

# with multiple notebooks at startup
server_task = @async Pluto.run(notebook=nbnames, port=port, launch_browser=false, workspace_use_distributed=false, require_secret_for_access=false, require_secret_for_open_links=false)
@test poll(5) do
server_running()
end
@async schedule(server_task, InterruptException(); error=true)

end

@testset "disable mimetype via workspace_custom_startup_expr" begin
🍭 = ServerSession()
🍭.options.evaluation.workspace_use_distributed = true
Expand All @@ -183,7 +147,4 @@ end
Pluto.WorkspaceManager.unmake_workspace((🍭, nb))
end

# TODO are the processes closed properly?
# TODO we reuse the same port without awaiting the shutdown of the previous server

end # testset
end

0 comments on commit e1bd21c

Please sign in to comment.