2019-03-21 09:30:33 +00:00
|
|
|
use rocket::config::Limits;
|
2019-03-22 18:51:36 +00:00
|
|
|
use rocket::Config as RocketConfig;
|
|
|
|
use std::env::{self, var};
|
2019-03-21 09:30:33 +00:00
|
|
|
|
|
|
|
#[cfg(not(test))]
|
2019-03-22 18:51:36 +00:00
|
|
|
const DB_NAME: &str = "plume";
|
2019-03-21 09:30:33 +00:00
|
|
|
#[cfg(test)]
|
|
|
|
const DB_NAME: &str = "plume_tests";
|
|
|
|
|
|
|
|
pub struct Config {
|
|
|
|
pub base_url: String,
|
|
|
|
pub database_url: String,
|
2019-03-21 10:51:41 +00:00
|
|
|
pub db_name: &'static str,
|
2019-03-21 09:30:33 +00:00
|
|
|
pub search_index: String,
|
|
|
|
pub rocket: Result<RocketConfig, RocketError>,
|
2019-03-21 10:51:41 +00:00
|
|
|
pub logo: LogoConfig,
|
2019-03-21 09:30:33 +00:00
|
|
|
}
|
|
|
|
|
2019-03-22 18:51:36 +00:00
|
|
|
#[derive(Debug, Clone)]
|
2019-03-21 09:30:33 +00:00
|
|
|
pub enum RocketError {
|
|
|
|
InvalidEnv,
|
|
|
|
InvalidAddress,
|
|
|
|
InvalidSecretKey,
|
|
|
|
}
|
|
|
|
|
|
|
|
fn get_rocket_config() -> Result<RocketConfig, RocketError> {
|
|
|
|
let mut c = RocketConfig::active().map_err(|_| RocketError::InvalidEnv)?;
|
|
|
|
|
|
|
|
let address = var("ROCKET_ADDRESS").unwrap_or_else(|_| "localhost".to_owned());
|
2019-03-22 18:51:36 +00:00
|
|
|
let port = var("ROCKET_PORT")
|
|
|
|
.ok()
|
|
|
|
.map(|s| s.parse::<u16>().unwrap())
|
|
|
|
.unwrap_or(7878);
|
2019-03-21 09:30:33 +00:00
|
|
|
let secret_key = var("ROCKET_SECRET_KEY").map_err(|_| RocketError::InvalidSecretKey)?;
|
2019-03-22 18:51:36 +00:00
|
|
|
let form_size = var("FORM_SIZE")
|
|
|
|
.unwrap_or_else(|_| "32".to_owned())
|
|
|
|
.parse::<u64>()
|
|
|
|
.unwrap();
|
|
|
|
let activity_size = var("ACTIVITY_SIZE")
|
|
|
|
.unwrap_or_else(|_| "1024".to_owned())
|
|
|
|
.parse::<u64>()
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
c.set_address(address)
|
|
|
|
.map_err(|_| RocketError::InvalidAddress)?;
|
2019-03-21 09:30:33 +00:00
|
|
|
c.set_port(port);
|
2019-03-22 18:51:36 +00:00
|
|
|
c.set_secret_key(secret_key)
|
|
|
|
.map_err(|_| RocketError::InvalidSecretKey)?;
|
2019-03-21 09:30:33 +00:00
|
|
|
|
2019-03-22 18:51:36 +00:00
|
|
|
c.set_limits(
|
|
|
|
Limits::new()
|
|
|
|
.limit("forms", form_size * 1024)
|
|
|
|
.limit("json", activity_size * 1024),
|
|
|
|
);
|
2019-03-21 09:30:33 +00:00
|
|
|
|
|
|
|
Ok(c)
|
|
|
|
}
|
|
|
|
|
2019-03-21 10:51:41 +00:00
|
|
|
pub struct LogoConfig {
|
|
|
|
pub main: String,
|
|
|
|
pub favicon: String,
|
2019-03-26 11:45:17 +00:00
|
|
|
pub other: Vec<Icon>, //url, size, type
|
2019-03-21 10:51:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Serialize)]
|
|
|
|
pub struct Icon {
|
|
|
|
pub src: String,
|
|
|
|
#[serde(skip_serializing_if = "Option::is_none")]
|
|
|
|
pub sizes: Option<String>,
|
|
|
|
#[serde(skip_serializing_if = "Option::is_none")]
|
|
|
|
#[serde(rename = "type")]
|
|
|
|
pub image_type: Option<String>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Icon {
|
|
|
|
pub fn with_prefix(&self, prefix: &str) -> Icon {
|
|
|
|
Icon {
|
|
|
|
src: format!("{}/{}", prefix, self.src),
|
|
|
|
sizes: self.sizes.clone(),
|
|
|
|
image_type: self.image_type.clone(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for LogoConfig {
|
|
|
|
fn default() -> Self {
|
|
|
|
let to_icon = |(src, sizes, image_type): &(&str, Option<&str>, Option<&str>)| Icon {
|
|
|
|
src: str::to_owned(src),
|
|
|
|
sizes: sizes.map(str::to_owned),
|
2019-03-26 11:45:17 +00:00
|
|
|
image_type: image_type.map(str::to_owned),
|
2019-03-21 10:51:41 +00:00
|
|
|
};
|
|
|
|
let icons = [
|
|
|
|
(
|
|
|
|
"icons/trwnh/feather/plumeFeather48.png",
|
|
|
|
Some("48x48"),
|
|
|
|
Some("image/png"),
|
|
|
|
),
|
|
|
|
(
|
|
|
|
"icons/trwnh/feather/plumeFeather72.png",
|
|
|
|
Some("72x72"),
|
|
|
|
Some("image/png"),
|
|
|
|
),
|
|
|
|
(
|
|
|
|
"icons/trwnh/feather/plumeFeather96.png",
|
|
|
|
Some("96x96"),
|
|
|
|
Some("image/png"),
|
|
|
|
),
|
|
|
|
(
|
|
|
|
"icons/trwnh/feather/plumeFeather144.png",
|
|
|
|
Some("144x144"),
|
|
|
|
Some("image/png"),
|
|
|
|
),
|
|
|
|
(
|
|
|
|
"icons/trwnh/feather/plumeFeather160.png",
|
|
|
|
Some("160x160"),
|
|
|
|
Some("image/png"),
|
|
|
|
),
|
|
|
|
(
|
|
|
|
"icons/trwnh/feather/plumeFeather192.png",
|
|
|
|
Some("192x192"),
|
|
|
|
Some("image/png"),
|
|
|
|
),
|
|
|
|
(
|
|
|
|
"icons/trwnh/feather/plumeFeather256.png",
|
|
|
|
Some("256x256"),
|
|
|
|
Some("image/png"),
|
|
|
|
),
|
|
|
|
(
|
|
|
|
"icons/trwnh/feather/plumeFeather512.png",
|
|
|
|
Some("512x512"),
|
|
|
|
Some("image/png"),
|
|
|
|
),
|
2019-03-26 11:45:17 +00:00
|
|
|
("icons/trwnh/feather/plumeFeather.svg", None, None),
|
|
|
|
]
|
|
|
|
.iter()
|
|
|
|
.map(to_icon)
|
|
|
|
.collect();
|
2019-03-21 10:51:41 +00:00
|
|
|
|
|
|
|
let custom_main = var("PLUME_LOGO").ok();
|
2019-03-26 11:45:17 +00:00
|
|
|
let custom_favicon = var("PLUME_LOGO_FAVICON")
|
|
|
|
.ok()
|
|
|
|
.or_else(|| custom_main.clone());
|
2019-03-21 10:51:41 +00:00
|
|
|
let other = if let Some(main) = custom_main.clone() {
|
|
|
|
let ext = |path: &str| match path.rsplitn(2, '.').next() {
|
|
|
|
Some("png") => Some("image/png".to_owned()),
|
2019-03-26 11:45:17 +00:00
|
|
|
Some("jpg") | Some("jpeg") => Some("image/jpeg".to_owned()),
|
2019-03-21 10:51:41 +00:00
|
|
|
Some("svg") => Some("image/svg+xml".to_owned()),
|
|
|
|
Some("webp") => Some("image/webp".to_owned()),
|
|
|
|
_ => None,
|
|
|
|
};
|
|
|
|
let mut custom_icons = env::vars()
|
2019-03-26 11:45:17 +00:00
|
|
|
.filter_map(|(var, val)| {
|
|
|
|
if var.starts_with("PLUME_LOGO_") {
|
|
|
|
Some((var[11..].to_owned(), val))
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.filter_map(|(var, val)| var.parse::<u64>().ok().map(|var| (var, val)))
|
|
|
|
.map(|(dim, src)| Icon {
|
2019-03-21 10:51:41 +00:00
|
|
|
image_type: ext(&src),
|
|
|
|
src,
|
|
|
|
sizes: Some(format!("{}x{}", dim, dim)),
|
|
|
|
})
|
|
|
|
.collect::<Vec<_>>();
|
|
|
|
custom_icons.push(Icon {
|
|
|
|
image_type: ext(&main),
|
|
|
|
src: main,
|
|
|
|
sizes: None,
|
|
|
|
});
|
|
|
|
custom_icons
|
|
|
|
} else {
|
|
|
|
icons
|
|
|
|
};
|
|
|
|
|
|
|
|
LogoConfig {
|
2019-03-26 11:45:17 +00:00
|
|
|
main: custom_main
|
|
|
|
.unwrap_or_else(|| "icons/trwnh/feather/plumeFeather256.png".to_owned()),
|
|
|
|
favicon: custom_favicon.unwrap_or_else(|| {
|
|
|
|
"icons/trwnh/feather-filled/plumeFeatherFilled64.png".to_owned()
|
|
|
|
}),
|
2019-03-21 10:51:41 +00:00
|
|
|
other,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-21 09:30:33 +00:00
|
|
|
lazy_static! {
|
|
|
|
pub static ref CONFIG: Config = Config {
|
|
|
|
base_url: var("BASE_URL").unwrap_or_else(|_| format!(
|
2019-03-22 18:51:36 +00:00
|
|
|
"127.0.0.1:{}",
|
2019-04-06 15:41:57 +00:00
|
|
|
var("ROCKET_PORT").unwrap_or_else(|_| "7878".to_owned())
|
2019-03-22 18:51:36 +00:00
|
|
|
)),
|
2019-03-21 09:30:33 +00:00
|
|
|
db_name: DB_NAME,
|
|
|
|
#[cfg(feature = "postgres")]
|
2019-03-22 18:51:36 +00:00
|
|
|
database_url: var("DATABASE_URL")
|
|
|
|
.unwrap_or_else(|_| format!("postgres://plume:plume@localhost/{}", DB_NAME)),
|
2019-03-21 09:30:33 +00:00
|
|
|
#[cfg(feature = "sqlite")]
|
2019-03-22 18:51:36 +00:00
|
|
|
database_url: var("DATABASE_URL").unwrap_or_else(|_| format!("{}.sqlite", DB_NAME)),
|
2019-03-21 09:30:33 +00:00
|
|
|
search_index: var("SEARCH_INDEX").unwrap_or_else(|_| "search_index".to_owned()),
|
2019-03-21 10:51:41 +00:00
|
|
|
rocket: get_rocket_config(),
|
|
|
|
logo: LogoConfig::default(),
|
2019-03-21 09:30:33 +00:00
|
|
|
};
|
|
|
|
}
|