diff --git a/src/builder.rs b/src/builder.rs index d238fe2..8cfa7e7 100644 --- a/src/builder.rs +++ b/src/builder.rs @@ -33,7 +33,7 @@ use crate::symbols::wordpress::{ Plugin as WordpressPluginSymbol, Translation as WordpressTranslationSymbol, }; use crate::templates::nginx; -use crate::templates::php::fpm_pool_config as php_fpm_pool_config; +use crate::templates::php::{fpm_pool_config as php_fpm_pool_config, FpmPoolConfig as PhpFpmPoolConfig}; use crate::templates::systemd::{ nodejs_service as systemd_nodejs_service, socket_service as systemd_socket_service, }; @@ -250,16 +250,16 @@ impl + Clone + Display> ImplementationBuilder> for } } -impl> ImplementationBuilder> for DefaultBuilder { +impl, C: Clone + Into> ImplementationBuilder> for DefaultBuilder { type Prerequisites = ( PhpFpmPool, CertChain, Key, AcmeChallengesNginxSnippet, ); - fn prerequisites(resource: &ServePhp) -> Self::Prerequisites { + fn prerequisites(resource: &ServePhp) -> Self::Prerequisites { ( - PhpFpmPool(resource.0.clone(), 10), + PhpFpmPool(resource.0.clone(), resource.4.clone().into()), CertChain(resource.0.clone()), Key(resource.0.clone()), AcmeChallengesNginxSnippet, @@ -271,8 +271,8 @@ impl> ImplementationBuilder> f ReloadServiceSymbol, ); fn create( - resource: &ServePhp, - target: & as Resource>::Artifact, + resource: &ServePhp, + target: & as Resource>::Artifact, (pool, cert, key, challenges_snippet_path): ::Artifact, ) -> Self::Implementation { ( @@ -428,7 +428,7 @@ impl ImplementationBuilder> for DefaultBuilder { ( FileSymbol::new( conf_path.clone().into(), - php_fpm_pool_config(&user_name.0, &socket_path, resource.1), + php_fpm_pool_config(&user_name.0, &socket_path, &resource.1), ), ReloadServiceSymbol::new(StdCommandRunner, service_name.0.clone()), ) diff --git a/src/locator.rs b/src/locator.rs index 9b70ae4..6633d4e 100644 --- a/src/locator.rs +++ b/src/locator.rs @@ -298,11 +298,11 @@ impl, POLICY> ResourceLocator> for DefaultLocator< } } -impl, P, POLICY> ResourceLocator> for DefaultLocator { +impl, P, C, POLICY> ResourceLocator> for DefaultLocator { type Prerequisites = (); fn locate( - resource: &ServePhp, - ) -> ( as Resource>::Artifact, Self::Prerequisites) { + resource: &ServePhp, + ) -> ( as Resource>::Artifact, Self::Prerequisites) { ( PathArtifact::from(Path::new("/etc/nginx/sites-enabled/").join(&resource.0)), (), diff --git a/src/resources/mod.rs b/src/resources/mod.rs index 75f6d3e..d8837f5 100644 --- a/src/resources/mod.rs +++ b/src/resources/mod.rs @@ -2,6 +2,7 @@ use crate::artifacts::{ DatabaseName as DatabaseNameArtifact, Path as PathArtifact, ServiceName as ServiceNameArtifact, UserName as UserNameArtifact, }; +use crate::templates::php::FpmPoolConfig; use std::hash::Hash; use std::path::PathBuf; @@ -146,8 +147,14 @@ impl Resource for ServeCustom { } #[derive(Debug, Hash, PartialEq, Eq)] -pub struct ServePhp(pub D, pub P, pub &'static str, pub String, pub usize); -impl Resource for ServePhp { +pub struct ServePhp( + pub D, + pub P, + pub &'static str, + pub String, + pub C, +); +impl Resource for ServePhp { type Artifact = PathArtifact; } @@ -177,7 +184,7 @@ impl Resource for DefaultServer { } #[derive(Debug, Hash, PartialEq, Eq)] -pub struct PhpFpmPool(pub D, pub usize); +pub struct PhpFpmPool(pub D, pub FpmPoolConfig); impl Resource for PhpFpmPool { type Artifact = ( PathArtifact, @@ -275,6 +282,7 @@ macro_rules! default_resources { } } +// Only one enum entry per resource type, otherwise the equality checks fail default_resources!( AcmeAccountKey: AcmeAccountKey, AcmeChallengesDir: AcmeChallengesDir, @@ -301,7 +309,7 @@ default_resources!( PhpFpmPool: PhpFpmPool, ServeCustom: ServeCustom, ServeService: ServeService, - ServePhp: ServePhp, + ServePhp: ServePhp, ServeRedir: ServeRedir, ServeStatic: ServeStatic, StoredDirectory: StoredDirectory, @@ -310,3 +318,8 @@ default_resources!( WordpressPlugin: WordpressPlugin, WordpressTranslation: WordpressTranslation, ); + +pub fn serve_php, C: Into>(domain: D, path: P, root_filename: &'static str, nginx_config: impl Into, pool_config: C) -> ServePhp { + ServePhp( + domain, path.into(), root_filename, nginx_config.into(), pool_config.into()) +} diff --git a/src/templates/php.rs b/src/templates/php.rs index 87fc6bf..d131fe2 100644 --- a/src/templates/php.rs +++ b/src/templates/php.rs @@ -1,9 +1,40 @@ +use std::fmt::{Display, Error, Formatter}; use std::path::Path; +#[derive(Clone, Debug, PartialEq, Hash, Eq)] +pub struct FpmPoolConfig { + max_children: usize, + custom: Option, +} + +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 for FpmPoolConfig { + fn from(max_children: usize) -> Self { + Self { + max_children, + custom: None, + } + } +} + +impl FpmPoolConfig { + pub fn new(max_children: usize, custom: impl Into) -> Self { + Self { max_children, custom: Some(custom.into()) } + } +} + pub fn fpm_pool_config, S: AsRef>( user_name: U, socket_path: S, - max_children: usize, + config: &FpmPoolConfig, ) -> String { format!( "[{0}] @@ -13,12 +44,12 @@ group = www-data listen = {1} listen.owner = www-data pm = ondemand -pm.max_children = {2} catch_workers_output = yes env[PATH] = /usr/local/bin:/usr/bin:/bin +{2} ", user_name.as_ref(), socket_path.as_ref().to_str().unwrap(), - max_children, + config ) }