1use serde_default::DefaultFromSerde;
2use serde_derive::{Deserialize, Serialize};
3use std::{net::SocketAddr, path::PathBuf, time::Duration};
4use utoipa::ToSchema;
5
6#[derive(Debug, DefaultFromSerde, Clone, PartialEq, Eq, Serialize, Deserialize, ToSchema)]
7pub struct AppConfig {
8 #[serde(with = "humantime_serde", default = "default_background_task_interval")]
9 #[schema(value_type = String, example = "1h")]
10 pub background_task_interval: Duration,
11
12 #[serde(default)]
13 pub admin: AdminConfig,
14
15 #[serde(default)]
16 pub log: LogConfig,
17
18 #[serde(default = "default_http_challenge_addr")]
19 #[schema(value_type = String, example = "0.0.0.0:80")]
20 pub http_challenge_addr: SocketAddr,
21}
22
23fn default_background_task_interval() -> Duration {
24 Duration::from_secs(60 * 60)
25}
26
27fn default_http_challenge_addr() -> SocketAddr {
28 SocketAddr::from(([0, 0, 0, 0], 80))
29}
30
31#[derive(Clone, Serialize, ToSchema)]
32pub struct AppInfo {
33 #[schema(example = "0.0.0")]
34 pub version: &'static str,
35 #[schema(example = "aarch64-apple-darwin")]
36 pub target: &'static str,
37 #[schema(example = "debug")]
38 pub profile: &'static str,
39 #[schema(example = json!([]))]
40 pub features: &'static [&'static str],
41 #[schema(example = "rustc 1.69.0 (84c898d65 2023-04-16)")]
42 pub rustc: &'static str,
43 #[schema(value_type = String, example = "/home/taxy/.config/taxy")]
44 pub config_path: PathBuf,
45 #[schema(value_type = String, example = "/home/taxy/.config/taxy")]
46 pub log_path: PathBuf,
47}
48
49#[derive(Debug, DefaultFromSerde, Copy, Clone, PartialEq, Eq, Serialize, Deserialize, ToSchema)]
50pub struct AdminConfig {
51 #[serde(with = "humantime_serde", default = "default_admin_session_expiry")]
52 #[schema(value_type = String, example = "1d")]
53 pub session_expiry: Duration,
54
55 #[serde(default = "default_max_attempts")]
56 pub max_login_attempts: u32,
57
58 #[serde(with = "humantime_serde", default = "default_login_attempts_reset")]
59 #[schema(value_type = String, example = "15m")]
60 pub login_attempts_reset: Duration,
61}
62
63fn default_admin_session_expiry() -> Duration {
64 Duration::from_secs(60 * 60)
65}
66
67fn default_max_attempts() -> u32 {
68 10
69}
70
71fn default_login_attempts_reset() -> Duration {
72 Duration::from_secs(60 * 15)
73}
74
75#[derive(Debug, DefaultFromSerde, Copy, Clone, PartialEq, Eq, Serialize, Deserialize, ToSchema)]
76pub struct LogConfig {
77 #[serde(with = "humantime_serde", default = "default_database_log_retention")]
78 #[schema(value_type = String, example = "3months")]
79 pub database_log_retention: Duration,
80}
81
82fn default_database_log_retention() -> Duration {
83 Duration::from_secs(60 * 60 * 24 * 30 * 3)
84}