Is it possible to pass in constants system variables? Docker-Compose

What I'm trying to do is to pass system variables from the docker-compose to my api. And when I try to do this I get errors. And I wanted to know if it's possible to do it. Or is there any other more elegant way to do this?

pub const SESSION_DURATION : u16 = env::var("SESSION_DURATION").unwrap().parse::<u16>().unwrap();

pub const DEPLOY_PORT : u16 = env::var("DEPLOY_PORT").unwrap().parse::<u16>().unwrap();

pub const FILE_LOCATION : &'static str = env::var("FILE_LOCATION").unwrap().parse::<&'static str>().unwrap();

pub const BOT_TOKEN_LOCATION : &'static str = env::var("BOT_TOKEN_LOCATION").unwrap().parse::<&'static str>().unwrap();

env!() (you can only get string literals, though – if you do need other types, parse the values in a LazyLock.)

3 Likes

Thanks! I'll give it a try!

Note that the env! macro reads these variables during compile time, which I don't think is what you are looking for. So I believe a static LazyLock calling env::var as paramagnetic also suggested is the better approach.

1 Like

Yeah, it's true. I've tried it and it's no what I was looking for. It's a weekend now, so I don't think it's the right time to solve this puzzle. I'll return to this task on next Monday =)

Besides the solutions provided by @paramagnetic and @jofas, I would recommend using a proper configuration library such as Figment — Rust config library // Lib.rs as the next step.

1 Like

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.