Skip to content

Commit

Permalink
Add base_url option
Browse files Browse the repository at this point in the history
  • Loading branch information
Pangoraw committed Jul 25, 2022
1 parent 80a9b07 commit 22be5d0
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 2 deletions.
4 changes: 4 additions & 0 deletions src/Configuration.jl
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ function __init__()
end

const ROOT_URL_DEFAULT = nothing
const BASE_URL_DEFAULT = "/"
const HOST_DEFAULT = "127.0.0.1"
const PORT_DEFAULT = nothing
const PORT_HINT_DEFAULT = 1234
Expand Down Expand Up @@ -74,6 +75,7 @@ The HTTP server options. See [`SecurityOptions`](@ref) for additional settings.
"""
@option mutable struct ServerOptions
root_url::Union{Nothing,String} = ROOT_URL_DEFAULT
base_url::String = BASE_URL_DEFAULT
host::String = HOST_DEFAULT
port::Union{Nothing,Integer} = PORT_DEFAULT
port_hint::Integer = PORT_HINT_DEFAULT
Expand Down Expand Up @@ -218,6 +220,7 @@ end

function from_flat_kwargs(;
root_url::Union{Nothing,String} = ROOT_URL_DEFAULT,
base_url::String = BASE_URL_DEFAULT,
host::String = HOST_DEFAULT,
port::Union{Nothing,Integer} = PORT_DEFAULT,
port_hint::Integer = PORT_HINT_DEFAULT,
Expand Down Expand Up @@ -252,6 +255,7 @@ function from_flat_kwargs(;
)
server = ServerOptions(;
root_url,
base_url,
host,
port,
port_hint,
Expand Down
28 changes: 27 additions & 1 deletion src/webserver/Static.jl
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,27 @@ function is_authenticated(session::ServerSession, request::HTTP.Request)
# that ) || ( kind of looks like Krabs from spongebob
end

"""
scoped_router(base_url::String, base_router::HTTP.Router)::HTTP.Router
Returns a new `HTTP.Router` which delegates all requests to `base_router` but with requests trimmed
so that they seem like they arrived at `/**` instead of `/\$base_url/**`.
"""
function scoped_router(base_url, base_router)
@assert startswith(base_url, '/') && endswith(base_url, '/') "Invalid base_url \"$base_url\""

function handler(request)
request.target = request.target[length(base_url):end]
return base_router(request)
end

router = HTTP.Router(base_router._404, base_router._405)
HTTP.register!(router, base_url * "**", handler)
HTTP.register!(router, base_url, handler)

return router
end

function http_router_for(session::ServerSession)
router = HTTP.Router()
security = session.options.security
Expand Down Expand Up @@ -263,7 +284,7 @@ function http_router_for(session::ServerSession)
)
end
HTTP.register!(router, "GET", "/sample/*", serve_sample)
HTTP.register!(router, "POST", "/sample/*", serve_sample)
HTTP.register!(router, "POST","/sample/*", serve_sample)

notebook_from_uri(request) = let
uri = HTTP.URI(request.target)
Expand Down Expand Up @@ -348,5 +369,10 @@ function http_router_for(session::ServerSession)
HTTP.register!(router, "GET", "/**", serve_asset)
HTTP.register!(router, "GET", "/favicon.ico", create_serve_onefile(project_relative_path(frontend_directory(allow_bundled=false), "img", "favicon.ico")))

base_url = session.options.server.base_url
if base_url != "/"
return scoped_router(base_url, router)
end

return router
end
3 changes: 2 additions & 1 deletion src/webserver/WebServer.jl
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,8 @@ function pretty_address(session::ServerSession, hostIP, port)
host_str
end
port_pretty = Int(port)
"http://$(host_pretty):$(port_pretty)/"
base_url = session.options.server.base_url
"http://$(host_pretty):$(port_pretty)$(base_url)"
end

url_params = Dict{String,String}()
Expand Down

0 comments on commit 22be5d0

Please sign in to comment.