diff --git a/.env b/.env new file mode 100644 index 0000000..921752f --- /dev/null +++ b/.env @@ -0,0 +1,3 @@ +PORT=8001 +MODE=dev +SECRET=etxrZL_OeyDt6MUawYx8OM_yzSdHNjWT6pohRIVZ4j5bt_tI0fOF5Z_ySf0gprO6 diff --git a/gleam.toml b/gleam.toml index 8f54e97..b8cf68f 100644 --- a/gleam.toml +++ b/gleam.toml @@ -19,6 +19,7 @@ gleam_erlang = ">= 0.25.0 and < 1.0.0" mist = ">= 1.2.0 and < 2.0.0" gleam_http = ">= 3.6.0 and < 4.0.0" radiate = ">= 0.4.0 and < 1.0.0" +dot_env = ">= 1.0.0 and < 2.0.0" [dev-dependencies] gleeunit = ">= 1.0.0 and < 2.0.0" diff --git a/manifest.toml b/manifest.toml index c7eb85a..548133f 100644 --- a/manifest.toml +++ b/manifest.toml @@ -3,6 +3,7 @@ packages = [ { name = "birl", version = "1.7.1", build_tools = ["gleam"], requirements = ["gleam_stdlib", "ranger"], otp_app = "birl", source = "hex", outer_checksum = "5C66647D62BCB11FE327E7A6024907C4A17954EF22865FE0940B54A852446D01" }, + { name = "dot_env", version = "1.0.0", build_tools = ["gleam"], requirements = ["gleam_stdlib", "simplifile"], otp_app = "dot_env", source = "hex", outer_checksum = "E7B84DC7B579553AF3B9F0A03B2F8DDB9B44521F553CCFBE633AA595C27F1A05" }, { name = "exception", version = "2.0.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "exception", source = "hex", outer_checksum = "F5580D584F16A20B7FCDCABF9E9BE9A2C1F6AC4F9176FA6DD0B63E3B20D450AA" }, { name = "filepath", version = "1.0.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "filepath", source = "hex", outer_checksum = "EFB6FF65C98B2A16378ABC3EE2B14124168C0CE5201553DE652E2644DCFDB594" }, { name = "filespy", version = "0.5.0", build_tools = ["gleam"], requirements = ["fs", "gleam_erlang", "gleam_otp", "gleam_stdlib"], otp_app = "filespy", source = "hex", outer_checksum = "F8E7A9C9CA86D68CCC25491125BFF36BEF7483892D7BEC24AA30D6B540504F06" }, @@ -29,10 +30,11 @@ packages = [ ] [requirements] +dot_env = { version = ">= 1.0.0 and < 2.0.0"} gleam_erlang = { version = ">= 0.25.0 and < 1.0.0" } gleam_http = { version = ">= 3.6.0 and < 4.0.0" } gleam_stdlib = { version = ">= 0.34.0 and < 2.0.0" } gleeunit = { version = ">= 1.0.0 and < 2.0.0" } mist = { version = ">= 1.2.0 and < 2.0.0" } -radiate = { version = ">= 0.4.0 and < 1.0.0"} +radiate = { version = ">= 0.4.0 and < 1.0.0" } wisp = { version = ">= 0.15.0 and < 1.0.0" } diff --git a/src/okane.gleam b/src/okane.gleam index 61b5455..df646cd 100644 --- a/src/okane.gleam +++ b/src/okane.gleam @@ -1,28 +1,41 @@ import app/router +import dot_env +import dot_env/env import gleam/erlang/process import mist import radiate import wisp pub fn main() { - let _ = - radiate.new() - |> radiate.add_dir(".") - |> radiate.start() + // load env vars + dot_env.load_default() + + // only enable hot-reload in dev + case env.get_or("MODE", "dev") { + "dev" -> { + let _ = + radiate.new() + |> radiate.add_dir(".") + |> radiate.start() + + Nil + } + + _ -> Nil + } // This sets the logger to print INFO level logs, and other sensible defaults // for a web application. wisp.configure_logger() - // Here we generate a secret key, but in a real application you would want to - // load this from somewhere so that it is not regenerated on every restart. - let secret_key_base = wisp.random_string(64) - // Start the Mist web server. let assert Ok(_) = - wisp.mist_handler(router.handle_request, secret_key_base) + wisp.mist_handler( + router.handle_request, + env.get_or("SECRET", wisp.random_string(64)), + ) |> mist.new - |> mist.port(8000) + |> mist.port(env.get_int_or("PORT", 8000)) |> mist.start_http // The web server runs in new Erlang process, so put this one to sleep while