A new env variable

BACKEND_PORT for the port of the backend
This commit is contained in:
2026-05-20 12:49:33 +02:00
parent 7f4237a6b7
commit 0db9b76cad

View File

@@ -1,12 +1,13 @@
/// Environment configuration for the application. /// Environment configuration for the application.
/// ///
/// Loads required configuration from environment variables at startup. /// Loads required configuration from environment variables at startup.
/// All variables must be present or the application will panic. /// All variables must be present or the application will panic during [`Env::load`].
/// Used by [`AppState`](crate::AppState) for configuring JWT signing and CORS.
/// ///
/// # Fields /// # Fields
/// - `db_url`: PostgreSQL database connection URL. /// - `db_url`: PostgreSQL database connection URL
/// - `token_secret`: Secret key used to sign and verify JWT tokens. /// - `token_secret`: Secret key used to sign and verify [`Claims`](crate::models::Claims) in JWT tokens
/// - `origin`: Frontend origin URL for CORS policy. /// - `origin`: Frontend origin URL for CORS policy configuration
/// ///
/// # Required Environment Variables /// # Required Environment Variables
/// - `DATABASE_URL`: PostgreSQL connection string (e.g., `postgresql://user:pass@localhost/dbname`) /// - `DATABASE_URL`: PostgreSQL connection string (e.g., `postgresql://user:pass@localhost/dbname`)
@@ -20,26 +21,38 @@ pub struct Env {
pub token_secret: String, pub token_secret: String,
/// Frontend origin URL for CORS policy /// Frontend origin URL for CORS policy
pub origin: String, pub origin: String,
/// Backend port number
pub backend_port: String,
} }
impl Env { impl Env {
/// Loads environment configuration from system environment variables. /// Loads environment configuration from system environment variables.
/// ///
/// Panics if any required variable is missing. /// Reads `DATABASE_URL`, `TOKEN_SECRET`, and `ORIGIN` from the environment and returns
/// a configured [`Env`] instance. Used during server initialization in the `main` function.
///
/// # Panics
/// If any required variable is missing (DATABASE_URL, TOKEN_SECRET, or ORIGIN).
/// ///
/// # Example /// # Example
/// ```ignore /// ```ignore
/// let env = Env::load(); /// let env = Env::load();
/// // Environment must have DATABASE_URL, TOKEN_SECRET, and ORIGIN set /// // Environment must have DATABASE_URL, TOKEN_SECRET, and ORIGIN set
/// let app_state = AppState {
/// db: pool,
/// env,
/// };
/// ``` /// ```
pub fn load() -> Env { pub fn load() -> Env {
let db_url = std::env::var("DATABASE_URL").expect("DATABASE_URL must be set"); let db_url = std::env::var("DATABASE_URL").expect("DATABASE_URL must be set");
let token_secret = std::env::var("TOKEN_SECRET").expect("TOKEN_SECRET must be set"); let token_secret = std::env::var("TOKEN_SECRET").expect("TOKEN_SECRET must be set");
let origin = std::env::var("ORIGIN").expect("ORIGIN must be set"); let origin = std::env::var("ORIGIN").expect("ORIGIN must be set");
let backend_port = std::env::var("BACKEND_PORT").expect("BACKEND_PORT must be set");
Env { Env {
db_url, db_url,
token_secret, token_secret,
origin, origin,
backend_port,
} }
} }
} }