Skip to content
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

Merged
merged 11 commits into from
Mar 1, 2021
Merged

Conversation

dessalines
Copy link
Member

Haven't tried upgrading deps yet, but it works!

@dessalines dessalines requested a review from Nutomic February 17, 2021 00:55
src/main.rs Outdated Show resolved Hide resolved
@dessalines dessalines marked this pull request as draft February 17, 2021 15:15
@dessalines dessalines marked this pull request as ready for review February 17, 2021 16:32
@@ -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,
Copy link
Member

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?

Copy link
Member Author

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),
    };

Copy link
Member

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.

@Nutomic
Copy link
Member

Nutomic commented Feb 18, 2021

Does this change still allow editing the config via /admin?

@dessalines dessalines marked this pull request as draft February 18, 2021 16:26
- Removing unwrap_or_defaults, using impl functions.
- Reorganized settings
@dessalines dessalines marked this pull request as ready for review February 19, 2021 01:07
@@ -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"]
Copy link
Member Author

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.

@Nutomic
Copy link
Member

Nutomic commented Feb 23, 2021

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).

@dessalines
Copy link
Member Author

I'll try that out tmrw

@dessalines dessalines marked this pull request as draft February 25, 2021 00:39
let mut custom_config = from_str::<Settings>(&Self::read_config_file()?)?;

// Merge with env vars
custom_config.merge(envy::prefixed("LEMMY_").from_env::<Settings>()?);
Copy link
Member Author

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)

Copy link
Member

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?

Copy link
Member Author

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
Copy link
Member Author

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" \
Copy link
Member Author

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

@dessalines dessalines marked this pull request as ready for review February 25, 2021 01:00
@dessalines dessalines requested a review from Nutomic February 25, 2021 01:00
@Nutomic
Copy link
Member

Nutomic commented Feb 26, 2021

I tried to test the env vars by doing:

git checkout main -- api_tests
cd api_tests
./run-federation-test.sh

But the tests are all failing. After adding dbg!(&settings.federation().allowed_instances) in the code, it logs settings.federation().allowed_instances = None. So parsing of nested env vars doesnt seem to work yet (I tried both LEMMY_FEDERATION_ALLOWED_INSTANCES and LEMMY_FEDERATION__ALLOWED_INSTANCES).

@dessalines
Copy link
Member Author

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.

iframely_url: Some("http://iframely".into()),
}
}
}
Copy link
Member

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.

@Nutomic
Copy link
Member

Nutomic commented Mar 1, 2021

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.

@dessalines dessalines requested a review from Nutomic March 1, 2021 17:07
@Nutomic Nutomic merged commit 462c4a2 into main Mar 1, 2021
Mart-Bogdan added a commit to Mart-Bogdan/lemmy that referenced this pull request Mar 13, 2021
…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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants