From fac146e8590d006fec603b6c9f30d36aa281e855 Mon Sep 17 00:00:00 2001 From: oxalica Date: Wed, 25 Sep 2024 12:45:34 -0400 Subject: [PATCH] build: validate CFG_SRC_URL and simplify CFG_RELEASE handling --- blahctl/src/main.rs | 7 +++++-- blahd/Cargo.toml | 3 +++ blahd/build.rs | 20 ++++++++++++++++++++ blahd/src/bin/blahd.rs | 2 +- blahd/src/lib.rs | 6 +----- blahd/src/register.rs | 6 ++---- 6 files changed, 32 insertions(+), 12 deletions(-) create mode 100644 blahd/build.rs diff --git a/blahctl/src/main.rs b/blahctl/src/main.rs index 189772c..f63e778 100644 --- a/blahctl/src/main.rs +++ b/blahctl/src/main.rs @@ -16,7 +16,10 @@ use reqwest::Url; use rusqlite::{named_params, prepare_and_bind, Connection}; use tokio::runtime::Runtime; -const USER_AGENT: &str = concat!("blahctl/", env!("CARGO_PKG_VERSION")); +const USER_AGENT: fn() -> String = || match option_env!("CFG_RELEASE") { + None => concat!("blahctl/", env!("CARGO_PKG_VERSION")).into(), + Some(vers) => format!("blahctl/{vers}"), +}; /// Control or manage Blah Chat Server. #[derive(Debug, clap::Parser)] @@ -337,7 +340,7 @@ fn build_rt() -> Result { fn build_client() -> Result { reqwest::Client::builder() - .user_agent(USER_AGENT) + .user_agent(USER_AGENT()) .redirect(reqwest::redirect::Policy::none()) .build() .context("failed to build HTTP client") diff --git a/blahd/Cargo.toml b/blahd/Cargo.toml index b2c9cca..c695bbc 100644 --- a/blahd/Cargo.toml +++ b/blahd/Cargo.toml @@ -38,6 +38,9 @@ url = { version = "2", features = ["serde"] } blah-types = { path = "../blah-types", features = ["rusqlite"] } +[build-dependencies] +url = "2" + [dev-dependencies] expect-test = "1" nix = { version = "0.29", features = ["fs", "process", "signal"] } diff --git a/blahd/build.rs b/blahd/build.rs new file mode 100644 index 0000000..555dd31 --- /dev/null +++ b/blahd/build.rs @@ -0,0 +1,20 @@ +fn main() { + // No rerun on file changes. + println!("cargo::rerun-if-changed=build.rs"); + + println!("cargo::rerun-if-env-changed=CFG_RELEASE"); + if std::env::var_os("CFG_RELEASE").is_none() { + let vers = std::env::var("CARGO_PKG_VERSION").expect("cargo should set it"); + println!("cargo::rustc-env=CFG_RELEASE={vers}"); + } + + println!("cargo::rerun-if-env-changed=CFG_SRC_URL"); + if let Some(url) = std::env::var_os("CFG_SRC_URL") { + url.to_str() + .expect("CFG_SRC_URL is not in UTF-8") + .parse::() + .expect("CFG_SRC_URL is not a valid URL"); + } else { + println!("cargo::warning=CFG_SRC_URL is not set"); + } +} diff --git a/blahd/src/bin/blahd.rs b/blahd/src/bin/blahd.rs index c84dde2..d52f67c 100644 --- a/blahd/src/bin/blahd.rs +++ b/blahd/src/bin/blahd.rs @@ -10,7 +10,7 @@ use tokio::signal::unix::{signal, SignalKind}; /// Blah Chat Server #[derive(Debug, clap::Parser)] -#[clap(about, version = option_env!("CFG_RELEASE").unwrap_or(env!("CARGO_PKG_VERSION")))] +#[clap(about, version = env!("CFG_RELEASE"))] enum Cli { /// Run the server with given configuration. Serve { diff --git a/blahd/src/lib.rs b/blahd/src/lib.rs index 56780ff..f968792 100644 --- a/blahd/src/lib.rs +++ b/blahd/src/lib.rs @@ -100,11 +100,7 @@ impl AppState { pub fn new(db: Database, config: ServerConfig) -> Self { let meta = ServerMetadata { server: SERVER_AND_VERSION.into(), - // TODO: Validate this at compile time? - src_url: SERVER_SRC_URL.map(|url| { - url.parse() - .expect("BLAHD_SRC_URL from compile time should be valid") - }), + src_url: SERVER_SRC_URL.map(|url| url.parse().expect("checked by build script")), capabilities: ServerCapabilities { allow_public_register: config.register.enable_public, }, diff --git a/blahd/src/register.rs b/blahd/src/register.rs index 4bfab15..4cf04b9 100644 --- a/blahd/src/register.rs +++ b/blahd/src/register.rs @@ -15,9 +15,7 @@ use serde::Deserialize; use sha2::{Digest, Sha256}; use crate::database::TransactionOps; -use crate::{ApiError, AppState}; - -const USER_AGENT: &str = concat!("blahd/", env!("CARGO_PKG_VERSION")); +use crate::{ApiError, AppState, SERVER_AND_VERSION}; #[derive(Debug, Clone, PartialEq, Eq, Deserialize)] #[serde(default, deny_unknown_fields)] @@ -94,7 +92,7 @@ impl State { pub fn new(config: Config) -> Self { // TODO: Audit this. let client = reqwest::ClientBuilder::new() - .user_agent(USER_AGENT) + .user_agent(SERVER_AND_VERSION) .redirect(reqwest::redirect::Policy::none()) .timeout(Duration::from_secs(config.request_timeout_secs)) .build()