From d0ff39e6e406ba24ae66f90d95be39db8de3aa3a Mon Sep 17 00:00:00 2001 From: Gautam Mishra Date: Wed, 9 Sep 2020 23:55:31 +0530 Subject: [PATCH] add launbrowser on run (#190) Co-authored-by: Fons van der Plas --- Project.toml | 2 +- src/webserver/Configuration.jl | 13 +++++----- src/webserver/WebServer.jl | 45 ++++++++++++++++++++++++++++------ 3 files changed, 46 insertions(+), 14 deletions(-) diff --git a/Project.toml b/Project.toml index edfda328fd..becc9a1867 100644 --- a/Project.toml +++ b/Project.toml @@ -2,7 +2,7 @@ name = "Pluto" uuid = "c3e4b0f8-55cb-11ea-2926-15256bba5781" license = "MIT" authors = ["Fons van der Plas ", "Mikołaj Bochenski "] -version = "0.11.13" +version = "0.11.14" [deps] Base64 = "2a0f44e3-6c83-55bd-87e4-b1978d98bd5f" diff --git a/src/webserver/Configuration.jl b/src/webserver/Configuration.jl index a2b7c4dac9..bd324857ec 100644 --- a/src/webserver/Configuration.jl +++ b/src/webserver/Configuration.jl @@ -4,13 +4,14 @@ end # ServerSecurity(val::Bool) = ServerSecurity(val, val, val) -# THIS IS NOT USED YET +"(More options coming...)" Base.@kwdef struct ServerConfiguration - host::AbstractString = "127.0.0.1" - port::Integer = 1234 - skip_main_menu::Bool = false - show_file_system::Bool = true + root_url::Union{Nothing,String} = nothing + # host::AbstractString = "127.0.0.1" + # port::Union{Nothing,Integer} = nothing + launch_browser::Bool = true + # single_notebook_mode::Union{Nothing,Notebook} = nothing + # show_file_system::Bool = true end -# ServerSecurity(val::Bool) = ServerSecurity(val) diff --git a/src/webserver/WebServer.jl b/src/webserver/WebServer.jl index f8e1a58396..f376dbb014 100644 --- a/src/webserver/WebServer.jl +++ b/src/webserver/WebServer.jl @@ -23,6 +23,32 @@ function HTTP.closebody(http::HTTP.Stream{HTTP.Messages.Request,S}) where S <: I end end +# from https://github.com/JuliaLang/julia/pull/36425 +function detectwsl() + Sys.islinux() && + isfile("/proc/sys/kernel/osrelease") && + occursin(r"Microsoft|WSL"i, read("/proc/sys/kernel/osrelease", String)) +end + +function open_in_default_browser(url::AbstractString)::Bool + try + if Sys.isapple() + Base.run(`open $url`) + true + elseif Sys.iswindows() || detectwsl() + Base.run(`cmd.exe /s /c start "" /b $url`) + true + elseif Sys.islinux() + Base.run(`xdg-open $url`) + true + else + false + end + catch ex + showerror(stderr, ex, catch_backtrace()) + false + end +end """ run([host,] port=1234[; kwargs...]) @@ -36,18 +62,18 @@ Start Pluto! Are you excited? I am! ## Kwargs -- `launchbrowser`: launch browser directly. (not implemented yet) +- `configuration`: specifiy the `Pluto.ServerConfiguration` to change the HTTP server behavior. - `session`: specifiy the `Pluto.ServerSession` to run the web server on. - `security`: specifiy the `Pluto.ServerSecurity` options for the web server. -Different configurations are possible by creating a custom [`ServerSession`](@ref) or [`ServerSecurity`](@ref) object. Have a look at their documentation. +Different configurations are possible by creating a custom [`ServerConfiguration`](@ref), [`ServerSession`](@ref) or [`ServerSecurity`](@ref) object. Have a look at their documentation. ## Technobabble This will start the static HTTP server and a WebSocket server. The server runs _synchronously_ (i.e. blocking call) on `http://[host]:[port]/`. Pluto notebooks can be started from the main menu in the web browser. """ -function run(host, port::Union{Nothing,Integer}=nothing; launchbrowser::Bool=false, session=ServerSession(), security=ServerSecurity(true)) +function run(host, port::Union{Nothing,Integer}=nothing; configuration=ServerConfiguration(), session=ServerSession(), security=ServerSecurity(true)) pluto_router = http_router_for(session, security) hostIP = parse(Sockets.IPAddr, host) @@ -163,18 +189,23 @@ function run(host, port::Union{Nothing,Integer}=nothing; launchbrowser::Bool=fal end end - address = let + address = if configuration.root_url === nothing hostPretty = (hostStr = string(hostIP)) == "127.0.0.1" ? "localhost" : hostStr portPretty = Int(port) "http://$(hostPretty):$(portPretty)/" + else + configuration.root_url end Sys.set_process_title("Pluto server - $address") - println("Go to $address to start writing ~ have fun!") + + if configuration.launch_browser && open_in_default_browser(address) + println("Opening $address in your default browser... ~ have fun!") + else + println("Go to $address in your browser to start writing ~ have fun!") + end println() println("Press Ctrl+C in this terminal to stop Pluto") println() - - launchbrowser && @warn "Not implemented yet" kill_server[] = () -> @sync begin println("\n\nClosing Pluto... Restart Julia for a fresh session. \n\nHave a nice day! 🎈")