write a config module
This commit is contained in:
parent
7149273986
commit
4fc398bb26
11 changed files with 115 additions and 50 deletions
78
src/config.gleam
Normal file
78
src/config.gleam
Normal file
|
|
@ -0,0 +1,78 @@
|
|||
import envoy
|
||||
import glaml
|
||||
import gleam/result
|
||||
import simplifile
|
||||
|
||||
pub type Config {
|
||||
Config(mqtt_host: String, mqtt_user: String, mqtt_pw: String, jwt_key: String)
|
||||
}
|
||||
|
||||
fn load_file() -> Result(String, String) {
|
||||
simplifile.read("config.yml")
|
||||
|> result.map_error(fn(_) { "Failed to read config.yml" })
|
||||
}
|
||||
|
||||
fn parse_yaml(contents: String) -> Result(glaml.Document, String) {
|
||||
use docs <- result.try(
|
||||
glaml.parse_string(contents)
|
||||
|> result.map_error(fn(_) { "Failed to parse config.yml" }),
|
||||
)
|
||||
|
||||
case docs {
|
||||
[doc] -> Ok(doc)
|
||||
_ -> Error("Expected exactly one YAML document in config.yml")
|
||||
}
|
||||
}
|
||||
|
||||
fn compile_config(doc: glaml.Document) -> Result(Config, String) {
|
||||
let root = glaml.document_root(doc)
|
||||
|
||||
// Extract values from YAML
|
||||
use mqtt_host_result <- result.try(
|
||||
glaml.select_sugar(root, "mqtt.host")
|
||||
|> result.map_error(fn(_) { "mqtt.host not found in config.yml" }),
|
||||
)
|
||||
use mqtt_user_result <- result.try(
|
||||
glaml.select_sugar(root, "mqtt.user")
|
||||
|> result.map_error(fn(_) { "mqtt.user not found in config.yml" }),
|
||||
)
|
||||
use jwt_key_result <- result.try(
|
||||
glaml.select_sugar(root, "jwt_key")
|
||||
|> result.map_error(fn(_) { "jwt_key not found in config.yml" }),
|
||||
)
|
||||
|
||||
// Extract strings from nodes
|
||||
case mqtt_host_result, mqtt_user_result, jwt_key_result {
|
||||
glaml.NodeStr(host), glaml.NodeStr(user), glaml.NodeStr(key) -> {
|
||||
let yaml_config =
|
||||
Config(
|
||||
mqtt_host: host,
|
||||
mqtt_user: user,
|
||||
mqtt_pw: "placeholder",
|
||||
jwt_key: key,
|
||||
)
|
||||
|
||||
// Override with env vars
|
||||
let final_config =
|
||||
Config(
|
||||
mqtt_host: envoy.get("MQTT_HOST")
|
||||
|> result.unwrap(yaml_config.mqtt_host),
|
||||
mqtt_user: envoy.get("MQTT_USER")
|
||||
|> result.unwrap(yaml_config.mqtt_user),
|
||||
mqtt_pw: envoy.get("MQTT_PW")
|
||||
|> result.unwrap(yaml_config.mqtt_pw),
|
||||
jwt_key: envoy.get("JWT_KEY")
|
||||
|> result.unwrap(yaml_config.jwt_key),
|
||||
)
|
||||
|
||||
Ok(final_config)
|
||||
}
|
||||
_, _, _ -> Error("Config values must be strings in config.yml")
|
||||
}
|
||||
}
|
||||
|
||||
pub fn load_config() -> Result(Config, String) {
|
||||
use file_content <- result.try(load_file())
|
||||
use doc <- result.try(parse_yaml(file_content))
|
||||
compile_config(doc)
|
||||
}
|
||||
|
|
@ -1,5 +1,17 @@
|
|||
import config
|
||||
import gleam/io
|
||||
|
||||
pub fn main() -> Nil {
|
||||
io.println("Hello from weather_portal!")
|
||||
case config.load_config() {
|
||||
Ok(cfg) -> {
|
||||
io.println("Config loaded successfully!")
|
||||
io.println("MQTT Host: " <> cfg.mqtt_host)
|
||||
io.println("MQTT User: " <> cfg.mqtt_user)
|
||||
io.println("MQTT PW: " <> cfg.mqtt_pw)
|
||||
io.println("JWT Key: " <> cfg.jwt_key)
|
||||
}
|
||||
Error(err) -> {
|
||||
io.println("Failed to load config: " <> err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue