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
//! HomeDisk cloud server
//!
//! [Source code available on GitHub](https://github.com/MedzikUser/HomeDisk)
//!
//! ## Configure
//!
//! Go to [config] module

use std::{
    fs::{self, File},
    io::Write,
    path::Path,
};

use clap::Parser;
use config::Config;
use tracing::{info, warn};

use crate::database::Database;

mod config;
mod database;
mod logger;
mod server;
mod variables;

pub use variables::*;

#[derive(Debug, Parser)]
#[clap(
    name = env!("CARGO_PKG_NAME"),
    about = env!("CARGO_PKG_DESCRIPTION"),
    version = env!("CARGO_PKG_VERSION"),
)]
struct Cli {
    #[clap(short = 'c', long = "config", help = "Configuration file path", default_value = DEFAULT_CONFIG_FILE, display_order = 1)]
    config: String,
    #[clap(short = 'd', long = "database", help = "SQLite database path", default_value = DEFAULT_DATABASE_FILE, display_order = 2)]
    database: String,
}

#[tokio::main]
async fn main() {
    logger::init();

    let args = Cli::parse();

    let config_path = args.config;
    let database_path = args.database;

    // if configuration file doesn't exist, create it
    if !Path::new(&config_path).exists() {
        warn!("Configuration file doesn't exists.");

        let mut file =
            File::create(config_path).expect("notrace - Failed to create configuration file");

        file.write_all(DEFAULT_CONFIG_CONTENT)
            .expect("notrace - Failed to write default configuration to config file");

        info!("Created configuration file. Exiting...");

        std::process::exit(0);
    }

    let config = Config::parse(&config_path).expect("notrace - Failed to parse configuration file");

    // if storage directory doesn't exist, create it
    if !Path::new(&config.storage.path).exists() {
        fs::create_dir_all(&config.storage.path)
            .expect("notrace - Failed to create storage directory");
    }

    // open database connection
    let db =
        // if database file doesn't exists create it
        if !Path::new(&database_path).exists() {
            warn!("Database file doesn't exists.");
            info!("Creating database file...");

            // create an empty database file
            File::create(&database_path).expect("notrace - Failed to create a database file");

            // open database file
            let db = Database::open(&database_path)
                .await
                .expect("notrace - Failed to open database file");

            // create tables in the database
            db.create_tables()
                .await
                .expect("notrace - Failed to create tables in the database");

            db
        }
        // if database file exists
        else {
            Database::open(&database_path)
                .await
                .expect("notrace - Failed to open database file")
        };

    server::start_server(config, db)
        .await
        .expect("notrace - HTTP Server error");
}