-
-
Notifications
You must be signed in to change notification settings - Fork 892
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
A first attempt at using deser-hjson. Fixes #1270 #1433
Conversation
crates/utils/src/settings.rs
Outdated
@@ -56,7 +97,16 @@ pub struct EmailConfig { | |||
#[derive(Debug, Deserialize, Clone)] | |||
pub struct CaptchaConfig { | |||
pub enabled: bool, | |||
pub difficulty: String, // easy, medium, or hard | |||
pub difficulty: String, |
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.
Can we turn this into an enum?
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.
I doubt it, since json / hjson doesn't support enums.
The API code does this:
let captcha = match captcha_settings.difficulty.as_str() {
"easy" => gen(Difficulty::Easy),
"medium" => gen(Difficulty::Medium),
"hard" => gen(Difficulty::Hard),
_ => gen(Difficulty::Medium),
};
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.
I mean in Rust, so that it gets serialized to string automatically.
Does this change still allow editing the config via |
- Removing unwrap_or_defaults, using impl functions. - Reorganized settings
@@ -29,8 +29,9 @@ | |||
# https://join.lemmy.ml/docs/en/federation/administration.html#instance-allowlist-and-blocklist | |||
# | |||
# comma separated list of instances with which federation is allowed | |||
# allowed_instances: "" | |||
# Only one of these blocks should be uncommented | |||
# allowed_instances: ["instance1.tld","instance2.tld"] |
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.
hjson needs quoted strings if its in a list.
This looks actually pretty good, because it needs way less boilerplate for the settings compared to my approach. The only problem is that it doesnt support env vars. If you can add that somehow, then I would prefer your implementation (feel free to copy things from my branch). |
I'll try that out tmrw |
let mut custom_config = from_str::<Settings>(&Self::read_config_file()?)?; | ||
|
||
// Merge with env vars | ||
custom_config.merge(envy::prefixed("LEMMY_").from_env::<Settings>()?); |
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.
Okay @Nutomic , got env vars working. I took this from your PR, and it worked (see below)
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.
So it works for all config values, including nested ones? Can we revert the changes to configuration in api_tests/
and docker/
then?
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.
We could, but since I already wrote the configs as proper hjson
files, I don't think there's a reason to.
@@ -0,0 +1,36 @@ | |||
{ | |||
port: 8541 |
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.
I removed hostname from this file to test env vars.
LEMMY_SETUP__ADMIN_USERNAME=lemmy_alpha \ | ||
LEMMY_SETUP__SITE_NAME=lemmy-alpha \ | ||
target/lemmy_server >/dev/null 2>&1 & | ||
LEMMY_HOSTNAME="lemmy-alpha:8541" \ |
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.
added an env var for hostname here, removed it from the config file
I tried to test the env vars by doing:
But the tests are all failing. After adding |
Remember that while every top level field is optional, not every field in the blocks are, so you might need to add the other required fields for that block. |
crates/utils/src/settings/mod.rs
Outdated
iframely_url: Some("http://iframely".into()), | ||
} | ||
} | ||
} |
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.
I would move struct Settings
into structs.rs
, and impl Default
into defaults.rs
, just to have everything in one place.
Ah you are using envy, no wonder that nested vars arent working then. There is no easy solution it seems, so lets merge with env parsing as is, and open an issue to improve it later (linking to softprops/envy#55). We also need to emphasize this as a breaking change in the changelog. Edit: Actually that issue I linked has a (somewhat verbose) solution. Doesnt have to be part of this PR, but we should be able to implement it before v0.10.0 and avoid the breaking change. |
…n-pwd-change * origin/main: revert Compose file version from 3.3 to 2.2 Adding more mem limits bump memory limit of iframely Remove extra category_id s . Fixes LemmyNet#1429 Fixing wrong user_ and community icon and banner urls. Remove category from activitypub context Adding a password length check to other API actions. (LemmyNet#1474) Update test script Use URL type in most outstanding struct fields (LemmyNet#1468) Forbid usage of unwrap Upgrade Rust version Rewrite settings implementation. Fixes LemmyNet#1270 (LemmyNet#1433) Rename `lemmy_structs` to `lemmy_api_structs` # Conflicts: # crates/db_schema/src/source/user.rs
Haven't tried upgrading deps yet, but it works!