mirror of
https://github.com/Blah-IM/blahrs.git
synced 2025-05-01 00:31:09 +00:00
build: validate CFG_SRC_URL and simplify CFG_RELEASE handling
This commit is contained in:
parent
fa14844d0d
commit
fac146e859
6 changed files with 32 additions and 12 deletions
|
@ -16,7 +16,10 @@ use reqwest::Url;
|
||||||
use rusqlite::{named_params, prepare_and_bind, Connection};
|
use rusqlite::{named_params, prepare_and_bind, Connection};
|
||||||
use tokio::runtime::Runtime;
|
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.
|
/// Control or manage Blah Chat Server.
|
||||||
#[derive(Debug, clap::Parser)]
|
#[derive(Debug, clap::Parser)]
|
||||||
|
@ -337,7 +340,7 @@ fn build_rt() -> Result<Runtime> {
|
||||||
|
|
||||||
fn build_client() -> Result<reqwest::Client> {
|
fn build_client() -> Result<reqwest::Client> {
|
||||||
reqwest::Client::builder()
|
reqwest::Client::builder()
|
||||||
.user_agent(USER_AGENT)
|
.user_agent(USER_AGENT())
|
||||||
.redirect(reqwest::redirect::Policy::none())
|
.redirect(reqwest::redirect::Policy::none())
|
||||||
.build()
|
.build()
|
||||||
.context("failed to build HTTP client")
|
.context("failed to build HTTP client")
|
||||||
|
|
|
@ -38,6 +38,9 @@ url = { version = "2", features = ["serde"] }
|
||||||
|
|
||||||
blah-types = { path = "../blah-types", features = ["rusqlite"] }
|
blah-types = { path = "../blah-types", features = ["rusqlite"] }
|
||||||
|
|
||||||
|
[build-dependencies]
|
||||||
|
url = "2"
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
expect-test = "1"
|
expect-test = "1"
|
||||||
nix = { version = "0.29", features = ["fs", "process", "signal"] }
|
nix = { version = "0.29", features = ["fs", "process", "signal"] }
|
||||||
|
|
20
blahd/build.rs
Normal file
20
blahd/build.rs
Normal file
|
@ -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::<url::Url>()
|
||||||
|
.expect("CFG_SRC_URL is not a valid URL");
|
||||||
|
} else {
|
||||||
|
println!("cargo::warning=CFG_SRC_URL is not set");
|
||||||
|
}
|
||||||
|
}
|
|
@ -10,7 +10,7 @@ use tokio::signal::unix::{signal, SignalKind};
|
||||||
|
|
||||||
/// Blah Chat Server
|
/// Blah Chat Server
|
||||||
#[derive(Debug, clap::Parser)]
|
#[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 {
|
enum Cli {
|
||||||
/// Run the server with given configuration.
|
/// Run the server with given configuration.
|
||||||
Serve {
|
Serve {
|
||||||
|
|
|
@ -100,11 +100,7 @@ impl AppState {
|
||||||
pub fn new(db: Database, config: ServerConfig) -> Self {
|
pub fn new(db: Database, config: ServerConfig) -> Self {
|
||||||
let meta = ServerMetadata {
|
let meta = ServerMetadata {
|
||||||
server: SERVER_AND_VERSION.into(),
|
server: SERVER_AND_VERSION.into(),
|
||||||
// TODO: Validate this at compile time?
|
src_url: SERVER_SRC_URL.map(|url| url.parse().expect("checked by build script")),
|
||||||
src_url: SERVER_SRC_URL.map(|url| {
|
|
||||||
url.parse()
|
|
||||||
.expect("BLAHD_SRC_URL from compile time should be valid")
|
|
||||||
}),
|
|
||||||
capabilities: ServerCapabilities {
|
capabilities: ServerCapabilities {
|
||||||
allow_public_register: config.register.enable_public,
|
allow_public_register: config.register.enable_public,
|
||||||
},
|
},
|
||||||
|
|
|
@ -15,9 +15,7 @@ use serde::Deserialize;
|
||||||
use sha2::{Digest, Sha256};
|
use sha2::{Digest, Sha256};
|
||||||
|
|
||||||
use crate::database::TransactionOps;
|
use crate::database::TransactionOps;
|
||||||
use crate::{ApiError, AppState};
|
use crate::{ApiError, AppState, SERVER_AND_VERSION};
|
||||||
|
|
||||||
const USER_AGENT: &str = concat!("blahd/", env!("CARGO_PKG_VERSION"));
|
|
||||||
|
|
||||||
#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
|
#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
|
||||||
#[serde(default, deny_unknown_fields)]
|
#[serde(default, deny_unknown_fields)]
|
||||||
|
@ -94,7 +92,7 @@ impl State {
|
||||||
pub fn new(config: Config) -> Self {
|
pub fn new(config: Config) -> Self {
|
||||||
// TODO: Audit this.
|
// TODO: Audit this.
|
||||||
let client = reqwest::ClientBuilder::new()
|
let client = reqwest::ClientBuilder::new()
|
||||||
.user_agent(USER_AGENT)
|
.user_agent(SERVER_AND_VERSION)
|
||||||
.redirect(reqwest::redirect::Policy::none())
|
.redirect(reqwest::redirect::Policy::none())
|
||||||
.timeout(Duration::from_secs(config.request_timeout_secs))
|
.timeout(Duration::from_secs(config.request_timeout_secs))
|
||||||
.build()
|
.build()
|
||||||
|
|
Loading…
Add table
Reference in a new issue