Skip to content

Commit

Permalink
internal api consolidation
Browse files Browse the repository at this point in the history
  • Loading branch information
Adrian committed Nov 9, 2019
1 parent fab23ac commit a6d4528
Show file tree
Hide file tree
Showing 18 changed files with 128 additions and 182 deletions.
4 changes: 2 additions & 2 deletions files/new_app/config/initializers/logging.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ function initialize_logging()
date_format = "yyyy-mm-dd HH:MM:SS"

logger = if Genie.config.log_to_file
isdir(Genie.LOG_PATH) || mkpath(Genie.LOG_PATH)
isdir(Genie.config.path_log) || mkpath(Genie.config.path_log)
LoggingExtras.DemuxLogger(
LoggingExtras.FileLogger(joinpath(Genie.LOG_PATH, "$(Genie.config.app_env)-$(Dates.today()).log"), always_flush = true, append = true),
LoggingExtras.FileLogger(joinpath(Genie.config.path_log, "$(Genie.config.app_env)-$(Dates.today()).log"), always_flush = true, append = true),
LoggingExtras.ConsoleLogger(stdout, Genie.config.log_level),
include_current_global = false
)
Expand Down
6 changes: 3 additions & 3 deletions src/App.jl
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ const ASSET_FINGERPRINT = ""
Kickstarts the loading of a Genie app by loading the environment settings.
"""
function bootstrap(context::Union{Module,Nothing} = Genie.default_context(context)) :: Nothing
if haskey(ENV, "GENIE_ENV") && isfile(joinpath(Genie.ENV_PATH, ENV["GENIE_ENV"] * ".jl"))
isfile(joinpath(Genie.ENV_PATH, Genie.GLOBAL_ENV_FILE_NAME)) && Base.include(context, joinpath(Genie.ENV_PATH, Genie.GLOBAL_ENV_FILE_NAME))
isfile(joinpath(Genie.ENV_PATH, ENV["GENIE_ENV"] * ".jl")) && Base.include(context, joinpath(Genie.ENV_PATH, ENV["GENIE_ENV"] * ".jl"))
if haskey(ENV, "GENIE_ENV") && isfile(joinpath(Genie.config.path_env, ENV["GENIE_ENV"] * ".jl"))
isfile(joinpath(Genie.config.path_env, Genie.GLOBAL_ENV_FILE_NAME)) && Base.include(context, joinpath(Genie.config.path_env, Genie.GLOBAL_ENV_FILE_NAME))
isfile(joinpath(Genie.config.path_env, ENV["GENIE_ENV"] * ".jl")) && Base.include(context, joinpath(Genie.config.path_env, ENV["GENIE_ENV"] * ".jl"))
else
ENV["GENIE_ENV"] = Genie.Configuration.DEV
Core.eval(context, Meta.parse("const config = Genie.Configuration.Settings(app_env = Configuration.DEV)"))
Expand Down
3 changes: 0 additions & 3 deletions src/AppServer.jl
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,6 @@ function startup(port::Int = Genie.config.server_port, host::String = Genie.conf
verbose::Bool = false, ratelimit::Union{Rational{Int},Nothing} = nothing,
server::Union{Sockets.TCPServer,Nothing} = nothing) :: ServersCollection

# Create build folders
Genie.config.flax_compile_templates && Flax.create_build_folders()

if Genie.config.websocket_server
SERVERS.websockets = @async HTTP.listen(host, ws_port) do req
if HTTP.WebSockets.is_upgrade(req.message)
Expand Down
53 changes: 37 additions & 16 deletions src/Configuration.jl
Original file line number Diff line number Diff line change
Expand Up @@ -147,15 +147,12 @@ App configuration - sets up the app's defaults. Individual options are overwritt
- `log_views::Bool`: if true, information from the view layer (template building) is logged
- `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
- `tests_force_test_env::Bool`: if true, when running tests, Genie will automatically switch the configuration to the test environment to avoid accidental coruption of dev or prod data
- `session_auto_start::Bool`: if true, a session is automatically started for each request
- `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
- `flax_compile_templates::Bool`: if true, the view templates are compiled and persisted between requests
- `run_as_server::Bool`: when true the server thread is launched synchronously to avoid that the script exits
- `websocket_server::Bool`: if true, the websocket server is also started together with the web server
- `renderer_default_layout_file::Symbol`: default name for the layout file (:app)
"""
mutable struct Settings
server_port::Int
Expand All @@ -181,22 +178,34 @@ mutable struct Settings

