Skip to content

HOST, PORT and DASH_APP_NAME enviroments #97

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

Merged
merged 3 commits into from
Apr 16, 2021
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
16 changes: 9 additions & 7 deletions src/env.jl
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
dash_env_key(name::String) = "DASH_" * uppercase(name)
const DASH_ENV_PREFIX = "DASH_"

dash_env(name::String, default = nothing) = get(ENV, dash_env_key(name), default)
dash_env_key(name::String; prefix = DASH_ENV_PREFIX) = prefix * uppercase(name)

function dash_env(t::Type{T}, name::String, default = nothing) where {T<:Number}
key = dash_env_key(name)
!haskey(ENV, key) && return default
dash_env(name::String, default = nothing; prefix = DASH_ENV_PREFIX) = get(ENV, dash_env_key(name, prefix = prefix), default)

function dash_env(t::Type{T}, name::String, default = nothing; prefix = DASH_ENV_PREFIX) where {T<:Number}
key = dash_env_key(name, prefix = prefix)
!haskey(ENV, key) && return default
return parse(T, lowercase(get(ENV, key, "")))
end

dash_env(t::Type{String}, name::String, default = nothing) = dash_env(name, default)
dash_env(t::Type{String}, name::String, default = nothing; prefix = DASH_ENV_PREFIX) = dash_env(name, default, prefix = prefix)


