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

2 years ago
  1. use std::fmt::{Display, Error, Formatter};
  2. use std::num::NonZeroUsize;
  3. use std::path::Path;
  4. #[derive(Clone, Debug, PartialEq, Hash, Eq)]
  5. pub struct FpmPoolConfig {
  6. max_children: NonZeroUsize,
  7. custom: Option<Box<str>>,
  8. }
  9. impl Display for FpmPoolConfig {
  10. fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error> {
  11. match &self.custom {
  12. None => write!(f, "pm.max_children = {}", self.max_children),
  13. Some(custom) => write!(f, "pm.max_children = {}\n{}", self.max_children, custom),
  14. }
  15. }
  16. }
  17. impl From<usize> for FpmPoolConfig {
  18. fn from(max_children: usize) -> Self {
  19. Self {
  20. max_children: NonZeroUsize::try_from(max_children).unwrap(),
  21. custom: None,
  22. }
  23. }
  24. }
  25. impl FpmPoolConfig {
  26. pub fn new(max_children: NonZeroUsize, custom: impl Into<Box<str>>) -> Self {
  27. Self {
  28. max_children,
  29. custom: Some(custom.into()),
  30. }
  31. }
  32. }
  33. pub fn fpm_pool_config<U: AsRef<str>, S: AsRef<Path>>(
  34. user_name: U,
  35. socket_path: S,
  36. config: &FpmPoolConfig,
  37. ) -> String {
  38. format!(
  39. "[{0}]
  40. user = {0}
  41. group = www-data
  42. listen = {1}
  43. listen.owner = www-data
  44. pm = ondemand
  45. catch_workers_output = yes
  46. env[PATH] = /usr/local/bin:/usr/bin:/bin
  47. {2}
  48. ",
  49. user_name.as_ref(),
  50. socket_path.as_ref().to_str().unwrap(),
  51. config
  52. )
  53. }
  54. #[cfg(test)]
  55. mod test {
  56. use super::fpm_pool_config;
  57. #[test]
  58. fn test_fpm_pool_config() {
  59. assert_eq!(
  60. fpm_pool_config("user", "socket", &5.into()),
  61. r"[user]
  62. user = user
  63. group = www-data
  64. listen = socket
  65. listen.owner = www-data
  66. pm = ondemand
  67. catch_workers_output = yes
  68. env[PATH] = /usr/local/bin:/usr/bin:/bin
  69. pm.max_children = 5
  70. "
  71. );
  72. }
  73. }