-
Notifications
You must be signed in to change notification settings - Fork 190
/
Configuration.jl
executable file
·321 lines (250 loc) · 9.99 KB
/
Configuration.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
"""
Core genie configuration / settings functionality.
"""
module Configuration
import Pkg
import Dates
"""
pkginfo(pkg::String)
Returns installed package information for `pkg`
"""
pkginfo(pkg::String) = filter(x -> x.name == pkg && x.is_direct_dep, values(Pkg.dependencies()) |> collect)
"""
const GENIE_VERSION
References the current Genie version number.
"""
const GENIE_VERSION = try
pkginfo("Genie")[1].version
catch _
v"1"
end
import Logging
import Genie
import MbedTLS
export isdev, isprod, istest, env
export Settings, DEV, PROD, TEST
# app environments
const DEV = "dev"
const PROD = "prod"
const TEST = "test"
haskey(ENV, "GENIE_ENV") || (ENV["GENIE_ENV"] = DEV)
haskey(ENV, "HOST") || (ENV["HOST"] = "127.0.0.1")
"""
isdev() :: Bool
Set of utility functions that return whether or not the current environment is development, production or testing.
# Examples
```julia
julia> Configuration.isdev()
true
julia> Configuration.isprod()
false
```
"""
isdev() :: Bool = (Genie.config.app_env == DEV)
"""
isprod() :: Bool
Set of utility functions that return whether or not the current environment is development, production or testing.
# Examples
```julia
julia> Configuration.isdev()
true
julia> Configuration.isprod()
false
```
"""
isprod():: Bool = (Genie.config.app_env == PROD)
"""
istest() :: Bool
Set of utility functions that return whether or not the current environment is development, production or testing.
# Examples
```julia
julia> Configuration.isdev()
true
julia> Configuration.isprod()
false
```
"""
istest():: Bool = (Genie.config.app_env == TEST)
"""
env() :: String
Returns the current Genie environment.
# Examples
```julia
julia> Configuration.env()
"dev"
```
"""
env() :: String = Genie.config.app_env
"""
buildpath() :: String
Constructs the temp dir where Genie's view files are built.
"""
buildpath() :: String = Base.Filesystem.mktempdir(prefix = "jl_genie_build_")
"""
mutable struct Settings
App configuration - sets up the app's defaults. Individual options are overwritten in the corresponding environment file.
# Arguments
- `server_port::Int`: the port for running the web server (default 8000)
- `server_host::String`: the host for running the web server (default "127.0.0.1")
- `server_document_root::String`: path to the document root (default "public/")
- `server_handle_static_files::Bool`: if `true`, Genie will also serve static files. In production, it is recommended to serve static files with a web server like Nginx.
- `server_signature::String`: Genie's signature used for tagging the HTTP responses. If empty, it will not be added.
- `app_env::String`: the environment in which the app is running (dev, test, or prod)
- `cors_headers::Dict{String,String}`: default `Access-Control-*` CORS settings
- `cors_allowed_origins::Vector{String}`: allowed origin hosts for CORS settings
- `cache_duraction::Int`: cache expiration time in seconds
- `log_level::Logging.LogLevel`: logging severity level
- `log_to_file::Bool`: if true, information will be logged to file besides REPL
- `assets_fingerprinted::Bool`: if true, asset fingerprinting is used in the asset pipeline
- `session_key_name::String`: the name of the session cookie
- `session_storage::Symbol`: the backend adapter for session storage (default File)
- `inflector_irregulars::Vector{Tuple{String,String}}`: additional irregular singular-plural forms to be used by the Inflector
- `run_as_server::Bool`: when true the server thread is launched synchronously to avoid that the script exits
- `websockets_server::Bool`: if true, the websocket server is also started together with the web server
- `html_parser_close_tag::String`: default " /". Can be changed to an empty string "" so the single tags would not be closed.
- `ssl_enabled::Bool`: default false. Server runs over SSL/HTTPS in development.
- `ssl_config::MbedTLS.SSLConfig`: default `nothing`. If not `nothing` and `ssl_enabled`, it will use the config to start the server over HTTPS.
"""
mutable struct Settings
server_port::Int
server_host::String
server_document_root::String
server_handle_static_files::Bool
server_signature::String
app_env::String
cors_headers::Dict{String,String}
cors_allowed_origins::Vector{String}
cache_duration::Int
cache_storage::Union{Symbol,Nothing}
log_level::Logging.LogLevel
log_to_file::Bool
log_requests::Bool
assets_fingerprinted::Bool
inflector_irregulars::Vector{Tuple{String,String}}
run_as_server::Bool
websockets_server::Bool
websockets_port::Int
initializers_folder::String
path_config::String
path_env::String
path_app::String
path_resources::String
path_lib::String
path_helpers::String
path_log::String
path_tasks::String
path_build::String
path_plugins::String
path_cache::String
path_initializers::String
path_db::String
path_bin::String
path_src::String
webchannels_default_route::String
webchannels_js_file::String
webchannels_subscribe_channel::String
webchannels_unsubscribe_channel::String
webchannels_autosubscribe::Bool
webthreads_default_route::String
webthreads_js_file::String
webthreads_pull_route::String
webthreads_push_route::String
webthreads_connection_threshold::Dates.Millisecond
html_parser_close_tag::String
html_parser_char_at::String
html_parser_char_dot::String
html_parser_char_column::String
html_parser_char_dash::String
ssl_enabled::Bool
ssl_config::Union{MbedTLS.SSLConfig,Nothing}
session_key_name::String
session_storage::Union{Symbol,Nothing}
session_options::Dict{String,Any}
base_path::String
Settings(;
server_port = (haskey(ENV, "PORT") ? parse(Int, ENV["PORT"]) : 8000), # default port for binding the web server
server_host = ENV["HOST"],
server_document_root = "public",
server_handle_static_files = true,
server_signature = "Genie/$GENIE_VERSION/Julia/$VERSION",
app_env = ENV["GENIE_ENV"],
cors_headers = Dict{String,String}(
"Access-Control-Allow-Origin" => "", # ex: "*" or "http://mozilla.org"
"Access-Control-Expose-Headers" => "", # ex: "X-My-Custom-Header, X-Another-Custom-Header"
"Access-Control-Max-Age" => "86400", # 24 hours
"Access-Control-Allow-Credentials" => "", # "true" or "false"
"Access-Control-Allow-Methods" => "", # ex: "POST, GET"
"Access-Control-Allow-Headers" => "", # ex: "X-PINGOTHER, Content-Type"
),
cors_allowed_origins = String[],
cache_duration = 0,
cache_storage = nothing,
log_level = Logging.Debug,
log_to_file = false,
log_requests = true,
assets_fingerprinted = false,
inflector_irregulars = Tuple{String,String}[],
run_as_server = false,
websockets_server = false,
websockets_port = server_port,
initializers_folder = "initializers",
path_config = "config",
path_env = joinpath(path_config, "env"),
path_app = "app",
path_resources = joinpath(path_app, "resources"),
path_lib = "lib",
path_helpers = joinpath(path_app, "helpers"),
path_log = "log",
path_tasks = "tasks",
path_build = buildpath(),
path_plugins = "plugins",
path_cache = "cache",
path_initializers = joinpath(path_config, initializers_folder),
path_db = "db",
path_bin = "bin",
path_src = "src",
webchannels_default_route = "__",
webchannels_js_file = "channels.js",
webchannels_subscribe_channel = "subscribe",
webchannels_unsubscribe_channel = "unsubscribe",
webchannels_autosubscribe = true,
webthreads_default_route = "__",
webthreads_js_file = "webthreads.js",
webthreads_pull_route = "pull",
webthreads_push_route = "push",
webthreads_connection_threshold = Dates.Millisecond(60_000), # 1 minute
html_parser_close_tag = " /",
html_parser_char_at = "!!",
html_parser_char_dot = "!",
html_parser_char_column = "!",
html_parser_char_dash = "__",
ssl_enabled = false,
ssl_config = nothing,
session_key_name = "__geniesid",
session_storage = nothing,
session_options = Dict("Path" => "/", "HttpOnly" => true, "Secure" => ssl_enabled),
base_path = "/"
) =
new(
server_port, server_host,
server_document_root, server_handle_static_files, server_signature,
app_env,
cors_headers, cors_allowed_origins,
cache_duration, cache_storage,
log_level, log_to_file, log_requests,
assets_fingerprinted,
inflector_irregulars,
run_as_server,
websockets_server, websockets_port,
initializers_folder,
path_config, path_env, path_app, path_resources, path_lib, path_helpers, path_log, path_tasks, path_build,
path_plugins, path_cache, path_initializers, path_db, path_bin, path_src,
webchannels_default_route, webchannels_js_file, webchannels_subscribe_channel, webchannels_unsubscribe_channel, webchannels_autosubscribe,
webthreads_default_route, webthreads_js_file, webthreads_pull_route, webthreads_push_route, webthreads_connection_threshold,
html_parser_close_tag, html_parser_char_at, html_parser_char_dot, html_parser_char_column, html_parser_char_dash,
ssl_enabled, ssl_config,
session_key_name, session_storage, session_options,
base_path
)
end
end