Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add new get_server methods to facilitate getting URL within App #272

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
27 changes: 15 additions & 12 deletions src/server-defaults.jl
Original file line number Diff line number Diff line change
Expand Up @@ -178,16 +178,19 @@ end

get_server() = get_server(find_proxy_in_environment())

function get_server(proxy_callback)
# we have found nothing, so don't do any fancy setup
if isnothing(proxy_callback)
return singleton_server()
else
url = proxy_callback(8888) # call it with any port, to see if its empty and therefore no proxy is found/needed
listen_url = isempty(url) ? "127.0.0.1" : "0.0.0.0" # we only need to serve on 0.0.0.0, if we proxy
server = singleton_server(; listen_url=listen_url)
real_port = server.port # can change if already in use
server.proxy_url = proxy_callback(real_port) # which is why the url needs to be a callbacks
return server
end
# we have found nothing, so don't do any fancy setup
get_server(::Nothing) = singleton_server()

function get_server(proxy_callback::Function)
url = proxy_callback(8888) # call it with any port, to see if its empty and therefore no proxy is found/needed
listen_url = isempty(url) ? "127.0.0.1" : "0.0.0.0" # we only need to serve on 0.0.0.0, if we proxy
server = singleton_server(; listen_url=listen_url)
real_port = server.port # can change if already in use
server.proxy_url = proxy_callback(real_port) # which is why the url needs to be a callbacks
return server
end

get_server(session::Session) = get_server(session.asset_server)
get_server(server::HTTPAssetServer) = server.server
get_server(server::Bonito.ChildAssetServer) = get_server(server.parent)
get_server(any) = error("Not an HTTP server for session")
7 changes: 7 additions & 0 deletions test/server.jl
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,10 @@ using RelocatableFolders
@test isfile(Bonito.serving_target(asset))
@test read(Bonito.serving_target(asset)) isa Vector{UInt8}
end

@testset "get online URL" begin
App() do session::Session
server = get_server(session)
DOM.div(string(Bonito.online_url(server, "")))
end
end
Loading