assets_fingerprinted::Bool

tests_force_test_env::Bool

session_auto_start::Bool
session_key_name::String
session_storage::Symbol

inflector_irregulars::Vector{Tuple{String,String}}

flax_compile_templates::Bool

run_as_server::Bool

websocket_server::Bool
websocket_port::Int

renderer_default_layout_file::Symbol
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

Settings(;
server_port = (haskey(ENV, "PORT") ? parse(Int, ENV["PORT"]) : 8000), # default port for binding the web server
Expand Down Expand Up @@ -228,22 +237,34 @@ mutable struct Settings

assets_fingerprinted = false,

tests_force_test_env = true,

session_auto_start = false,
session_key_name = "__geniesid",
session_storage = :File,

inflector_irregulars = Tuple{String,String}[],

flax_compile_templates = false,

run_as_server = false,

websocket_server = false,
websocket_port = 8001,

renderer_default_layout_file = :app,
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 = Base.Filesystem.mktempdir(prefix = "jl_genie_build_"),
path_plugins = "plugins",
path_cache = "cache",
path_initializers = joinpath(path_config, initializers_folder),
path_db = "db",
path_bin = "bin",
path_src = "src"
) =
new(
server_port, server_host,
Expand All @@ -253,13 +274,13 @@ mutable struct Settings
cache_adapter, cache_duration,
log_level, log_formatted, log_cache, log_views, log_to_file,
assets_fingerprinted,
tests_force_test_env,
session_auto_start, session_key_name, session_storage,
inflector_irregulars,
flax_compile_templates,
run_as_server,
websocket_server, websocket_port,
renderer_default_layout_file
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
)
end

Expand Down
19 changes: 0 additions & 19 deletions src/FileTemplates.jl
Original file line number Diff line number Diff line change
Expand Up @@ -89,25 +89,6 @@ function appmodule(path::String)
end


function runtests()
"""
isdefined(Main, :UserApp) || include(normpath(joinpath("..", "bootstrap.jl")))
using Test
using Genie, Genie.Router, Genie.Configuration
current_env = Genie.config.app_env
Genie.config.app_env = Genie.Configuration.TEST
@testset "Integration testing" begin
# awesome tests here
end
Genie.config.app_env = current_env
"""
end


function dockerfile(; user::String = "genie", supervisor::Bool = false, nginx::Bool = false, env::String = "dev",
filename::String = "Dockerfile", port::Int = 8000, dockerport::Int = 80, host::String = "0.0.0.0")
appdir = "/home/$user/app"
Expand Down
20 changes: 5 additions & 15 deletions src/Flax.jl
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,13 @@ function parseview(data::String; partial = false, context::Module = @__MODULE__)

func_name = function_name(string(data_hash, partial)) |> Symbol
mod_name = m_name(string(path, partial)) * ".jl"
f_path = joinpath(Genie.BUILD_PATH, BUILD_NAME, mod_name)
f_path = joinpath(Genie.config.path_build, BUILD_NAME, mod_name)
f_stale = build_is_stale(f_path, f_path)

if f_stale || ! isdefined(context, func_name)
f_stale && build_module(string_to_flax(data, partial = partial), path, mod_name)

return Base.include(context, joinpath(Genie.BUILD_PATH, BUILD_NAME, mod_name))
return Base.include(context, joinpath(Genie.config.path_build, BUILD_NAME, mod_name))
end

getfield(context, func_name)
Expand Down Expand Up @@ -177,7 +177,7 @@ end
Persists compiled Flax view data to file and returns the path
"""
function build_module(content::String, path::String, mod_name::String) :: String
module_path = joinpath(Genie.BUILD_PATH, BUILD_NAME, mod_name)
module_path = joinpath(Genie.config.path_build, BUILD_NAME, mod_name)

isdir(dirname(module_path)) || mkpath(dirname(module_path))

Expand Down Expand Up @@ -387,8 +387,8 @@ end
Sets up the build folder and the build module file for generating the compiled views.
"""
function prepare_build(subfolder) :: Bool
build_path = joinpath(Genie.BUILD_PATH, subfolder)
function prepare_build(subfolder = BUILD_NAME) :: Bool
build_path = joinpath(Genie.config.path_build, subfolder)

