File sharing server for small files

config.rs 2.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. use clappconfig::{anyhow, clap::ArgMatches, AppConfig};
  2. use std::collections::HashMap;
  3. use std::time::Duration;
  4. #[derive(Debug, Serialize, Deserialize, Clone)]
  5. #[serde(default)]
  6. #[serde(deny_unknown_fields)]
  7. pub(crate) struct Config {
  8. /// Log level
  9. pub(crate) logging: String,
  10. /// Per-module log levels
  11. pub(crate) log_levels: HashMap<String, String>,
  12. /// Server bind address
  13. pub(crate) host: String,
  14. /// Server port
  15. pub(crate) port: u16,
  16. /// Default expiry time in seconds
  17. #[serde(with = "serde_duration_secs")]
  18. pub(crate) default_expiry: Duration,
  19. /// Max expiry time in seconds
  20. #[serde(with = "serde_duration_secs")]
  21. pub(crate) max_expiry: Duration,
  22. /// Expired post clearing interval (triggered on write)
  23. #[serde(with = "serde_duration_secs")]
  24. pub(crate) expired_gc_interval: Duration,
  25. /// Max uploaded file size in bytes
  26. pub(crate) max_file_size: usize,
  27. /// Enable persistence
  28. pub(crate) persistence: bool,
  29. /// Enable compression when persisting/loading
  30. pub(crate) compression: bool,
  31. /// Persistence file
  32. pub(crate) persist_file: String,
  33. }
  34. impl Default for Config {
  35. fn default() -> Self {
  36. Self {
  37. logging: "debug".to_string(),
  38. log_levels: Default::default(),
  39. host: "0.0.0.0".to_string(),
  40. port: 7745,
  41. default_expiry: Duration::from_secs(60 * 10),
  42. max_expiry: Duration::from_secs(60 * 10),
  43. expired_gc_interval: Duration::from_secs(60),
  44. max_file_size: 1 * (1024 * 1024), // 1MB
  45. persistence: false,
  46. compression: true,
  47. persist_file: "postit.db".to_string(),
  48. }
  49. }
  50. }
  51. impl AppConfig for Config {
  52. type Init = Config;
  53. fn logging(&self) -> &str {
  54. &self.logging
  55. }
  56. fn logging_mod_levels(&self) -> Option<&HashMap<String, String>> {
  57. Some(&self.log_levels)
  58. }
  59. fn configure(self, _clap: &ArgMatches) -> anyhow::Result<Self::Init> {
  60. Ok(self)
  61. }
  62. }
  63. mod serde_duration_secs {
  64. use serde::{self, Deserialize, Deserializer, Serializer};
  65. use std::time::Duration;
  66. pub fn serialize<S>(value: &Duration, se: S) -> Result<S::Ok, S::Error>
  67. where
  68. S: Serializer,
  69. {
  70. se.serialize_u64(value.as_secs())
  71. }
  72. pub fn deserialize<'de, D>(de: D) -> Result<Duration, D::Error>
  73. where
  74. D: Deserializer<'de>,
  75. {
  76. let s: u64 = u64::deserialize(de)?;
  77. Ok(Duration::from_secs(s))
  78. }
  79. }