Skip to content

Commit bba2bcb

Browse files
authored
Merge pull request #97 from plotly/host_port_enviroment
HOST, PORT and DASH_APP_NAME enviroments
2 parents daca40d + 90f00c0 commit bba2bcb

File tree

5 files changed

+93
-71
lines changed

5 files changed

+93
-71
lines changed

src/env.jl

+9-7
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,22 @@
1-
dash_env_key(name::String) = "DASH_" * uppercase(name)
1+
const DASH_ENV_PREFIX = "DASH_"
22

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

5-
function dash_env(t::Type{T}, name::String, default = nothing) where {T<:Number}
6-
key = dash_env_key(name)
7-
!haskey(ENV, key) && return default
5+
dash_env(name::String, default = nothing; prefix = DASH_ENV_PREFIX) = get(ENV, dash_env_key(name, prefix = prefix), default)
6+
7+
function dash_env(t::Type{T}, name::String, default = nothing; prefix = DASH_ENV_PREFIX) where {T<:Number}
8+
key = dash_env_key(name, prefix = prefix)
9+
!haskey(ENV, key) && return default
810
return parse(T, lowercase(get(ENV, key, "")))
911
end
1012

11-
dash_env(t::Type{String}, name::String, default = nothing) = dash_env(name, default)
13+
dash_env(t::Type{String}, name::String, default = nothing; prefix = DASH_ENV_PREFIX) = dash_env(name, default, prefix = prefix)
1214

1315

1416
macro env_default!(name, type = String, default = nothing)
1517
name_str = string(name)
1618
return esc(:(
17-
$name = isnothing($name) ?
19+
$name = isnothing($name) ?
1820
dash_env($type, $name_str, $default)
1921
:
2022
$name

src/server.jl

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
"""
32
run_server(app::DashApp, host = HTTP.Sockets.localhost, port = 8050; debug::Bool = false)
43
@@ -22,7 +21,9 @@ julia> run_server(handler, HTTP.Sockets.localhost, 8050)
2221
```
2322
2423
"""
25-
function run_server(app::DashApp, host = HTTP.Sockets.localhost, port = 8050;
24+
function run_server(app::DashApp,
25+
host = dash_env("HOST", "127.0.0.1", prefix = ""),
26+
port = dash_env(Int64, "PORT", 8050, prefix = "");
2627
debug = nothing,
2728
dev_tools_ui = nothing,
2829
dev_tools_props_check = nothing,

src/utils/paths.jl

+10-5
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@ end
55

66
function app_root_path()
77
prog_path = program_path()
8-
8+
99
return isnothing(prog_path) ? pwd() : prog_path
1010
end
1111

1212
function pathname_configs(url_base_pathname, requests_pathname_prefix, routes_pathname_prefix)
13-
13+
1414
raise_error = (s) -> error("""
1515
$s This is ambiguous.
1616
To fix this, set `routes_pathname_prefix` instead of `url_base_pathname`.
@@ -42,15 +42,20 @@ function pathname_configs(url_base_pathname, requests_pathname_prefix, routes_pa
4242

4343
!startswith(routes_pathname_prefix, "/") && error("routes_pathname_prefix` needs to start with `/`")
4444
!endswith(routes_pathname_prefix, "/") && error("routes_pathname_prefix` needs to end with `/`")
45-
46-
if isnothing(requests_pathname_prefix)
45+
46+
app_name = dash_env("APP_NAME")
47+
48+
49+
if isnothing(requests_pathname_prefix) && !isnothing(app_name)
50+
requests_pathname_prefix = "/" * app_name * routes_pathname_prefix
51+
elseif isnothing(requests_pathname_prefix)
4752
requests_pathname_prefix = routes_pathname_prefix
4853
end
4954

5055
!startswith(requests_pathname_prefix, "/") &&
5156
error("requests_pathname_prefix` needs to start with `/`")
5257
!endswith(requests_pathname_prefix, routes_pathname_prefix) &&
5358
error("requests_pathname_prefix` needs to end with `routes_pathname_prefix`")
54-
59+
5560
return (url_base_pathname, requests_pathname_prefix, routes_pathname_prefix)
5661
end

test/config_functional.jl

+52-49
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,15 @@ using HTTP
55

66

77

8-
@testset "external_stylesheets" begin
8+
@testset "external_stylesheets" begin
99
app = dash()
10-
resources = ApplicationResources(app, main_registry())
10+
resources = ApplicationResources(app, main_registry())
1111
index_page = Dash.index_page(app, resources)
12-
12+
1313
@test isnothing(findfirst("link rel=\"stylesheet\"", index_page))
14-
14+
1515
app = dash(external_stylesheets = ["https://test.css"])
16-
resources = ApplicationResources(app, main_registry())
16+
resources = ApplicationResources(app, main_registry())
1717
index_page = Dash.index_page(app, resources)
1818
@test !isnothing(findfirst("<link rel=\"stylesheet\" href=\"https://test.css\">", index_page))
1919

@@ -22,54 +22,57 @@ using HTTP
2222
Dict("href" => "https://test2.css", "rel" => "stylesheet")
2323
]
2424
)
25-
resources = ApplicationResources(app, main_registry())
25+
resources = ApplicationResources(app, main_registry())
2626
index_page = Dash.index_page(app, resources)
27-
27+
2828
@test !isnothing(findfirst("<link rel=\"stylesheet\" href=\"https://test.css\">", index_page))
2929
@test !isnothing(findfirst("href=\"https://test2.css\"", index_page))
30-
30+
3131
end
3232

3333
@testset "external_scripts" begin
34-
35-
34+
35+
3636
app = dash(external_scripts = ["https://test.js"])
37-
resources = ApplicationResources(app, main_registry())
37+
resources = ApplicationResources(app, main_registry())
3838
index_page = Dash.index_page(app, resources)
3939
@test !isnothing(findfirst("""<script src="https://test.js"></script>""", index_page))
4040

4141
app = dash(external_scripts = [
42-
"https://test.js",
42+
"https://test.js",
4343
Dict("src" => "https://test2.js", "crossorigin" => "anonymous")
4444
])
45-
resources = ApplicationResources(app, main_registry())
45+
resources = ApplicationResources(app, main_registry())
4646
index_page = Dash.index_page(app, resources)
4747

4848
@test !isnothing(findfirst("""<script src="https://test.js"></script>""", index_page))
49-
49+
5050
@test !isnothing(findfirst("""<script src="https://test2.js" crossorigin="anonymous"></script>""", index_page))
5151

5252
end
5353

5454
@testset "url paths" begin
55-
#=app = dash(requests_pathname_prefix = "/reg/prefix/", routes_pathname_prefix = "/prefix/")
56-
resources = ApplicationResources(app, main_registry())
57-
index_page = Dash.index_page(app, resources)
58-
59-
@test !isnothing(findfirst("""requests_pathname_prefix":"/reg/prefix/""", index_page))
60-
handler = Dash.make_handler(app)
61-
request = HTTP.Request("GET", "/prefix/")
62-
response = handler(request)
63-
@test response.status == 200
64-
65-
request = HTTP.Request("GET", "/prefix/_dash-layout")
66-
response = handler(request)
67-
@test response.status == 200
68-
69-
request = HTTP.Request("GET", "/prefix/_dash-dependencies")
70-
response = handler(request)
71-
@test response.status == 200=#
72-
55+
app = dash(requests_pathname_prefix = "/reg/prefix/", routes_pathname_prefix = "/prefix/")
56+
@test app.config.requests_pathname_prefix == "/reg/prefix/"
57+
@test app.config.routes_pathname_prefix == "/prefix/"
58+
59+
app = dash(routes_pathname_prefix = "/prefix/")
60+
@test app.config.routes_pathname_prefix == "/prefix/"
61+
@test app.config.requests_pathname_prefix == "/prefix/"
62+
63+
app = dash()
64+
@test app.config.routes_pathname_prefix == "/"
65+
@test app.config.requests_pathname_prefix == "/"
66+
67+
ENV["DASH_APP_NAME"] = "test-app"
68+
app = dash(routes_pathname_prefix = "/prefix/")
69+
@test app.config.routes_pathname_prefix == "/prefix/"
70+
@test app.config.requests_pathname_prefix == "/test-app/prefix/"
71+
72+
app = dash()
73+
@test app.config.routes_pathname_prefix == "/"
74+
@test app.config.requests_pathname_prefix == "/test-app/"
75+
7376
end
7477

7578
@testset "assets paths" begin
@@ -131,23 +134,23 @@ end
131134

132135
@testset "suppress_callback_exceptions" begin
133136
app = dash()
134-
resources = ApplicationResources(app, main_registry())
137+
resources = ApplicationResources(app, main_registry())
135138
index_page = Dash.index_page(app, resources)
136139
@test !isnothing(findfirst("\"suppress_callback_exceptions\":false", index_page))
137140
@test isnothing(findfirst("\"suppress_callback_exceptions\":true", index_page))
138141

139142
app = dash(suppress_callback_exceptions = true)
140-
resources = ApplicationResources(app, main_registry())
143+
resources = ApplicationResources(app, main_registry())
141144
index_page = Dash.index_page(app, resources)
142145
@test isnothing(findfirst("\"suppress_callback_exceptions\":false", index_page))
143146
@test !isnothing(findfirst("\"suppress_callback_exceptions\":true", index_page))
144147
end
145148

146149
@testset "meta_tags" begin
147150
app = dash()
148-
resources = ApplicationResources(app, main_registry())
151+
resources = ApplicationResources(app, main_registry())
149152
index_page = Dash.index_page(app, resources)
150-
153+
151154
@test !isnothing(
152155
findfirst(
153156
"<meta http-equiv=\"X-UA-Compatible\" content=\"IE=edge\">",
@@ -161,29 +164,29 @@ end
161164
)
162165

163166
app = dash(meta_tags = [Dict("type" => "tst", "rel" => "r")])
164-
resources = ApplicationResources(app, main_registry())
167+
resources = ApplicationResources(app, main_registry())
165168
index_page = Dash.index_page(app, resources)
166169

167170
@test !isnothing(
168171
findfirst(
169172
"<meta http-equiv=\"X-UA-Compatible\" content=\"IE=edge\">",
170173
index_page)
171174
)
172-
175+
173176
@test !isnothing(
174177
findfirst(
175178
"<meta charset=\"UTF-8\">",
176179
index_page)
177180
)
178-
181+
179182
@test !isnothing(
180183
findfirst(
181184
Dash.format_tag("meta", Dict("type" => "tst", "rel" => "r"), opened = true),
182185
index_page)
183186
)
184187

185188
app = dash(meta_tags = [Dict("charset" => "Win1251"), Dict("type" => "tst", "rel" => "r")])
186-
resources = ApplicationResources(app, main_registry())
189+
resources = ApplicationResources(app, main_registry())
187190
index_page = Dash.index_page(app, resources)
188191

189192
@test isnothing(
@@ -204,7 +207,7 @@ end
204207
)
205208

206209
app = dash(meta_tags = [Dict("http-equiv" => "X-UA-Compatible", "content" => "IE"), Dict("type" => "tst", "rel" => "r")])
207-
resources = ApplicationResources(app, main_registry())
210+
resources = ApplicationResources(app, main_registry())
208211
index_page = Dash.index_page(app, resources)
209212
@test isnothing(
210213
findfirst(
@@ -222,23 +225,23 @@ end
222225
@testset "index_string" begin
223226
index_string = "test test test, {%metas%},{%title%},{%favicon%},{%css%},{%app_entry%},{%config%},{%scripts%},{%renderer%}"
224227
app = dash(index_string = index_string)
225-
resources = ApplicationResources(app, main_registry())
228+
resources = ApplicationResources(app, main_registry())
226229
index_page = Dash.index_page(app, resources)
227-
@test startswith(index_page, "test test test,")
228-
230+
@test startswith(index_page, "test test test,")
231+
229232
end
230233

231234
@testset "show_undo_redo" begin
232-
235+
233236
app = dash()
234-
235-
resources = ApplicationResources(app, main_registry())
237+
238+
resources = ApplicationResources(app, main_registry())
236239
index_page = Dash.index_page(app, resources)
237240
@test !isnothing(findfirst("\"show_undo_redo\":false", index_page))
238241

239242
app = dash(show_undo_redo = true)
240-
241-
resources = ApplicationResources(app, main_registry())
243+
244+
resources = ApplicationResources(app, main_registry())
242245
index_page = Dash.index_page(app, resources)
243246
@test !isnothing(findfirst("\"show_undo_redo\":true", index_page))
244247
end

test/env.jl

+19-8
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,20 @@ using Dash:dash_env, @env_default!
1010
ENV["DASH_STRING_TEST"] = "test_path"
1111
@test dash_env("string_test", "aaaa") == "test_path"
1212
@test_throws ArgumentError dash_env(Int, "string_test", "aaaa") == "test_path"
13-
13+
1414
string_test = nothing
1515
@env_default! string_test
1616
@test string_test == "test_path"
1717
string_test = "aaaa"
1818
@env_default! string_test
1919
@test string_test == "aaaa"
2020

21-
21+
2222

2323
ENV["DASH_INT_TEST"] = "100"
2424
@test dash_env("int_test", "aaaa") == "100"
2525
@test dash_env(Int, "int_test", 50) == 100
26-
26+
2727
int_test = nothing
2828
@env_default! int_test Int
2929
@test int_test == 100
@@ -34,15 +34,26 @@ using Dash:dash_env, @env_default!
3434
@env_default! int_test2 Int 40
3535
@test int_test2 == 40
3636

37-
ENV["DASH_BOOL_TEST"] = "1"
38-
@test dash_env(Bool, "bool_test", 50) == true
37+
ENV["DASH_BOOL_TEST"] = "1"
38+
@test dash_env(Bool, "bool_test", 50) == true
3939

40-
ENV["DASH_BOOL_TEST"] = "0"
40+
ENV["DASH_BOOL_TEST"] = "0"
4141
@test dash_env(Bool, "bool_test", 50) == false
4242

43-
ENV["DASH_BOOL_TEST"] = "TRUE"
43+
ENV["DASH_BOOL_TEST"] = "TRUE"
4444
@test dash_env(Bool, "bool_test", 50) == true
4545

46-
ENV["DASH_BOOL_TEST"] = "FALSE"
46+
ENV["DASH_BOOL_TEST"] = "FALSE"
4747
@test dash_env(Bool, "bool_test", 50) == false
4848
end
49+
50+
@testset "prefixes" begin
51+
ENV["DASH_HOST"] = "localhost"
52+
@test dash_env("host") == "localhost"
53+
@test isnothing(dash_env("host", prefix = ""))
54+
55+
@test dash_env(Int64, "port", 8050, prefix = "") == 8050
56+
ENV["PORT"] = "2001"
57+
@test isnothing(dash_env(Int64, "port"))
58+
@test dash_env(Int64, "port", prefix = "") == 2001
59+
end

0 commit comments

Comments
 (0)