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 overridable externalurl(...) #211

Merged
merged 1 commit into from
Sep 2, 2024
Merged
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
1 change: 1 addition & 0 deletions src/context.jl
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ end
middleware_cache :: Dict{String, Function} = Dict{String, Function}()
history :: History = History(1_000_000)
history_lock :: ReentrantLock = ReentrantLock()
external_url :: Ref{Nullable{String}} = Ref{Nullable{String}}(nothing)
end

@kwdef struct Context
Expand Down
20 changes: 14 additions & 6 deletions src/core.jl
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,15 @@ oxygen_title = raw"""

"""

function serverwelcome(host::String, port::Int, docs::Bool, metrics::Bool, parallel::Bool, docspath::String)
function serverwelcome(external_url::String, docs::Bool, metrics::Bool, parallel::Bool, docspath::String)
printstyled(oxygen_title, color=:blue, bold=true)
@info "📦 Version 1.5.12 (2024-06-18)"
@info "✅ Started server: http://$host:$port"
@info "✅ Started server: $external_url"
if docs
@info "📖 Documentation: http://$host:$port$docspath"
@info "📖 Documentation: $external_url$docspath"
end
if docs && metrics
@info "📊 Metrics: http://$host:$port$docspath/metrics"
@info "📊 Metrics: $external_url$docspath/metrics"
end
if parallel
@info "🚀 Running in parallel mode with $(Threads.nthreads()) threads"
Expand All @@ -56,7 +56,7 @@ end


"""
serve(; middleware::Vector=[], handler=stream_handler, host="127.0.0.1", port=8080, async=false, parallel=false, serialize=true, catch_errors=true, docs=true, metrics=true, show_errors=true, show_banner=true, docs_path="/docs", schema_path="/schema", kwargs...)
serve(; middleware::Vector=[], handler=stream_handler, host="127.0.0.1", port=8080, async=false, parallel=false, serialize=true, catch_errors=true, docs=true, metrics=true, show_errors=true, show_banner=true, docs_path="/docs", schema_path="/schema", external_url=nothing, kwargs...)

Start the webserver with your own custom request handler
"""
Expand All @@ -75,8 +75,16 @@ function serve(ctx::Context;
show_banner = true,
docs_path = "/docs",
schema_path = "/schema",
external_url = nothing,
kwargs...) :: Server

# set the external url if it's passed
if !isnothing(external_url)
ctx.service.external_url[] = external_url
else
ctx.service.external_url[] = "http://$host:$port"
end

# overwrite docs & schema paths
ctx.docs.enabled[] = docs
ctx.docs.docspath[] = docs_path
Expand Down Expand Up @@ -240,7 +248,7 @@ function startserver(ctx::Context; host, port, show_banner=false, docs=false, me
docs && setupdocs(ctx)
metrics && setupmetrics(ctx)

show_banner && serverwelcome(host, port, docs, metrics, parallel, ctx.docs.docspath[])
show_banner && serverwelcome(ctx.service.external_url[], docs, metrics, parallel, ctx.docs.docspath[])

# start the HTTP server
ctx.service.server[] = start(preprocesskwargs(kwargs))
Expand Down
12 changes: 12 additions & 0 deletions src/methods.jl
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,18 @@ dynamicfiles(
loadfile::Nullable{Function}=nothing
) = Oxygen.Core.dynamicfiles(CONTEXT[].service.router, folder, mountdir; headers, loadfile)

"""
getexternalurl()

Return the external URL of the service
"""
function getexternalurl()::String
external_url = CONTEXT[].service.external_url[]
if isnothing(external_url)
error("getexternalurl() is only available when the service is running")
end
return external_url
end

internalrequest(req::Oxygen.Request; middleware::Vector=[], metrics::Bool=false, serialize::Bool=true, catch_errors=true) =
Oxygen.Core.internalrequest(CONTEXT[], req; middleware, metrics, serialize, catch_errors)
Expand Down
Loading