-
Notifications
You must be signed in to change notification settings - Fork 0
Add config store support across all adapters #209
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
Open
prk-Jr
wants to merge
9
commits into
main
Choose a base branch
from
feat/config-store
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
43c7555
Config store implementation
a1b8d07
Production hardening for config store and added docs
516c341
Fix format
2842f1b
Merge branch 'main' into feat/config-store
aram356 ba1d1c7
Harden config store docs and adapter comments
6bca976
Format docs
70e55d0
Fix explicit wasm test jobs in ci
88fa265
fix fastly wasm contract tests
e99b8b0
Clarify config-aware dispatch APIs
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,6 +4,9 @@ pkg/ | |
| target/ | ||
| .wrangler/ | ||
|
|
||
| # Node | ||
| node_modules/ | ||
|
|
||
| # env | ||
| .env | ||
|
|
||
|
|
||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,154 @@ | ||
| //! Axum adapter config store: env vars with in-memory defaults fallback. | ||
|
|
||
| use std::collections::HashMap; | ||
|
|
||
| use edgezero_core::config_store::{ConfigStore, ConfigStoreError}; | ||
|
|
||
| /// Config store for local dev / Axum. Reads from env vars with manifest | ||
| /// defaults as fallback. Env vars take precedence over defaults. | ||
| /// | ||
| /// # Note on `from_env` | ||
| /// | ||
| /// [`AxumConfigStore::from_env`] only reads environment variables for keys | ||
| /// declared in `[stores.config.defaults]`. Use an empty-string default when a | ||
| /// key should be overrideable from env without carrying a real default value. | ||
| pub struct AxumConfigStore { | ||
| env: HashMap<String, String>, | ||
| defaults: HashMap<String, String>, | ||
| } | ||
|
|
||
| impl AxumConfigStore { | ||
| /// Create from env vars and optional manifest defaults. | ||
| pub fn new( | ||
| env: impl IntoIterator<Item = (String, String)>, | ||
| defaults: impl IntoIterator<Item = (String, String)>, | ||
| ) -> Self { | ||
| Self { | ||
| env: env.into_iter().collect(), | ||
| defaults: defaults.into_iter().collect(), | ||
| } | ||
| } | ||
|
|
||
| /// Create from the current process environment and manifest defaults. | ||
| pub fn from_env(defaults: impl IntoIterator<Item = (String, String)>) -> Self { | ||
| Self::from_lookup(defaults, |key| std::env::var(key).ok()) | ||
| } | ||
|
|
||
| fn from_lookup<F>(defaults: impl IntoIterator<Item = (String, String)>, mut lookup: F) -> Self | ||
| where | ||
| F: FnMut(&str) -> Option<String>, | ||
| { | ||
| let defaults: HashMap<String, String> = defaults.into_iter().collect(); | ||
| let env = defaults | ||
| .keys() | ||
| .filter_map(|key| lookup(key).map(|value| (key.clone(), value))) | ||
| .collect(); | ||
| Self { env, defaults } | ||
| } | ||
| } | ||
|
|
||
| impl ConfigStore for AxumConfigStore { | ||
| fn get(&self, key: &str) -> Result<Option<String>, ConfigStoreError> { | ||
| Ok(self | ||
| .env | ||
| .get(key) | ||
| .or_else(|| self.defaults.get(key)) | ||
| .cloned()) | ||
| } | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use super::*; | ||
|
|
||
| fn store(env: &[(&str, &str)], defaults: &[(&str, &str)]) -> AxumConfigStore { | ||
| AxumConfigStore::new( | ||
| env.iter().map(|(k, v)| (k.to_string(), v.to_string())), | ||
| defaults.iter().map(|(k, v)| (k.to_string(), v.to_string())), | ||
| ) | ||
| } | ||
|
|
||
| #[test] | ||
| fn axum_config_store_returns_values() { | ||
| let s = store(&[("MY_KEY", "my_val")], &[]); | ||
| assert_eq!( | ||
| s.get("MY_KEY").expect("config value"), | ||
| Some("my_val".to_string()) | ||
| ); | ||
| } | ||
|
|
||
| #[test] | ||
| fn axum_config_store_returns_none_for_missing() { | ||
| let s = store(&[], &[]); | ||
| assert_eq!(s.get("NOPE").expect("missing config"), None); | ||
| } | ||
|
|
||
| #[test] | ||
| fn axum_config_store_env_overrides_defaults() { | ||
| let s = store(&[("KEY", "from_env")], &[("KEY", "from_default")]); | ||
| assert_eq!( | ||
| s.get("KEY").expect("config value"), | ||
| Some("from_env".to_string()) | ||
| ); | ||
| } | ||
|
|
||
| #[test] | ||
| fn axum_config_store_falls_back_to_defaults() { | ||
| let s = store(&[], &[("KEY", "default_val")]); | ||
| assert_eq!( | ||
| s.get("KEY").expect("default config"), | ||
| Some("default_val".to_string()) | ||
| ); | ||
| } | ||
|
|
||
| #[test] | ||
| fn axum_config_store_from_env_reads_only_declared_keys() { | ||
| let s = AxumConfigStore::from_lookup( | ||
| [ | ||
| ("feature.new_checkout".to_string(), "false".to_string()), | ||
| ("service.timeout_ms".to_string(), "1500".to_string()), | ||
| ], | ||
| |key| match key { | ||
| "feature.new_checkout" => Some("true".to_string()), | ||
| "DATABASE_URL" => Some("postgres://secret".to_string()), | ||
| _ => None, | ||
| }, | ||
| ); | ||
|
|
||
| assert_eq!( | ||
| s.get("feature.new_checkout").expect("allowed env override"), | ||
| Some("true".to_string()) | ||
| ); | ||
| assert_eq!( | ||
| s.get("service.timeout_ms").expect("default fallback"), | ||
| Some("1500".to_string()) | ||
| ); | ||
| assert_eq!( | ||
| s.get("DATABASE_URL") | ||
| .expect("undeclared key should stay hidden"), | ||
| None | ||
| ); | ||
| } | ||
|
|
||
| // Run the shared contract tests against AxumConfigStore (env path). | ||
| edgezero_core::config_store_contract_tests!(axum_config_store_env_contract, { | ||
| AxumConfigStore::new( | ||
| [ | ||
| ("contract.key.a".to_string(), "value_a".to_string()), | ||
| ("contract.key.b".to_string(), "value_b".to_string()), | ||
| ], | ||
| [], | ||
| ) | ||
| }); | ||
|
|
||
| // Run the shared contract tests against AxumConfigStore (defaults path). | ||
| edgezero_core::config_store_contract_tests!(axum_config_store_defaults_contract, { | ||
| AxumConfigStore::new( | ||
| [], | ||
| [ | ||
| ("contract.key.a".to_string(), "value_a".to_string()), | ||
| ("contract.key.b".to_string(), "value_b".to_string()), | ||
| ], | ||
| ) | ||
| }); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is the call site that wires
from_envinto the server. After fixingfrom_envto allowlist-only, this should continue to work unchanged —config_store_defaults()already returns exactly the right key set.Worth noting: if someone constructs
AxumConfigStore::from_env(BTreeMap::new())directly (empty defaults), the allowlist fix would make the env layer empty too, which is the safe default.