Skip to content

Commit 0156d70

Browse files
committed
HOST and PORT enviroment variables (#70)
1 parent 4b2f015 commit 0156d70

File tree

3 files changed

+31
-17
lines changed

3 files changed

+31
-17
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,

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)