diff --git a/src/env.jl b/src/env.jl index 86a593c..665a2f3 100644 --- a/src/env.jl +++ b/src/env.jl @@ -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 diff --git a/src/server.jl b/src/server.jl index f207e41..485fbaa 100644 --- a/src/server.jl +++ b/src/server.jl @@ -1,4 +1,3 @@ - """ run_server(app::DashApp, host = HTTP.Sockets.localhost, port = 8050; debug::Bool = false) @@ -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, diff --git a/src/utils/paths.jl b/src/utils/paths.jl index c3eb023..ca0d493 100644 --- a/src/utils/paths.jl +++ b/src/utils/paths.jl @@ -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`. @@ -42,8 +42,13 @@ 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 @@ -51,6 +56,6 @@ function pathname_configs(url_base_pathname, requests_pathname_prefix, routes_pa 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 \ No newline at end of file diff --git a/test/config_functional.jl b/test/config_functional.jl index 094714e..8935fae 100644 --- a/test/config_functional.jl +++ b/test/config_functional.jl @@ -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("", index_page)) @@ -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("", 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("""""", 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("""""", index_page)) - + @test !isnothing(findfirst("""""", 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 @@ -131,13 +134,13 @@ 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)) @@ -145,9 +148,9 @@ 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( "", @@ -161,7 +164,7 @@ 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( @@ -169,13 +172,13 @@ end "", index_page) ) - + @test !isnothing( findfirst( "", index_page) ) - + @test !isnothing( findfirst( Dash.format_tag("meta", Dict("type" => "tst", "rel" => "r"), opened = true), @@ -183,7 +186,7 @@ end ) 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( @@ -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( @@ -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 diff --git a/test/env.jl b/test/env.jl index b88f9fa..ddf26f7 100644 --- a/test/env.jl +++ b/test/env.jl @@ -10,7 +10,7 @@ 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" @@ -18,12 +18,12 @@ using Dash:dash_env, @env_default! @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 @@ -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 \ No newline at end of file