Genie.Configuration.@ifdev rm(build_path, force = true, recursive = true)
if ! isdir(build_path)
Expand All @@ -399,14 +399,4 @@ function prepare_build(subfolder) :: Bool
true
end


"""
create_build_folders()
Sets up build folders.
"""
@inline function create_build_folders()
prepare_build(BUILD_NAME)
end

end
56 changes: 17 additions & 39 deletions src/Generator.jl
Original file line number Diff line number Diff line change
Expand Up @@ -50,28 +50,6 @@ function newresource(cmd_args::Dict{String,Any}; path::String = ".", pluralize::
views_path = joinpath(resource_path, "views")
isdir(views_path) || mkpath(views_path)

runtests()

isdir(Genie.TEST_PATH_UNIT) || mkpath(Genie.TEST_PATH_UNIT)
test_file = resource_name * Genie.TEST_FILE_IDENTIFIER |> lowercase
write_resource_file(Genie.TEST_PATH_UNIT, test_file, resource_name, :test, pluralize = pluralize) &&
@info "New $test_file created at $(joinpath(Genie.TEST_PATH_UNIT, test_file))"

nothing
end


function runtests() :: Nothing
testfile = joinpath(Genie.TEST_PATH, Genie.TESTS_FILE_NAME)

if ! isfile(testfile)
open(testfile, "w") do f
write(f, Genie.FileTemplates.runtests())
end
else
@error "File $testfile already exists"
end

nothing
end

Expand All @@ -92,9 +70,9 @@ end
Computes and creates the directories structure needed to persist a new resource.
"""
function setup_resource_path(resource_name::String; path::String = ".") :: String
isdir(Genie.APP_PATH) || Genie.Generator.mvc_support(path)
isdir(Genie.config.path_app) || Genie.Generator.mvc_support(path)

resource_path = joinpath(path, Genie.RESOURCES_PATH, lowercase(resource_name))
resource_path = joinpath(path, Genie.config.path_resources, lowercase(resource_name))

if ! isdir(resource_path)
mkpath(resource_path)
Expand Down Expand Up @@ -152,15 +130,15 @@ end
Creates the bin/server and bin/repl binaries for Windows
"""
function setup_windows_bin_files(path::String = ".") :: Nothing
open(joinpath(path, Genie.BIN_PATH, "repl.bat"), "w") do f
open(joinpath(path, Genie.config.path_bin, "repl.bat"), "w") do f
write(f, "$JULIA_PATH --color=yes --depwarn=no -q -i -- ../$(Genie.BOOTSTRAP_FILE_NAME) %*")
end

open(joinpath(path, Genie.BIN_PATH, "server.bat"), "w") do f
open(joinpath(path, Genie.config.path_bin, "server.bat"), "w") do f
write(f, "$JULIA_PATH --color=yes --depwarn=no -q -i -- ../$(Genie.BOOTSTRAP_FILE_NAME) s %*")
end

open(joinpath(path, Genie.BIN_PATH, "serverinteractive.bat"), "w") do f
open(joinpath(path, Genie.config.path_bin, "serverinteractive.bat"), "w") do f
write(f, "$JULIA_PATH --color=yes --depwarn=no -q -i -- ../$(Genie.BOOTSTRAP_FILE_NAME) si %*")
end

Expand All @@ -174,8 +152,8 @@ end
Creates the bin/server and bin/repl binaries for *nix systems
"""
function setup_nix_bin_files(app_path::String = ".") :: Nothing
chmod(joinpath(app_path, Genie.BIN_PATH, "server"), 0o700)
chmod(joinpath(app_path, Genie.BIN_PATH, "repl"), 0o700)
chmod(joinpath(app_path, Genie.config.path_bin, "server"), 0o700)
chmod(joinpath(app_path, Genie.config.path_bin, "repl"), 0o700)

nothing
end
Expand Down Expand Up @@ -221,7 +199,7 @@ end
Generates a valid secrets.jl file with a random SECRET_TOKEN.
"""
function write_secrets_file(app_path::String = ".") :: Nothing
open(joinpath(app_path, Genie.CONFIG_PATH, Genie.SECRETS_FILE_NAME), "w") do f
open(joinpath(app_path, Genie.config.path_config, Genie.SECRETS_FILE_NAME), "w") do f
write(f, """const SECRET_TOKEN = "$(secret_token())" """)
end

Expand Down Expand Up @@ -249,7 +227,7 @@ Writes the file necessary to create a microstack app.
function microstack_app(app_path::String = ".") :: Nothing
mkdir(app_path)

for f in [Genie.BIN_PATH, Genie.CONFIG_PATH, Genie.DOC_ROOT_PATH, Genie.SRC_PATH,
for f in [Genie.config.path_bin, Genie.config.path_config, Genie.config.server_document_root, Genie.config.path_src,
Genie.GENIE_FILE_NAME, Genie.ROUTES_FILE_NAME,
".gitattributes", ".gitignore"]
cp(joinpath(@__DIR__, "..", Genie.NEW_APP_PATH, f), joinpath(app_path, f))
Expand All @@ -267,7 +245,7 @@ end
Writes the files used for rendering resources using the MVC stack and the Flax templating system.
"""
function mvc_support(app_path::String = ".") :: Nothing
cp(joinpath(@__DIR__, "..", Genie.NEW_APP_PATH, Genie.APP_PATH), joinpath(app_path, Genie.APP_PATH))
cp(joinpath(@__DIR__, "..", Genie.NEW_APP_PATH, Genie.config.path_app), joinpath(app_path, Genie.config.path_app))

nothing
end
Expand All @@ -279,10 +257,10 @@ end
Writes files used for interacting with the SearchLight ORM.
"""
function db_support(app_path::String = ".") :: Nothing
cp(joinpath(@__DIR__, "..", Genie.NEW_APP_PATH, Genie.DB_PATH), joinpath(app_path, Genie.DB_PATH))
cp(joinpath(@__DIR__, "..", Genie.NEW_APP_PATH, Genie.config.path_db), joinpath(app_path, Genie.config.path_db))

initializer_path = joinpath(app_path, Genie.INITIALIZERS_PATH, Genie.SEARCHLIGHT_INITIALIZER_FILE_NAME)
isfile(initializer_path) || cp(joinpath(@__DIR__, "..", Genie.NEW_APP_PATH, Genie.INITIALIZERS_PATH, Genie.SEARCHLIGHT_INITIALIZER_FILE_NAME), initializer_path)
initializer_path = joinpath(app_path, Genie.config.path_initializers, Genie.SEARCHLIGHT_INITIALIZER_FILE_NAME)
isfile(initializer_path) || cp(joinpath(@__DIR__, "..", Genie.NEW_APP_PATH, Genie.config.path_initializers, Genie.SEARCHLIGHT_INITIALIZER_FILE_NAME), initializer_path)

nothing
end
Expand All @@ -296,7 +274,7 @@ Writes the Genie app main module file.
function write_app_custom_files(path::String, app_path::String) :: Nothing
moduleinfo = Genie.FileTemplates.appmodule(path)

open(joinpath(app_path, Genie.SRC_PATH, moduleinfo[1] * ".jl"), "w") do f
open(joinpath(app_path, Genie.config.path_src, moduleinfo[1] * ".jl"), "w") do f
write(f, moduleinfo[2])
end

Expand All @@ -308,7 +286,7 @@ function write_app_custom_files(path::String, app_path::String) :: Nothing
Pkg.pkg"activate ."
function main()
include(joinpath("$(Genie.SRC_PATH)", "$(moduleinfo[1]).jl"))
include(joinpath("$(Genie.config.path_src)", "$(moduleinfo[1]).jl"))
end; main()
""")
end
Expand Down Expand Up @@ -359,7 +337,7 @@ end
Removes the asset fingerprint initializers if it's not used
"""
function remove_fingerprint_initializer(app_path::String = ".") :: Nothing
rm(joinpath(app_path, Genie.INITIALIZERS_PATH, Genie.ASSETS_FINGERPRINT_INITIALIZER_FILE_NAME), force = true)
rm(joinpath(app_path, Genie.config.path_initializers, Genie.ASSETS_FINGERPRINT_INITIALIZER_FILE_NAME), force = true)

nothing
end
Expand All @@ -371,7 +349,7 @@ end
Removes the SearchLight initializer file if it's unused
"""
function remove_searchlight_initializer(app_path::String = ".") :: Nothing
rm(joinpath(app_path, Genie.INITIALIZERS_PATH, Genie.SEARCHLIGHT_INITIALIZER_FILE_NAME), force = true)
rm(joinpath(app_path, Genie.config.path_initializers, Genie.SEARCHLIGHT_INITIALIZER_FILE_NAME), force = true)

nothing
end
Expand Down
Loading

0 comments on commit a6d4528

Please sign in to comment.