Skip to content

Add environment variable override for cookie configuration #195

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 23, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Add environment variable override for cookie configuration
Cookies can now be overridden using LEETCODE_CSRF, LEETCODE_SESSION,
and LEETCODE_SITE environment variables when loading configuration.
  • Loading branch information
yousiki committed Jun 15, 2025
commit a776ab54bcd4f9952a0e7220e4bce2b48695429d
24 changes: 24 additions & 0 deletions src/config/cookies.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,27 @@ impl Display for Cookies {
)
}
}

/// Override cookies from environment variables
pub const LEETCODE_CSRF_ENV: &str = "LEETCODE_CSRF";
pub const LEETCODE_SESSION_ENV: &str = "LEETCODE_SESSION";
pub const LEETCODE_SITE_ENV: &str = "LEETCODE_SITE";

impl Cookies {
/// Load cookies from environment variables, overriding any existing values
/// if the environment variables are set.
pub fn with_env_override(mut self) -> Self {
if let Ok(csrf) = std::env::var(LEETCODE_CSRF_ENV) {
self.csrf = csrf;
}
if let Ok(session) = std::env::var(LEETCODE_SESSION_ENV) {
self.session = session;
}
if let Ok(site) = std::env::var(LEETCODE_SITE_ENV) {
if let Ok(leetcode_site) = LeetcodeSite::from_str(&site) {
self.site = leetcode_site;
}
}
self
}
}
19 changes: 12 additions & 7 deletions src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,19 @@ impl Config {

let s = fs::read_to_string(&conf)?;
match toml::from_str::<Config>(&s) {
Ok(config) => match config.cookies.site {
cookies::LeetcodeSite::LeetcodeCom => Ok(config),
cookies::LeetcodeSite::LeetcodeCn => {
let mut config = config;
config.sys.urls = sys::Urls::new_with_leetcode_cn();
Ok(config)
Ok(mut config) => {
// Override config.cookies with environment variables
config.cookies = config.cookies.with_env_override();

match config.cookies.site {
cookies::LeetcodeSite::LeetcodeCom => Ok(config),
cookies::LeetcodeSite::LeetcodeCn => {
let mut config = config;
config.sys.urls = sys::Urls::new_with_leetcode_cn();
Ok(config)
}
}
},
}
Err(e) => {
let tmp = Self::root()?.join("leetcode.tmp.toml");
Self::write_default(tmp)?;
Expand Down
Loading
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy