flams/server/
settings.rs

1use clap::Parser;
2use flams_utils::settings::GitlabSettings;
3use core::panic;
4use flams_system::settings::{BuildQueueSettings, ServerSettings, SettingsSpec};
5use std::path::{Path, PathBuf};
6
7#[derive(Parser,Debug)]
8#[command(propagate_version = true, version, about, long_about = Some(
9"𝖥𝖫∀𝖬∫ - Flexiformal Annotation Management System\n\
10--------------------------------------------------------------------\n\
11See the \u{1b}]8;;https://github.com/UniFormal/MMT\u{1b}\\documentation\u{1b}]8;;\u{1b}\\ for details"
12))]
13struct Cli {
14    /// a comma-separated list of `MathHub` paths (if not given, the default paths are used
15    /// as determined by the MATHHUB system variable or ~/.mathhub/mathhub.path)
16    #[arg(short, long)]
17    pub(crate) mathhubs: Option<String>,
18
19    /// whether to enable debug logging
20    #[arg(short, long)]
21    pub(crate) debug: Option<bool>,
22
23    #[arg(short, long)]
24    /// The toml config file to use
25    pub(crate) config_file: Option<PathBuf>,
26
27    #[arg(short, long)]
28    /// The log directory to use
29    pub(crate) log_dir: Option<PathBuf>,
30
31    #[arg(long)]
32    /// The directory used for temporary files
33    pub(crate) temp_dir: Option<PathBuf>,
34
35    #[arg(short, long)]
36    /// The admin password to use for the server
37    pub(crate) admin_pwd: Option<String>,
38
39    /// Network port to use for the server
40    #[arg(long,value_parser = clap::value_parser!(u16).range(1..))]
41    pub(crate) port: Option<u16>,
42
43    /// Network address to use for the server
44    #[arg(long)]
45    pub(crate) ip: Option<String>,
46
47    #[arg(long)]
48    pub(crate) external_url: Option<String>,
49
50    #[arg(long)]
51    /// The database file to use for account management etc.
52    pub(crate) db: Option<PathBuf>,
53
54    /// The number of threads to use for the buildqueue
55    #[arg(short, long)]
56    pub(crate) threads: Option<u8>,
57
58    /// enter lsp mode
59    #[arg(long)]
60    pub(crate) lsp: bool,
61
62    #[arg(long)]
63    pub(crate) gitlab_url: Option<String>,
64
65    #[arg(long)]
66    pub(crate) gitlab_token: Option<String>,
67    #[arg(long)]
68    pub(crate) gitlab_app_id: Option<String>,
69    #[arg(long)]
70    pub(crate) gitlab_app_secret: Option<String>,
71    #[arg(long)]
72    pub(crate) gitlab_redirect_url: Option<String>
73}
74impl From<Cli> for (Option<PathBuf>, SettingsSpec) {
75    /// #### Panics
76    fn from(cli: Cli) -> Self {
77        let settings = SettingsSpec {
78            mathhubs: cli
79                .mathhubs
80                .map(|s| {
81                    s.split(',')
82                        .map(|s| PathBuf::from(s.trim()).into_boxed_path())
83                        .collect()
84                })
85                .unwrap_or_default(),
86            debug: cli.debug,
87            database: cli.db.map(PathBuf::into_boxed_path),
88            log_dir: cli.log_dir.map(PathBuf::into_boxed_path),
89            temp_dir: cli.temp_dir.map(PathBuf::into_boxed_path),
90            server: ServerSettings {
91                port: cli.port.unwrap_or_default(),
92                ip: cli.ip.map(|s| s.parse().expect("Illegal ip")),
93                admin_pwd: cli.admin_pwd,
94                external_url: cli.external_url
95            },
96            buildqueue: BuildQueueSettings {
97                num_threads: cli.threads,
98            },
99            gitlab: GitlabSettings {
100                url: cli.gitlab_url.map(|s| s.parse().expect("Illegal url")),
101                token: cli.gitlab_token.map(Into::into),
102                app_id: cli.gitlab_app_id.map(Into::into),
103                app_secret: cli.gitlab_app_secret.map(Into::into),
104                redirect_url: cli.gitlab_redirect_url.map(Into::into),
105            },
106            lsp: cli.lsp
107        };
108        (cli.config_file, settings)
109    }
110}
111
112impl Cli {
113    #[must_use]#[inline]
114    fn get() -> Self {
115        Self::parse()
116    }
117}
118
119#[must_use]
120#[allow(clippy::missing_panics_doc)]
121pub fn get_settings() -> SettingsSpec {
122    fn from_file(cfg_file:&Path) -> SettingsSpec {
123        let cfg = std::fs::read_to_string(cfg_file).unwrap_or_else(|e| {
124            panic!("Could not read config file {}: {e}", cfg_file.display())
125        });
126        let cfg: SettingsSpec = toml::from_str(&cfg).unwrap_or_else(|e| {
127            panic!("Could not parse config file {}: {e}", cfg_file.display())
128        });
129        cfg
130    }
131    let cli = Cli::get();
132    let (cfg, mut settings) = cli.into();
133    settings += SettingsSpec::from_envs();
134    if let Some(cfg_file) = cfg {
135        if cfg_file.exists() {
136            settings += from_file(&cfg_file);
137        } else {
138            panic!("Could not find config file {}", cfg_file.display());
139        }
140    } else if let Ok(path) = std::env::current_exe() {
141        if let Some(path) = path.parent() {
142            let path = path.join("settings.toml");
143            if path.exists() {
144                settings += from_file(&path);
145            }
146        }
147    }
148    settings
149}