A library for writing host-specific, single-binary configuration management and deployment tools
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

81 lines
1.6 KiB

use std::fmt::{Display, Error, Formatter};
use std::num::NonZeroUsize;
use std::path::Path;
#[derive(Clone, Debug, PartialEq, Hash, Eq)]
pub struct FpmPoolConfig {
max_children: NonZeroUsize,
custom: Option<Box<str>>,
}
impl Display for FpmPoolConfig {
fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error> {
match &self.custom {
None => write!(f, "pm.max_children = {}", self.max_children),
Some(custom) => write!(f, "pm.max_children = {}\n{}", self.max_children, custom),
}
}
}
impl From<usize> for FpmPoolConfig {
fn from(max_children: usize) -> Self {
Self {
max_children: NonZeroUsize::try_from(max_children).unwrap(),
custom: None,
}
}
}
impl FpmPoolConfig {
pub fn new(max_children: NonZeroUsize, custom: impl Into<Box<str>>) -> Self {
Self {
max_children,
custom: Some(custom.into()),
}
}
}
pub fn fpm_pool_config<U: AsRef<str>, S: AsRef<Path>>(
user_name: U,
socket_path: S,
config: &FpmPoolConfig,
) -> String {
format!(
"[{0}]
user = {0}
group = www-data
listen = {1}
listen.owner = www-data
pm = ondemand
catch_workers_output = yes
env[PATH] = /usr/local/bin:/usr/bin:/bin
{2}
",
user_name.as_ref(),
socket_path.as_ref().to_str().unwrap(),
config
)
}
#[cfg(test)]
mod test {
use super::fpm_pool_config;
#[test]
fn test_fpm_pool_config() {
assert_eq!(
fpm_pool_config("user", "socket", &5.into()),
r"[user]
user = user
group = www-data
listen = socket
listen.owner = www-data
pm = ondemand
catch_workers_output = yes
env[PATH] = /usr/local/bin:/usr/bin:/bin
pm.max_children = 5
"
);
}
}