aboutsummaryrefslogtreecommitdiffstats
path: root/cms-backd/src/config.rs
blob: 9d80939efc5845f334cbe0bbf68519f2932f96e0 (plain)
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
// -*- coding: utf-8 -*-
//
// Simple CMS
//
// Copyright (C) 2011-2024 Michael Büsch <m@bues.ch>
//
// Licensed under the Apache License version 2.0
// or the MIT license, at your option.
// SPDX-License-Identifier: Apache-2.0 OR MIT

use crate::numparse::parse_bool;
use anyhow::{self as ah, format_err as err};
use configparser::ini::Ini;

const CONF_PATH: &str = "/opt/cms/etc/cms/backd.conf";
const SECT: &str = "CMS-BACKD";

fn get_debug(ini: &Ini) -> ah::Result<bool> {
    if let Some(debug) = ini.get(SECT, "debug") {
        return parse_bool(&debug);
    }
    Ok(false)
}

fn get_domain(ini: &Ini) -> ah::Result<String> {
    if let Some(domain) = ini.get(SECT, "domain") {
        for c in domain.chars() {
            if !c.is_ascii_alphanumeric() && c != '.' && c != '-' {
                return Err(err!("'domain' has an invalid value."));
            }
        }
        return Ok(domain);
    }
    Ok("example.com".to_string())
}

fn get_url_base(ini: &Ini) -> ah::Result<String> {
    if let Some(url_base) = ini.get(SECT, "url-base") {
        for c in url_base.chars() {
            if !c.is_ascii_alphanumeric() && c != '/' && c != '_' && c != '-' {
                return Err(err!("'url-base' has an invalid value."));
            }
        }
        return Ok(url_base);
    }
    Ok("/cms".to_string())
}

pub struct CmsConfig {
    debug: bool,
    domain: String,
    url_base: String,
}

impl CmsConfig {
    pub fn new() -> ah::Result<Self> {
        let mut ini = Ini::new_cs();
        if let Err(e) = ini.load(CONF_PATH) {
            return Err(err!("Failed to load configuration {CONF_PATH}: {e}"));
        };

        let debug = get_debug(&ini)?;
        let domain = get_domain(&ini)?;
        let url_base = get_url_base(&ini)?;

        Ok(Self {
            debug,
            domain,
            url_base,
        })
    }

    pub fn debug(&self) -> bool {
        self.debug
    }

    pub fn domain(&self) -> &str {
        &self.domain
    }

    pub fn url_base(&self) -> &str {
        &self.url_base
    }
}

// vim: ts=4 sw=4 expandtab
bues.ch cgit interface