Skip to main content

shorty_conf/
lib.rs

1// Copyright 2019 Federico Fissore
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//   http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15use std::env;
16
17#[derive(Debug, Clone)]
18pub struct Config {
19    pub redis_host: String,
20    pub redis_port: String,
21    pub rate_limit_period: usize,
22    pub rate_limit: i64,
23    pub id_length: usize,
24    pub id_alphabet: Vec<char>,
25    pub id_generation_max_attempts: u8,
26    pub api_key_mandatory: bool,
27    pub host: String,
28    pub port: String,
29}
30
31impl Config {
32    pub fn new() -> Config {
33        let redis_host =
34            env::var("SHORTENER_REDIS_HOST").unwrap_or_else(|_| String::from("127.0.0.1"));
35        let redis_port = env::var("SHORTENER_REDIS_PORT").unwrap_or_else(|_| String::from("6379"));
36
37        let rate_limit_period = env::var("SHORTENER_RATE_LIMIT_PERIOD")
38            .unwrap_or_else(|_| String::from("600"))
39            .parse::<usize>()
40            .unwrap();
41        let rate_limit = env::var("SHORTENER_RATE_LIMIT")
42            .unwrap_or_else(|_| String::from("10"))
43            .parse::<i64>()
44            .unwrap();
45
46        let id_length = env::var("SHORTENER_ID_LENGTH")
47            .unwrap_or_else(|_| String::from("10"))
48            .parse::<usize>()
49            .unwrap();
50        let id_alphabet = vec![
51            (b'a'..=b'z').map(char::from).collect::<Vec<_>>(),
52            (b'A'..=b'Z').map(char::from).collect::<Vec<_>>(),
53            (b'0'..=b'9').map(char::from).collect::<Vec<_>>(),
54        ]
55        .into_iter()
56        .flatten()
57        .collect::<Vec<char>>();
58        let id_generation_max_attempts = env::var("SHORTENER_ID_GENERATION_MAX_ATTEMPTS")
59            .unwrap_or_else(|_| String::from("10"))
60            .parse::<u8>()
61            .unwrap();
62
63        let host = env::var("SHORTENER_HOST").unwrap_or_else(|_| String::from("127.0.0.1"));
64        let port = env::var("SHORTENER_PORT").unwrap_or_else(|_| String::from("8088"));
65
66        let api_key_mandatory = env::var("SHORTENER_API_KEY_MANDATORY")
67            .unwrap_or_else(|_| String::from("true"))
68            .parse::<bool>()
69            .unwrap();
70
71        Config {
72            redis_host,
73            redis_port,
74            rate_limit_period,
75            rate_limit,
76            id_length,
77            id_alphabet,
78            id_generation_max_attempts,
79            api_key_mandatory,
80            host,
81            port,
82        }
83    }
84}