backend/
env.rs

1/// Environment configuration for the application.
2///
3/// Loads required configuration from environment variables at startup.
4/// All variables must be present or the application will panic during [`Env::load`].
5/// Used by [`AppState`](crate::AppState) for configuring JWT signing and CORS.
6///
7/// # Fields
8/// - `db_url`: PostgreSQL database connection URL
9/// - `token_secret`: Secret key used to sign and verify [`Claims`](crate::models::Claims) in JWT tokens
10/// - `origin`: Frontend origin URL for CORS policy configuration
11///
12/// # Required Environment Variables
13/// - `DATABASE_URL`: PostgreSQL connection string (e.g., `postgresql://user:pass@localhost/dbname`)
14/// - `TOKEN_SECRET`: Secret key for JWT token signing (use a strong random string in production)
15/// - `ORIGIN`: Frontend URL for CORS (e.g., `http://localhost:8080`)
16#[derive(Debug, Clone)]
17pub struct Env {
18    /// PostgreSQL database connection URL
19    pub db_url: String,
20    /// Secret key used to sign and verify JWT tokens
21    pub token_secret: String,
22    /// Frontend origin URL for CORS policy
23    pub origin: String,
24}
25
26impl Env {
27    /// Loads environment configuration from system environment variables.
28    ///
29    /// Reads `DATABASE_URL`, `TOKEN_SECRET`, and `ORIGIN` from the environment and returns
30    /// a configured [`Env`] instance. Used during server initialization in the `main` function.
31    ///
32    /// # Panics
33    /// If any required variable is missing (DATABASE_URL, TOKEN_SECRET, or ORIGIN).
34    ///
35    /// # Example
36    /// ```ignore
37    /// let env = Env::load();
38    /// // Environment must have DATABASE_URL, TOKEN_SECRET, and ORIGIN set
39    /// let app_state = AppState {
40    ///     db: pool,
41    ///     env,
42    /// };
43    /// ```
44    pub fn load() -> Env {
45        let db_url = std::env::var("DATABASE_URL").expect("DATABASE_URL must be set");
46        let token_secret = std::env::var("TOKEN_SECRET").expect("TOKEN_SECRET must be set");
47        let origin = std::env::var("ORIGIN").expect("ORIGIN must be set");
48        Env {
49            db_url,
50            token_secret,
51            origin,
52        }
53    }
54}