1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/*
 * Copyright (C) 2022  Aravinth Manivannan <realaravinth@batsense.net>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */
use std::env;
use std::path::Path;
use std::sync::Arc;

use config::{Config, Environment, File};
#[cfg(not(test))]
use log::{error, warn};

#[cfg(test)]
use std::{println as warn, println as error};

use serde::Deserialize;
use url::Url;

use crate::errors::*;
use crate::page::Page;

#[derive(Debug, Clone, Deserialize)]
pub struct Server {
    pub port: u32,
    pub ip: String,
    pub workers: Option<usize>,
}

impl Server {
    #[cfg(not(tarpaulin_include))]
    pub fn get_ip(&self) -> String {
        format!("{}:{}", self.ip, self.port)
    }
}

#[derive(Debug, Clone, Deserialize)]
pub struct Settings {
    pub server: Server,
    pub source_code: String,
    pub pages: Vec<Arc<Page>>,
}

#[cfg(not(tarpaulin_include))]
impl Settings {
    pub fn new() -> ServiceResult<Self> {
        let mut s = Config::builder();

        const CURRENT_DIR: &str = "./config/default.toml";
        const ETC: &str = "/etc/static-pages/config.toml";

        let mut read_file = false;

        if Path::new(ETC).exists() {
            s = s.add_source(File::with_name(ETC));
            read_file = true;
        }
        if Path::new(CURRENT_DIR).exists() {
            // merging default config from file
            s = s.add_source(File::with_name(CURRENT_DIR));
            read_file = true;
        }

        if let Ok(path) = env::var("PAGES_CONFIG") {
            s = s.add_source(File::with_name(&path));
            read_file = true;
        }

        if !read_file {
            warn!("configuration file not found");
        }

        s = s.add_source(Environment::with_prefix("PAGES").separator("__"));

        let mut settings = s.build()?.try_deserialize::<Settings>()?;
        settings.check_url();
        match env::var("PORT") {
            Ok(val) => {
                settings.server.port = val.parse().unwrap();
            }
            Err(e) => warn!("couldn't interpret PORT: {}", e),
        }

        settings.init();

        Ok(settings)
    }

    pub fn init(&self) {
        for (index, page) in self.pages.iter().enumerate() {
            Url::parse(&page.repo).unwrap();
            let path = Path::new(&page.path);
            if path.exists() && path.is_file() {
                panic!("Path is a file, should be a directory: {:?}", page);
            }

            if !path.exists() {
                std::fs::create_dir_all(&path).unwrap();
            }
            for (index2, page2) in self.pages.iter().enumerate() {
                if index2 == index {
                    continue;
                }
                if page.secret == page2.secret {
                    error!("{}", ServiceError::SecretTaken(page.clone(), page2.clone()));
                } else if page.repo == page2.repo {
                    error!(
                        "{}",
                        ServiceError::DuplicateRepositoryURL(page.clone(), page2.clone(),)
                    );
                } else if page.path == page2.path {
                    error!("{}", ServiceError::PathTaken(page.clone(), page2.clone()));
                }
            }
            if let Err(e) = page.update() {
                error!("{e}");
            }
        }
    }

    #[cfg(not(tarpaulin_include))]
    fn check_url(&self) {
        Url::parse(&self.source_code).expect("Please enter a URL for source_code in settings");
    }
}