macro env_default!(name, type = String, default = nothing)
name_str = string(name)
return esc(:(
$name = isnothing($name) ?
$name = isnothing($name) ?
dash_env($type, $name_str, $default)
:
$name
Expand Down
5 changes: 3 additions & 2 deletions src/server.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

"""
run_server(app::DashApp, host = HTTP.Sockets.localhost, port = 8050; debug::Bool = false)

Expand All @@ -22,7 +21,9 @@ julia> run_server(handler, HTTP.Sockets.localhost, 8050)
```

"""
function run_server(app::DashApp, host = HTTP.Sockets.localhost, port = 8050;
function run_server(app::DashApp,
host = dash_env("HOST", "127.0.0.1", prefix = ""),
port = dash_env(Int64, "PORT", 8050, prefix = "");
debug = nothing,
dev_tools_ui = nothing,
dev_tools_props_check = nothing,
Expand Down
15 changes: 10 additions & 5 deletions src/utils/paths.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ end

function app_root_path()
prog_path = program_path()

return isnothing(prog_path) ? pwd() : prog_path
end

function pathname_configs(url_base_pathname, requests_pathname_prefix, routes_pathname_prefix)

raise_error = (s) -> error("""
$s This is ambiguous.
To fix this, set `routes_pathname_prefix` instead of `url_base_pathname`.
Expand Down Expand Up @@ -42,15 +42,20 @@ function pathname_configs(url_base_pathname, requests_pathname_prefix, routes_pa

!startswith(routes_pathname_prefix, "/") && error("routes_pathname_prefix` needs to start with `/`")
!endswith(routes_pathname_prefix, "/") && error("routes_pathname_prefix` needs to end with `/`")

if isnothing(requests_pathname_prefix)

app_name = dash_env("APP_NAME")


if isnothing(requests_pathname_prefix) && !isnothing(app_name)
requests_pathname_prefix = "/" * app_name * routes_pathname_prefix
elseif isnothing(requests_pathname_prefix)
requests_pathname_prefix = routes_pathname_prefix
end

!startswith(requests_pathname_prefix, "/") &&
error("requests_pathname_prefix` needs to start with `/`")
!endswith(requests_pathname_prefix, routes_pathname_prefix) &&
error("requests_pathname_prefix` needs to end with `routes_pathname_prefix`")

return (url_base_pathname, requests_pathname_prefix, routes_pathname_prefix)
end
101 changes: 52 additions & 49 deletions test/config_functional.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ using HTTP



@testset "external_stylesheets" begin
@testset "external_stylesheets" begin
app = dash()
resources = ApplicationResources(app, main_registry())
resources = ApplicationResources(app, main_registry())
index_page = Dash.index_page(app, resources)

@test isnothing(findfirst("link rel=\"stylesheet\"", index_page))

app = dash(external_stylesheets = ["https://test.css"])
resources = ApplicationResources(app, main_registry())
resources = ApplicationResources(app, main_registry())
index_page = Dash.index_page(app, resources)
@test !isnothing(findfirst("<link rel=\"stylesheet\" href=\"https://test.css\">", index_page))

Expand All @@ -22,54 +22,57 @@ using HTTP
Dict("href" => "https://test2.css", "rel" => "stylesheet")
]
)
resources = ApplicationResources(app, main_registry())
resources = ApplicationResources(app, main_registry())
index_page = Dash.index_page(app, resources)

@test !isnothing(findfirst("<link rel=\"stylesheet\" href=\"https://test.css\">", index_page))
@test !isnothing(findfirst("href=\"https://test2.css\"", index_page))

end

@testset "external_scripts" begin


app = dash(external_scripts = ["https://test.js"])
resources = ApplicationResources(app, main_registry())
resources = ApplicationResources(app, main_registry())
index_page = Dash.index_page(app, resources)
@test !isnothing(findfirst("""<script src="https://test.js"></script>""", index_page))

app = dash(external_scripts = [
"https://test.js",
"https://test.js",
Dict("src" => "https://test2.js", "crossorigin" => "anonymous")
])
resources = ApplicationResources(app, main_registry())
resources = ApplicationResources(app, main_registry())
index_page = Dash.index_page(app, resources)

@test !isnothing(findfirst("""<script src="https://test.js"></script>""", index_page))

@test !isnothing(findfirst("""<script src="https://test2.js" crossorigin="anonymous"></script>""", index_page))

end

@testset "url paths" begin
#=app = dash(requests_pathname_prefix = "/reg/prefix/", routes_pathname_prefix = "/prefix/")
resources = ApplicationResources(app, main_registry())
index_page = Dash.index_page(app, resources)

@test !isnothing(findfirst("""requests_pathname_prefix":"/reg/prefix/""", index_page))
handler = Dash.make_handler(app)
request = HTTP.Request("GET", "/prefix/")
response = handler(request)
@test response.status == 200

request = HTTP.Request("GET", "/prefix/_dash-layout")
response = handler(request)
@test response.status == 200

request = HTTP.Request("GET", "/prefix/_dash-dependencies")
response = handler(request)
@test response.status == 200=#

app = dash(requests_pathname_prefix = "/reg/prefix/", routes_pathname_prefix = "/prefix/")
@test app.config.requests_pathname_prefix == "/reg/prefix/"
@test app.config.routes_pathname_prefix == "/prefix/"

app = dash(routes_pathname_prefix = "/prefix/")
@test app.config.routes_pathname_prefix == "/prefix/"
@test app.config.requests_pathname_prefix == "/prefix/"

app = dash()
@test app.config.routes_pathname_prefix == "/"
@test app.config.requests_pathname_prefix == "/"

ENV["DASH_APP_NAME"] = "test-app"
app = dash(routes_pathname_prefix = "/prefix/")
@test app.config.routes_pathname_prefix == "/prefix/"
@test app.config.requests_pathname_prefix == "/test-app/prefix/"

app = dash()
@test app.config.routes_pathname_prefix == "/"
@test app.config.requests_pathname_prefix == "/test-app/"

end

@testset "assets paths" begin
Expand Down Expand Up @@ -131,23 +134,23 @@ end

@testset "suppress_callback_exceptions" begin
app = dash()
resources = ApplicationResources(app, main_registry())
resources = ApplicationResources(app, main_registry())
index_page = Dash.index_page(app, resources)
@test !isnothing(findfirst("\"suppress_callback_exceptions\":false", index_page))
@test isnothing(findfirst("\"suppress_callback_exceptions\":true", index_page))

app = dash(suppress_callback_exceptions = true)
resources = ApplicationResources(app, main_registry())
resources = ApplicationResources(app, main_registry())
index_page = Dash.index_page(app, resources)
@test isnothing(findfirst("\"suppress_callback_exceptions\":false", index_page))
@test !isnothing(findfirst("\"suppress_callback_exceptions\":true", index_page))
end

@testset "meta_tags" begin
app = dash()
resources = ApplicationResources(app, main_registry())
resources = ApplicationResources(app, main_registry())
index_page = Dash.index_page(app, resources)

@test !isnothing(
findfirst(
"<meta http-equiv=\"X-UA-Compatible\" content=\"IE=edge\">",
Expand All @@ -161,29 +164,29 @@ end
)

app = dash(meta_tags = [Dict("type" => "tst", "rel" => "r")])
resources = ApplicationResources(app, main_registry())
resources = ApplicationResources(app, main_registry())
index_page = Dash.index_page(app, resources)

@test !isnothing(
findfirst(
"<meta http-equiv=\"X-UA-Compatible\" content=\"IE=edge\">",
index_page)
)

@test !isnothing(
findfirst(
"<meta charset=\"UTF-8\">",
index_page)
)

@test !isnothing(
findfirst(
Dash.format_tag("meta", Dict("type" => "tst", "rel" => "r"), opened = true),
index_page)
)

app = dash(meta_tags = [Dict("charset" => "Win1251"), Dict("type" => "tst", "rel" => "r")])
resources = ApplicationResources(app, main_registry())
resources = ApplicationResources(app, main_registry())
index_page = Dash.index_page(app, resources)

@test isnothing(
Expand All @@ -204,7 +207,7 @@ end
)

app = dash(meta_tags = [Dict("http-equiv" => "X-UA-Compatible", "content" => "IE"), Dict("type" => "tst", "rel" => "r")])
resources = ApplicationResources(app, main_registry())
resources = ApplicationResources(app, main_registry())
index_page = Dash.index_page(app, resources)
@test isnothing(
findfirst(
Expand All @@ -222,23 +225,23 @@ end
@testset "index_string" begin
index_string = "test test test, {%metas%},{%title%},{%favicon%},{%css%},{%app_entry%},{%config%},{%scripts%},{%renderer%}"
app = dash(index_string = index_string)
resources = ApplicationResources(app, main_registry())
resources = ApplicationResources(app, main_registry())
index_page = Dash.index_page(app, resources)
@test startswith(index_page, "test test test,")
@test startswith(index_page, "test test test,")

end

@testset "show_undo_redo" begin

app = dash()
resources = ApplicationResources(app, main_registry())

resources = ApplicationResources(app, main_registry())
index_page = Dash.index_page(app, resources)
@test !isnothing(findfirst("\"show_undo_redo\":false", index_page))

app = dash(show_undo_redo = true)
resources = ApplicationResources(app, main_registry())

resources = ApplicationResources(app, main_registry())
index_page = Dash.index_page(app, resources)
@test !isnothing(findfirst("\"show_undo_redo\":true", index_page))
end
27 changes: 19 additions & 8 deletions test/env.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,20 @@ using Dash:dash_env, @env_default!
ENV["DASH_STRING_TEST"] = "test_path"
@test dash_env("string_test", "aaaa") == "test_path"
@test_throws ArgumentError dash_env(Int, "string_test", "aaaa") == "test_path"

string_test = nothing
@env_default! string_test
@test string_test == "test_path"
string_test = "aaaa"
@env_default! string_test
@test string_test == "aaaa"



ENV["DASH_INT_TEST"] = "100"
@test dash_env("int_test", "aaaa") == "100"
@test dash_env(Int, "int_test", 50) == 100

int_test = nothing
@env_default! int_test Int
@test int_test == 100
Expand All @@ -34,15 +34,26 @@ using Dash:dash_env, @env_default!
@env_default! int_test2 Int 40
@test int_test2 == 40

ENV["DASH_BOOL_TEST"] = "1"
@test dash_env(Bool, "bool_test", 50) == true
ENV["DASH_BOOL_TEST"] = "1"
@test dash_env(Bool, "bool_test", 50) == true

ENV["DASH_BOOL_TEST"] = "0"
ENV["DASH_BOOL_TEST"] = "0"
@test dash_env(Bool, "bool_test", 50) == false

ENV["DASH_BOOL_TEST"] = "TRUE"
ENV["DASH_BOOL_TEST"] = "TRUE"
@test dash_env(Bool, "bool_test", 50) == true

ENV["DASH_BOOL_TEST"] = "FALSE"
ENV["DASH_BOOL_TEST"] = "FALSE"
@test dash_env(Bool, "bool_test", 50) == false
end

@testset "prefixes" begin
ENV["DASH_HOST"] = "localhost"
@test dash_env("host") == "localhost"
@test isnothing(dash_env("host", prefix = ""))

@test dash_env(Int64, "port", 8050, prefix = "") == 8050
ENV["PORT"] = "2001"
@test isnothing(dash_env(Int64, "port"))
@test dash_env(Int64, "port", prefix = "") == 2001
end