Skip to content

Commit 441de7d

Browse files
Replace http::uri::Uri with url::Url
The version of the `http::uri::Uri` that is used in this project is old, because it is provided by Actix. It was only directly used by the project to generate enrollment URLs. This also required the `http-serde` library at an older version to be compatible. This is not ideal, so I replaced it with the latest version of the `url::Url` which does the job even better.
1 parent 4c232f4 commit 441de7d

File tree

5 files changed

+15
-30
lines changed

5 files changed

+15
-30
lines changed

Cargo.lock

Lines changed: 1 addition & 11 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ anyhow = "1.0"
1414
argon2 = { version = "0.5" }
1515
clap = { version = "4", default-features = false, features = ["std", "derive", "help", "usage"] }
1616
handlebars = { version = "6", features = ["dir_source"] }
17-
http-serde = "1"
1817
rand = { version = "0.9", default-features = false, features = [] }
1918
rustls = { version = "0.23.13", default-features = false, features = ["logging", "ring", "std"] }
2019
rustls-pemfile = "2.1.3"
@@ -24,3 +23,4 @@ serde_yaml_ng = { version = "0.10.0", default-features = false, features = [] }
2423
thiserror = { version = "2", default-features = false, features = [] }
2524
tracing = { version = "0.1.40", features = ["log"] }
2625
tracing-subscriber = "0.3.18"
26+
url = { version = "2.5.7", default-features = false, features = ["serde"] }

config.yml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
---
22
listen_on: 127.0.0.1:9753
3-
worlds_path: /var/lib/minecraft/worlds
3+
worlds_path: /tmp/nix-shell.Bihv8S/tmp.YDoMbAsiBf
44
current_world_name: current_world
55
server_socket_path: /run/minecraft/minecraft.socket
6-
users_file_path: /var/lib/minecraft/users.yml
6+
users_file_path: ./users.yaml
77
base_url: http://127.0.0.1:9753/
88
min_password_length: 10
99
max_password_length: 128
10+
server_properties_path: ./server.properties

src/cli/user.rs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
use crate::core;
2-
use actix_web::http::uri;
3-
use std::str::FromStr;
42

53
#[derive(thiserror::Error, Debug)]
64
pub enum Error {
@@ -18,11 +16,9 @@ pub fn enroll(config: core::AppConfig, username: String) -> Result<(), Error> {
1816
let users = core::Users::load(config.users_file_path).map_err(Error::FailedToEnrol)?;
1917
let token = users.enroll_user(username).map_err(Error::FailedToEnrol)?;
2018

21-
let mut parts = config.base_url.into_parts();
22-
let path_and_query = uri::PathAndQuery::from_str(&format!("/enroll?token={}", token.reveal()))
23-
.expect("Failed to create the path and query part for an enrollment URL.");
24-
parts.path_and_query = Some(path_and_query);
25-
let url: uri::Uri = uri::Uri::from_parts(parts).expect("Failed to generate an enrollment URL.");
19+
let mut url = config.base_url;
20+
url.set_path("/enroll");
21+
url.set_query(Some(&format!("token={}", token.reveal())));
2622

2723
println!("To finish the enrollment visit {}", url);
2824

src/core/config.rs

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
use super::properties;
2-
use actix_web::http::uri;
32
use std::{env, fs, io, net, num, path};
43

54
#[derive(serde::Deserialize)]
65
struct ConfigFile {
76
listen_on: net::SocketAddr,
87
worlds_path: path::PathBuf,
98
users_file_path: path::PathBuf,
10-
#[serde(with = "http_serde::uri")]
11-
base_url: uri::Uri,
9+
base_url: url::Url,
1210
#[serde(default = "default_min_password_len")]
1311
min_password_length: u8,
1412
#[serde(default = "default_max_password_len")]
@@ -57,7 +55,7 @@ pub enum ConfigValidationError {
5755
#[error("Invalid users file path: {0}")]
5856
UsersFilePath(String),
5957
#[error("Invalid base URL: {0}")]
60-
InvalidBaseUrl(uri::Uri),
58+
InvalidBaseUrl(url::Url),
6159
#[error("Invalid server.properties path: {}", .0.display())]
6260
PropertiesPath(path::PathBuf),
6361
#[error("Unable to load server.properties file: {0}")]
@@ -70,7 +68,7 @@ pub struct AppConfig {
7068
pub worlds_path: path::PathBuf,
7169
pub rcon_address: net::SocketAddr,
7270
pub users_file_path: path::PathBuf,
73-
pub base_url: uri::Uri,
71+
pub base_url: url::Url,
7472
pub min_password_length: usize,
7573
pub max_password_length: usize,
7674
pub server_properties_path: path::PathBuf,
@@ -199,11 +197,11 @@ fn resolve_users_file_path(
199197
}
200198
}
201199

202-
fn check_base_url(url: uri::Uri) -> Result<uri::Uri, ConfigValidationError> {
203-
if url.scheme().is_none() {
204-
Err(ConfigValidationError::InvalidBaseUrl(url))
205-
} else {
200+
fn check_base_url(url: url::Url) -> Result<url::Url, ConfigValidationError> {
201+
if url.scheme().starts_with("http") {
206202
Ok(url)
203+
} else {
204+
Err(ConfigValidationError::InvalidBaseUrl(url))
207205
}
208206
}
209207

0 commit comments

Comments
 (0)