Skip to content

Commit

Permalink
feat: added env vars
Browse files Browse the repository at this point in the history
  • Loading branch information
soulsam480 committed Jun 30, 2024
1 parent b09d5e0 commit 7993506
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 11 deletions.
3 changes: 3 additions & 0 deletions .env
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
PORT=8001
MODE=dev
SECRET=etxrZL_OeyDt6MUawYx8OM_yzSdHNjWT6pohRIVZ4j5bt_tI0fOF5Z_ySf0gprO6
1 change: 1 addition & 0 deletions gleam.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
4 changes: 3 additions & 1 deletion manifest.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" },
Expand All @@ -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" }
33 changes: 23 additions & 10 deletions src/okane.gleam
Original file line number Diff line number Diff line change
@@ -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
Expand Down

0 comments on commit 7993506

Please sign in to comment.