1#[derive(Debug)]
2pub struct AwsConfig {
3 pub region: &'static str,
4 pub access_key_id: &'static str,
5 pub secret_access_key: &'static str,
6}
7
8impl Default for AwsConfig {
9 fn default() -> Self {
10 AwsConfig {
11 region: option_env!("AWS_REGION").expect("You must set AWS_REGION"),
12 access_key_id: option_env!("AWS_ACCESS_KEY_ID")
13 .expect("You must set AWS_ACCESS_KEY_ID"),
14 secret_access_key: option_env!("AWS_SECRET_ACCESS_KEY")
15 .expect("AWS_SECRET_ACCESS_KEY is required"),
16 }
17 }
18}
19
20#[derive(Debug, Clone)]
21pub enum AuthConfig {
22 Jwt {
23 secret: &'static str,
24 expiration: u64,
25 },
26}
27
28impl Default for AuthConfig {
29 fn default() -> Self {
30 let auth_type = option_env!("AUTH_TYPE").unwrap_or_else(|| {
31 tracing::warn!("You didn't set AUTH_TYPE and it will be set to jwt by default");
32 "jwt"
33 });
34
35 if auth_type.to_lowercase() == "jwt" {
36 AuthConfig::Jwt {
37 secret: option_env!("JWT_SECRET_KEY").expect("You must set JWT_SECRET_KEY"),
38 expiration: option_env!("JWT_EXPIRATION")
39 .unwrap_or("3600".into())
40 .parse()
41 .expect("EXPIRATION must be a number"),
42 }
43 } else {
44 panic!("AUTH_TYPE must be jwt");
45 }
46 }
47}
48
49#[derive(Debug)]
50pub enum DatabaseConfig {
51 DynamoDb {
52 aws: AwsConfig,
53 table_name: &'static str,
54 },
55 Postgres {
56 url: &'static str,
57 pool_size: u32,
58 },
59 Sqlite {
60 url: &'static str,
61 },
62}
63
64impl Default for DatabaseConfig {
65 fn default() -> Self {
66 match option_env!("DATABASE_TYPE")
67 .expect("You must set DATABASE_TYPE")
68 .to_lowercase()
69 .as_str()
70 {
71 "dynamo" | "dynamodb" => DatabaseConfig::DynamoDb {
72 aws: AwsConfig::default(),
73 table_name: option_env!("TABLE_NAME").expect("You must set TABLE_NAME"),
74 },
75 "rds" | "postgres" => DatabaseConfig::Postgres {
76 url: option_env!("DATABASE_URL").expect("You must set DATABASE_URL"),
77 pool_size: option_env!("POOL_SIZE")
78 .unwrap_or("5".into())
79 .parse()
80 .expect("POOL_SIZE must be a number"),
81 },
82 "sqlite" => DatabaseConfig::Sqlite {
83 url: option_env!("DATABASE_URL").expect("You must set DATABASE_URL"),
84 },
85 _ => panic!("DATABASE_TYPE must be dynamodb or postgres"),
86 }
87 }
88}